MegaProcessor

Check-in [8d5232f487]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added operand evaluation with symbol-table lookup, TODO: integrate it with the appropriate opcode-generating functions. In this commit, only "jmp start" gets assembled correctly.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:8d5232f487e2ae92ddc68a0f2ac9f5152f1308e7
User & Date: jos 2015-09-19 17:59:08
Context
2015-09-22
11:38
A little bit of cleanup. check-in: 69958f5974 user: jos tags: trunk
2015-09-19
17:59
Added operand evaluation with symbol-table lookup, TODO: integrate it with the appropriate opcode-generating functions. In this commit, only "jmp start" gets assembled correctly. check-in: 8d5232f487 user: jos tags: trunk
2015-08-22
18:12
DRYed up the output of listing-file lines. check-in: dcc9eecbd2 user: jos tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to eval_op.lua.

28
29
30
31
32
33
34



































































































    local result = opVal[op1];
    if( result == nil ) then
        Error("ADDQ value must be #2, #1, #-1 or #-2");
    end
    return result ;
end









































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

    local result = opVal[op1];
    if( result == nil ) then
        Error("ADDQ value must be #2, #1, #-1 or #-2");
    end
    return result ;
end

--------------------------------------------------------------------------------
-- Chapter: Evaluate symbol:
--------------------------------------------------------------------------------
require( './lib' );
require( 'symtab' );

--
-- Take a value in range 0..2^32-1 and return a table of byte-values
-- in little-endian order
--

function As_table(val)
    local r = {};

    while( val > 0 ) do
        local b = math.fmod(val,256);
        table.insert(r,b);
        val = math.floor(val / 256);
    end

    return r ;
end

--
-- Format array of byte-values into one string of
-- hex-format bytes separated by spaces.
--

function TableFormat( t )
    local r = "";
    t = t or {};
    for _,v in ipairs(t) do
        r = r .. string.format("%02x ",v) ;
    end
    return r ;
end

