Mercurial > repos > tabletprog
comparison modules/llcompile.tp @ 310:2308336790d4
WIP compiler module for low-level dialect
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 01 Aug 2014 18:56:39 -0700 |
parents | |
children | f987bb2a1911 |
comparison
equal
deleted
inserted
replaced
309:ed908b7fcec6 | 310:2308336790d4 |
---|---|
1 { | |
2 _compileError <- :_msg _line { | |
3 #{ | |
4 isError? <- { true } | |
5 msg <- { _msg } | |
6 line <- { _line } | |
7 } | |
8 } | |
9 | |
10 _notError <- :vals ifnoterr { | |
11 maybeErr <- vals find: :val { | |
12 (object does: val understand?: "isError?") && val isError? | |
13 } | |
14 maybErr value: :err { | |
15 err | |
16 } none: ifnoterr | |
17 } | |
18 | |
19 _ilFun <- :_name { | |
20 _buff <- #[] | |
21 _nextReg <- 0 | |
22 #{ | |
23 name <- { _name } | |
24 add <- :inst { _buff append: inst } | |
25 getReg <- { | |
26 r <- il reg: _nextReg | |
27 _nextReg <- _nextReg + 1 | |
28 r | |
29 } | |
30 } | |
31 } | |
32 | |
33 _exprHandlers <- dict hash | |
34 _compileExpr:syms:ilfun:dest <- :expr :syms :ilf :dst { | |
35 _exprHandlers ifget: (expr nodeType) :handler { | |
36 handler: expr syms ilf dst | |
37 } else: { | |
38 _compileError: "Expression with node type " . (expr nodeType) . " not implemented yet" | |
39 } | |
40 } | |
41 _opMap <- dict hash | |
42 mapOp <- macro: :op ilfun { | |
43 quote: (opMap set: op :ina inb out size { | |
44 il ilfun: ina inb out size | |
45 }) | |
46 } | |
47 mapOp: "+" add | |
48 mapOp: "-" sub | |
49 mapOp: "*" mul | |
50 mapOp: "/" div | |
51 mapOp: "and" and | |
52 mapOp: "or" or | |
53 mapOp: "xor" xor | |
54 | |
55 _compOps <- dict hash | |
56 _compOps set: "=" :signed? { il eq } | |
57 _compOps set: "!=" :signed? { il ne } | |
58 _compOps set: ">" :signed? { if: signed? { il gr } else: { il ugr } } | |
59 _compOps set: "<" :signed? { if: signed? { il ls } else: { il uls } } | |
60 _compOps set: ">=" :signed? { if: signed? { il ge } else: { il uge } } | |
61 _compOps set: "<=" :signed? { if: signed? { il le } else: { il ule } } | |
62 | |
63 _compileBinary <- :expr syms ilf assignTo { | |
64 _assignSize? <- false | |
65 _asize <- 0 | |
66 dest <- option value: assignTo :asn { | |
67 _assignSize? <- true | |
68 _asize <- asn size | |
69 asn | |
70 } none: { | |
71 ilf getReg | |
72 } | |
73 l <- _compileExpr: (expr left) syms: syms ilfun: ilf assign: (option value: dest) | |
74 r <- _compileExpr: (expr right) syms: syms ilfun: ilf assign: (option none) | |
75 _notError: [(l) (r)] { | |
76 lv <- l val | |
77 ls <- l size | |
78 rv <- r val | |
79 rs <- r size | |
80 _size <- if: ls > rs { ls } else: { rs } | |
81 _signed <- (ls signed?) || (rs signed?) | |
82 _opMap ifget: (expr op) :ingen { | |
83 ilf add: (ingen: lv rv (dest val) _size) | |
84 #{ | |
85 val <- dest | |
86 size <- _size | |
87 signed? <- _signed | |
88 } | |
89 } else: { | |
90 _compOps ifget: (expr op) :cond { | |
91 ilf add: (il bool: cond dest) | |
92 #{ | |
93 val <- dest | |
94 size <- il b | |
95 signed? <- false | |
96 } | |
97 } else: { | |
98 _compileError: "Operator " . (expr op) . " is not supported yet\n" 0 | |
99 } | |
100 } | |
101 } | |
102 } | |
103 _compileString <- :expr syms ilf assignTo { | |
104 | |
105 } | |
106 _compileInt <- :expr syms ilf assignTo { | |
107 expr | |
108 } | |
109 _compileSym <- :expr syms ilf assignTo { | |
110 syms ifDefined: (expr name) :def { | |
111 def | |
112 } else: { | |
113 _compileError: "Symbol " . (expr name) . " is not defined in " . (ilf name) | |
114 } | |
115 } | |
116 | |
117 _exprHandlers set: binary _compileBinary | |
118 _exprHandlers set: stringlit _compileString | |
119 #{ | |
120 import: [ | |
121 binary | |
122 stringlit | |
123 intlit | |
124 sym | |
125 call | |
126 obj | |
127 sequence | |
128 assignment | |
129 lambda | |
130 ] from: ast | |
131 llFun <- :{ | |
132 | |
133 } | |
134 } | |
135 } |