~nch/python-compiler

ref: cb5fb802215620641e7af3a6cca7f1b0bbff8d9a python-compiler/test_compiler.py -rw-r--r-- 12.7 KiB View raw
cb5fb802 — nc better debug messages 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/env python3
import unittest
from compiler import *
from collections.abc import Iterable
import textwrap
import os

class Var:
    def __init__(self, name):
        self.name = name

    def __hash__(self):
        return hash(self.name)

class TestCompiler(unittest.TestCase):
    # XXX: env can't be {} otherwise it doesn't reset the variable because python evals default values only at load
    def unify(self, a, b, env=None):
        env = {} if env is None else env
        def wlg(a, b, cond, then):
            if cond(a, b):
                return then(a, b)
            if cond(b, a):
                return then(b, a)
        def chase(v):
            if type(v) is Var:
                if v.name in env:
                    return chase(env[v.name])
            return v

        if type(a) != str and type(b) != str and isinstance(a, Iterable) and isinstance(b, Iterable):
            if len(a) != len(b):
                return None
            r = {}
            for x, y in zip(a, b):
                u = self.unify(x, y, env)
                if u is None:
                    return None
                else:
                    for k, v in u.items():
                        r[k] = v
            return r

        a = chase(a)
        b = chase(b)

        def assign_var(a, b):
            env[a.name] = b
            return env

        if wlg(a, b,
                lambda a, b: type(a) == Var and type(b) != Var,
                assign_var):
            return env
        if type(a) == type(b) == Var:
            return env

        if type(a) == Var and type(b) != Var:
            env[a] = b

        if a == b:
            return env

        return None

    def test_unify(self):
        self.assertNotEqual(self.unify(1, 1), None)
        self.assertEqual(self.unify(1, 2), None)
        self.assertEqual(self.unify(Var('a'), 2), {'a': 2})
        self.assertEqual(self.unify([1, Var('a'), 3], [1, 2, 3]), {'a': 2})
        self.assertEqual(self.unify([1, Var('a'), 2], [1, 2, 3]), None)
        # XXX: Doesn't work, because we chase down 'a' before 'b' is assigned...
        # need to backpropagate values if this is to work. This feature isn't really needed atm, though...
        # self.assertEqual(self.unify([1, Var('a'), 2], [1, Var('b'), Var('b')]), {'a': 2, 'b': 2})
        self.assertEqual(self.unify([('a', Var('x'), 'c'), ('d', Var('x'), 'f')],
                                    [('a', 'b', 'c'), ('d', 'b', 'f')]), {'x': 'b'})
        self.assertEqual(self.unify([('a', Var('x'), 'c'), ('d', Var('x'), 'f')],
                                    [('a', 'b', 'c'), ('d', 'e', 'f')]), None)

    def test_stream(self):
        s = Stream('''Some
text
on some lines''')
        self.assertEqual(s.row, 0)
        self.assertEqual(s.col, 0)

        s.i = 6
        self.assertEqual(s.row, 1)
        self.assertEqual(s.col, 2)

    def test_identifier(self):
        self.assertEqual(parse('whee whoo', identifier), 'whee')
        self.assertEqual(parse('wh3e whoo', identifier), 'wh3e')

    def test_parse_number(self):
        self.assertEqual(parse('32 45', number), 32)
        self.assertEqual(parse('007', number), 7)
        self.assertEqual(parse('0x13', number), 0)

    def test_expr(self):
        self.assertEqual(parse('a=b', identifier), 'a')
        self.assertEqual(parse('a=b', expr), 'a')

    def test_stmt(self):
        self.assertEqual(parse('a=b\n', stmt), ['=', 'a', 'b'])
        self.assertEqual(parse('a(b)\n', stmt), ['call', 'a', ['b']])

    def test_function_call(self):
        self.assertEqual(parse('f(1)', function_call), ['call', 'f', [1]])
        self.assertEqual(parse('f(2, x)', function_call), ['call', 'f', [2, 'x']])
        self.assertEqual(parse('fcall(g(x), 2)', function_call), ['call', 'fcall', [['call', 'g', ['x']], 2]])

    def test_assign_stmt(self):
        self.assertEqual(parse('a = b', assign_stmt_body), ['=', 'a', 'b'])
        self.assertEqual(parse('a = f(b, 1)', assign_stmt_body), ['=', 'a', ['call', 'f', ['b', 1]]])

    def test_return_stmt(self):
        self.assertEqual(parse('return 3', return_stmt_body), ['return', 3])
        self.assertEqual(parse('return f(3, 4)', return_stmt_body), ['return', ['call', 'f', [3, 4]]])

    def test_parse_identifier(self):
        with self.assertRaises(Exception) as e:
            parse('32 45', identifier)
        self.assertTrue(str(e.exception).startswith('1:1: expected one of'), f"got: '{e.exception}'")

        self.assertEqual(parse('l33t c0d3r', identifier), 'l33t')
        self.assertEqual(parse('val+34', identifier), 'val')

    def test_parse_block(self):
        r = parse(textwrap.dedent(
                    '''\
                    a
                        b
                    c'''), seq(identifier, block))
        self.assertEqual(r, ['a', ['b']])

        print('--')
        self.assertEqual(parse('\n  if true:\n   a(2)\n   else:\n   a(1)\n', block, debug=True),
                ['if', 'true', [['call', 'a', [2]]], 'else', [['call', 'a', [1]]]])


    def test_if(self):
        self.assertEqual(parse(textwrap.dedent(
                        '''\
                        if true:
                            print(2)
                            print(3)
                        else:
                            return 1
                            return 2
                        blah_blah()
                        '''), if_stmt),
                        ('if', 'true',
                            [['call', 'print', [2]],
                             ['call', 'print', [3]]],
                            [['return', 1],
                             ['return', 2]]))

        self.assertEqual(parse(textwrap.dedent(
                        '''\
                        if true:
                            print(2)
                            if true:
                                print(2)
                            else:
                                return false
                            print(2)
                        else:
                            return 1
                        '''), if_stmt, debug=True),
                        ('if', 'true',
                            [['call', 'print', [2]],
                            ('if', 'true',
                                [['call', 'print', [2]]],
                                [['return', 'false']])],
                            [['return', 1]]))

    def test_block(self):
        b1 = Block('a', 'b', 'c')
        self.assertEqual(b1, ['a', 'b', 'c'])

        b2 = Block('a', Block('b'), 'c')
        self.assertEqual(b2, ['a', 'b', 'c'])

    def test_nodeclass(self):
        A = nodeclass('A', 'a,b,c')
        a = A(1,2,3)
        self.assertEqual(a.a, 1)
        self.assertEqual(a.b, 2)
        self.assertEqual(a.c, 3)
        self.assertEqual(a[0], 1)
        self.assertEqual(a[1], 2)
        self.assertEqual(a[2], 3)

        B = nodeclass('B', 'a _ c', [None, 5])
        b = B(1, 3)
        self.assertEqual(b.a, 1)
        self.assertEqual(b.c, 3)
        self.assertEqual(b[0], 1)
        self.assertEqual(b[1], 5)
        self.assertEqual(b[2], 3)

        C = nodeclass('C', 'a b c *z')
        c = C(1,2,3)
        self.assertEqual(c.a, 1)
        self.assertEqual(c.b, 2)
        self.assertEqual(c.c, 3)
        self.assertEqual(c.z, ())
        self.assertEqual(c[0], 1)
        self.assertEqual(c[1], 2)
        self.assertEqual(c[2], 3)

        D = nodeclass('D', 'a _ c *z', [None, 44])
        d = D(1, 3, 4, 5, 6)
        self.assertEqual(d.a, 1)
        self.assertEqual(d.c, 3)
        self.assertEqual(d.z, (4,5,6))
        self.assertEqual(d[0], 1)
        self.assertEqual(d[1], 44)
        self.assertEqual(d[2], 3)
        self.assertEqual(d[3], 4)
        self.assertEqual(d[4], 5)
        self.assertEqual(d[5], 6)

        E = nodeclass('E', 'a *b')
        e = E(1,2,3)
        self.assertEqual(e.a, 1)
        self.assertEqual(e.b, (2,3))

    def test_normalize(self):
        tree1 = FunctionCall('a', 'b', 'c')
        self.assertEqual(normalize_expr(tree1)[0], ('a', 'b', 'c'))

        tree2 = FunctionCall('a', FunctionCall('b'), 'c')
        self.unify(normalize_stmt(tree2),
                [('=', Var('x'), ('b',)),
                 ('a', Var('x'), 'c')])

        tree3 = FunctionCall('a', FunctionCall('b', FunctionCall('x')), 'c')
        self.unify(normalize_stmt(tree3),
                [('=', Var('x'), ('x',)),
                 ('=', Var('y'), ('b', Var('x'))),
                 ('a', Var('y'), 'c')])

        tree4 = If(FunctionCall('b', FunctionCall('x')),
                   'c',
                   FunctionCall('d'))
        self.unify(normalize_stmt(tree4),
                [('=', Var('x'), ('x',)),
                 ('=', Var('y'), ('b', Var('x'))),
                 ('if', Var('y'), ['c'], [('d',)])])

        tree5 = Return(FunctionCall('a', FunctionCall('b')))
        self.unify(normalize_stmt(tree5),
                [('=', Var('x'), ('b',)),
                 ('=', Var('y'), ('a', Var('x'))),
                 ('return', Var('y'))])

        tree6 = Assign('x', FunctionCall('a', FunctionCall('b')))
        self.unify(normalize_stmt(tree6),
                [('=', Var('x'), ('b',)),
                 ('=', 'x', ('a', Var('x')))])

    def test_pack_modrm(self):
        import compiler
        def bin(x):
            return format(x, '#010b').replace('0b', '')
        self.assertEqual(bin(ord(compiler._pack_modrm(4, 2, '*+disp8'))), '01' '100' '010')
        self.assertEqual(bin(ord(compiler._pack_modrm(1, 1, '*'))),       '00' '001' '001')
        self.assertEqual(bin(ord(compiler._pack_modrm(7, 7, 'direct'))),  '11' '111' '111')

    def nasm_assemble(self, code: bytes):
        import tempfile
        import subprocess
        import binascii

        with tempfile.NamedTemporaryFile(delete=False) as f:
            f.write(b'BITS 64\n')
            f.write(code)

        p = subprocess.Popen(f'nasm -f bin -o {f.name}.out {f.name}'.split(),
                stdout = subprocess.PIPE,
                stderr = subprocess.PIPE)

        stdout, stderr = p.communicate()

        if stderr:
            raise Exception(stdout, stderr)

        with open(f'{f.name}.out', 'rb') as f:
            return f.read()

    def test_emit(self):
        # moves
        self.assertEqual(emit('rax <- rcx'), self.nasm_assemble(b'mov rax, rcx'))
        self.assertEqual(emit('rax <-', 8),  self.nasm_assemble(b'mov rax, 8'))
        self.assertEqual(emit('rax <-', ('rcx', 8)), self.nasm_assemble(b'mov rax, [rcx+8]'))
        self.assertEqual(emit('eax <-', ('ecx', 8)), self.nasm_assemble(b'mov eax, [ecx+8]'))
        self.assertEqual(emit('rax <- rcx'), b'\x48\x89\xc8')
        self.assertEqual(emit('eax <- ecx'), b'\x89\xc8')
        self.assertEqual(emit('rcx <- rax'), b'\x48\x89\xc1')
        self.assertEqual(emit('ecx <- eax'), b'\x89\xc1')

        # cmps
        self.assertEqual(emit('cmp eax ecx'), self.nasm_assemble(b'cmp ecx, eax'))
        self.assertEqual(emit('cmp rax rcx'), self.nasm_assemble(b'cmp rcx, rax'))
        self.assertEqual(emit('cmp eax', 3000), self.nasm_assemble(b'cmp eax, 3000'))
        self.assertEqual(emit('cmp rax', 3000), self.nasm_assemble(b'cmp rax, 3000'))

        # arithmetic
        self.assertEqual(emit('add eax ecx'), self.nasm_assemble(b'add eax, ecx'))

    def test_labels(self):
        r1 = []
        r1.append(emit('j l'))
        r1.append(emit('j l'))
        emit_label('l', r1)
        r1.append(emit('rax <- 1'))
        r1.append(emit('add eax eax'))
        r1.append(emit('j l'))
        self.assertEqual(pass2(r1), self.nasm_assemble(b'''
            jmp l
            jmp l
            l:
            mov eax, 1
            add eax, eax
            jmp l
        '''))

        r2 = []
        r2.append(emit('rax <- 1'))
        r2.append(emit('jne l'))
        r2.append(emit('jne l'))
        emit_label('l', r2)
        r2.append(emit('add eax eax'))
        r2.append(emit('cmp rax 2000'))
        r2.append(emit('jne l'))
        r2.append(emit('jne l'))
        self.assertEqual(pass2(r2), self.nasm_assemble(b'''
            mov eax, 1
            jne l
            jne l
            l:
            add eax, eax
            cmp rax, 2000
            jne l
            jne l
            '''))

    def test_compiler(self):
        t = If(1, 7, 4)
        g = []
        code_gen(normalize_stmt(parse(textwrap.dedent(
        '''\
        if 1:
            7
        else:
            4
        '''), if_stmt)), None, g)

        postlude = emit('rdi <- rax') +\
                   emit('rax <- 60') +\
                   emit('syscall')
        with open('a.bin', 'wb') as f:
            f.write(write_elf(pass2(g) + postlude))
        os.system("chmod +x a.bin")
        r = os.system("./a.bin")
        self.assertEqual(7, r >> 8)

import doctest
def load_tests(loader, tests, ignore):
    tests.addTests(doctest.DocTestSuite('compiler'))
    return tests

if __name__ == '__main__':
    unittest.main()