-- 
-- Evaluate a constant expression in any of the formats below
--    Decimal, just digits
--    Hex, prefix 0x or 0X
--    Binary, prefix 0b or '%'
--    String, enclosed in single or double quotes.
--    Symbol, start with underscore or letter, followed by one or more letters/digits/underscores
--
-- Return a table with the bytes that compose the value described object.
-- [==[
function Eval(exp)
    local capture, idx;

    if( exp == '' ) then return nil end;
    -- Decimal, just digits.
    capture = exp:match("^(%d+)") 
    if( capture and not exp:match("%D+") ) then return( As_table(tonumber(capture)) ); end;


    -- Hex, prefix 0x or 0X
    capture = exp:match("^(0[xX]%x+)") 
    if( capture and not exp:match("%X+",3) ) then return( As_table(tonumber(capture)) ); end;

    -- Binary, prefix 0b, 0B, or '%'
    idx,capture = exp:match("^0[bB]()([01]+)") 
    if( not idx ) then idx,capture = exp:match("^%%()([01]+)") end;

    if( not exp:match("[^01]+",idx) ) then
        local result = 0;
        for i = 1,capture:len() do
            result = result * 2;
            if( capture:sub(i,i) == '1' ) then result = result + 1; end;
        end--for
        return( As_table(result) );
    end

    -- String, enclosed in single or double-quotes.
    capture = exp:match("'(.+)'") or exp:match('"(.+)"') ;
    if( capture ) then
        local result = {};
        for i = 1,capture:len() do
            table.insert( result, capture:byte(i) );
        end--for
        return result ;
    end--if


    capture = exp:match("^([_%a]+[_%w]*)");
    if( capture ) then
        local result = symtab_lookup(capture);
        if( not result ) then
            print("Undefined symbol");
            return( nil );
        end
        return( As_table( result ) );
    end

end--Eval()

--EOF--

Changes to opcodes.lua.

187
188
189
190
191
192
193


194
195
196
197
198
199
200
201
...
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
...
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
end

function do_JMP(f1, f2, f3)
    if( f2:sub(1,1) == '(' ) then
        -- TODO check for (r0)
        return 0xf2 ;
    else


        return 0xf3,0x00,0x00 ;
    end
end

function do_JSR(f1, f2, f3)
    if( f2:sub(1,1) == '(' ) then
        -- TODO check for (r0)
        return 0xce ;
................................................................................
function len_A_O(f1,f2,f3)
    if( f2:upper() == 'PS')
        then return 2
        else return 1
    end
end

function len_JMP(f1,f2,f3)
    if( f2:sub(1,1) == '(' ) then return 1 else return 3 end;
end

function len_LD_B(f1,f2,f3)
    -- print(f1,f2,f3);
    if( f3:sub(1,1) == "#" ) then return 2 end;  -- LD.B Rx, #0xWW (4 cases)
    if( f3:match("^%( *[sS][pP]") ) then return 2 end; -- LD.B Rx,(SP,0xWW) (4 cases)
    if( f3:match("^%( *[rR]") ) then return 1 end; -- LD.B Ra,(Rb[++]) (8 cases)
    return 3; -- LD.B 0xWWWW (4 cases)
end

function len_LD_W(f1,f2,f3)
    -- print(f1,f2,f3);
    if( f3:sub(1,1) == "#" ) then return 3 end;  -- LD.W Rx, #0xWWWW (4 cases)
    if( f3:match("^%( *[sS][pP]") ) then return 2 end; -- LD.W Rx,(SP,0xWW) (4 cases)
    if( f3:match("^%( *[rR]") ) then return 1 end; -- LD.W Ra,(Rb[++]) (8 cases)
    return 3; -- LD.W 0xWWWW (4 cases)
end

function len_ST_B(f1,f2,f3)
................................................................................
  ['DIVS'] = { 1, do_DIVS },
  ['DIVU'] = { 1, do_DIVU },
  ['DIV.S'] = { 1, do_DIVS },  -- Alias for DIVS
  ['DIV.U'] = { 1, do_DIVU },  -- Alias for DIVU
  ['INC'] = { 1, do_INC },
  ['INV'] = { 1, do_INV },
  ['JMP'] = { len_JMP, do_JMP },  -- 1 if op1 = (R0), otherwise 3.
  ['JSR'] = { len_JMP, do_JSR },  -- 1 if op1 = (R0), otherwise 3.
  ['LD.B'] = { len_LD_B, do_LD_B },
  ['LD.W'] = { len_LD_W, do_LD_W },
  ['LSL'] = { 2, do_LSL },
  ['LSL.WT'] = { 2, do_LSL_WT },
  ['LSR'] = { 2, do_LSR },
  ['LSR.WT'] = { 2, do_LSR_WT },
  ['MOVE'] = { 1, do_MOVE },







>
>
|







 







|




<







<







 







|







187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
...
468
469
470
471
472
473
474
475
476
477
478
479

480
481
482
483
484
485
486

487
488
489
490
491
492
493
...
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
end

function do_JMP(f1, f2, f3)
    if( f2:sub(1,1) == '(' ) then
        -- TODO check for (r0)
        return 0xf2 ;
    else
        local dst = Eval(f2);
        if( #dst == 1 ) then table.insert(dst,0x00); end--if
        return 0xf3, table.unpack(dst) ;
    end
end

function do_JSR(f1, f2, f3)
    if( f2:sub(1,1) == '(' ) then
        -- TODO check for (r0)
        return 0xce ;
................................................................................
function len_A_O(f1,f2,f3)
    if( f2:upper() == 'PS')
        then return 2
        else return 1
    end
end

function len_JMP(f1,f2,f3)  -- For instruction length, JMP and JSR follow same rule.
    if( f2:sub(1,1) == '(' ) then return 1 else return 3 end;
end

function len_LD_B(f1,f2,f3)

    if( f3:sub(1,1) == "#" ) then return 2 end;  -- LD.B Rx, #0xWW (4 cases)
    if( f3:match("^%( *[sS][pP]") ) then return 2 end; -- LD.B Rx,(SP,0xWW) (4 cases)
    if( f3:match("^%( *[rR]") ) then return 1 end; -- LD.B Ra,(Rb[++]) (8 cases)
    return 3; -- LD.B 0xWWWW (4 cases)
end

function len_LD_W(f1,f2,f3)

    if( f3:sub(1,1) == "#" ) then return 3 end;  -- LD.W Rx, #0xWWWW (4 cases)
    if( f3:match("^%( *[sS][pP]") ) then return 2 end; -- LD.W Rx,(SP,0xWW) (4 cases)
    if( f3:match("^%( *[rR]") ) then return 1 end; -- LD.W Ra,(Rb[++]) (8 cases)
    return 3; -- LD.W 0xWWWW (4 cases)
end

function len_ST_B(f1,f2,f3)
................................................................................
  ['DIVS'] = { 1, do_DIVS },
  ['DIVU'] = { 1, do_DIVU },
  ['DIV.S'] = { 1, do_DIVS },  -- Alias for DIVS
  ['DIV.U'] = { 1, do_DIVU },  -- Alias for DIVU
  ['INC'] = { 1, do_INC },
  ['INV'] = { 1, do_INV },
  ['JMP'] = { len_JMP, do_JMP },  -- 1 if op1 = (R0), otherwise 3.
  ['JSR'] = { len_JMP, do_JSR },  -- For instruction length, JMP and JSR follow same rule.
  ['LD.B'] = { len_LD_B, do_LD_B },
  ['LD.W'] = { len_LD_W, do_LD_W },
  ['LSL'] = { 2, do_LSL },
  ['LSL.WT'] = { 2, do_LSL_WT },
  ['LSR'] = { 2, do_LSR },
  ['LSR.WT'] = { 2, do_LSR_WT },
  ['MOVE'] = { 1, do_MOVE },