annotate ilbackend.js @ 251:2557ce4e671f

Fix a couple of compiler bugs. topenv was getting initialized in multiple places. This resulted in multiple copies of modules getting created which caused problems for macro expansion. Additionally, arguments were not being marked as declared during code generation so assigning to an argument that was not closed over generated invalid C code.
author Michael Pavone <pavone@retrodev.com>
date Fri, 11 Apr 2014 22:29:32 -0700
parents 6fe9343b1400
children d1dc2d70bdfd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
205
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1 var mainModule;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
2 var modules = {};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
3
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
4 var methodIds = {};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
5 var methodNames = [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6 var assignNames;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
8 function register(num)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
9 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
10 this.num = num;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
11 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
12
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
13 register.prototype = {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
14 valueOf: function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
15 return 'r' + this.num;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
16 },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
17 get isRegister() { return true; },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
18 get isArgument() { return false; }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
19 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
20
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
21 function argument(num)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
22 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
23 this.num = num;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
24 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
25
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
26 argument.prototype = {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 valueOf: function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 return 'a' + this.num;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
29 },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
30 get isRegister() { return true; },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
31 get isArgument() { return true; }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
32 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
33
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
34 function retreg(size)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
35 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
36 this.size = size;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
37 this.reg = NULL;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
38 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
39
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
40 retreg.prototype = {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
41 valueOf: function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
42 if (!this.reg) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
43 return 'retr';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
44 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
45 return this.reg.valueOf();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
46 },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47 clobber: function(code) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
48 this.reg = code.getReg();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 code.addInst('mov', 'retr', this.reg, this.size);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 get isRegister() { return true; },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 get isArgument() { return false; }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
55 function indexed(base, offset, index, size)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 this.base = base;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58 this.offset = offset;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
59 this.index = index;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
60 this.size = size;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
61 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
62
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
63 indexed.prototype = {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
64 valueOf: function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
65 return this.offset + '[' + this.base + ' ' + this.index + '*' + this.size + ']';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
66 },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
67 get isRegister() { return false; },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
68 get isArgument() { return false; }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
69 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
70
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
71 function offset(base, offset)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
72 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
73 this.base = base;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 this.offset = offset;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
75 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
76
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
77 offset.prototype = {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
78 valueOf: function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
79 var out = '';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
80 if (this.offset) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
81 out += this.offset;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
82 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
83 return out + '[' + this.base + ']';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
84 },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
85 get isRegister() { return false; },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
86 get isArgument() { return false; }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
87 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
88
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
89 function funCode(name)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
90 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
91 this.name = name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
92 this.instructions = [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
93 this.nextReg = 0;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
94 this.toclobber = [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
95 this.envreg = null;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
96 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
97
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
98 funCode.prototype = {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
99 addInst: function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
100 var inst = '';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
101 for (var i = 0; i < arguments.length; i++) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
102 if (arguments[0] == 'call') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
103 for (var i = 0; i < this.toclobber.length; i++) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
104 this.toclobber[i].clobber();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
105 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
106 this.toclobber = [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
107 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
108 if (inst) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
109 inst += ' ';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
110 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
111 inst += arguments[i];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
112 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
113 this.instructions.push(inst);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
114 },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
115 getReg: function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
116 return new register(this.nextReg++);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
117 },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
118 getRetReg: function(size) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
119 var reg = new retreg(size);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
120 this.toclobber.push(reg);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
121 return reg;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
122 },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
123 getEnvReg: function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
124 if (!this.envreg) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
125 this.envreg = this.getReg();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
126 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
127 return this.envreg;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
128 },
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
129 valueOf: function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
130 return this.name + ':\n\t' + this.instructions.join('\n\t') + '\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
131 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
132 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
133 function getMethodId(methodName)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
134 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
135 if (!(methodName in methodIds)) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
136 methodIds[methodName] = methodNames.length;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
137 methodNames.push(methodName);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
138 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
139 return methodIds[methodName];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
140 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
141
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
142 function getOpMethodName(opname)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
143 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
144 var optoMeth = {'+': 'ADD_', '-': 'SUB_', '*': 'MUL_', '/': 'DIV_', '%': 'MOD_',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
145 '=': 'EQ_', '!=': 'NEQ_', '<': 'LT_', '>': 'GT_', '>=': 'GEQ_', '<=': 'LEQ_',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
146 '.': 'CAT_', '&&':'if', '||':'ifnot', '|': 'CONS_'};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
147 if (opname in optoMeth) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
148 return optoMeth[opname];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
149 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
150 return opname;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
151 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
152 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
153
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
154 var slot_arr_offset = 8;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
155 function getMethodSlot(code, base, methodid)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
156 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
157 var reg;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
158 if (!base.isRegister()) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
159 reg = code.getReg();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
160 code.addInst('mov', base, reg, 'q');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
161 base = reg;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
162 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
163 reg = code.getReg();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
164 code.addInst('mov', new offset(base, 0), reg, 'q');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
165 return new offset(reg, slot_arr_offset + (methodid & 0xF)*8);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
166 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
167
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
168 op.prototype.toIL = function(code, isReceiver) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
169 var method = getOpMethodName(this.op);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
170 var left = this.left.toIL(code);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
171 var right = this.right.toIL(code);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
172 var methId = getMethodId(method);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
173 if (this.op == '|') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
174 code.addInst('call', getMethodSlot(code, right, methId), methId, right, left);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
175 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
176 code.addInst('call', getMethodSlot(code, left, methId), methId, left, right);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
177 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
178 return code.getRetReg();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
179 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
180 op.prototype.toILLLExpr = function(vars) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
181 var opmap = {'=': '==', 'xor': '^', 'or': '|', 'and': '&'};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
182 return this.left.toILLLExpr(vars) + (this.op in opmap ? opmap[this.op] : this.op) + this.right.toILLLExpr(vars);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
183 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
184 op.prototype.toILLines = function(vars, needsreturn) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
185 return [ (needsreturn ? 'return (object *)' : '' ) + this.toILLLExpr(vars) + ';'];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
186 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
187
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
188
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
189 function escapeCName(name)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
190 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
191 if (name == 'self') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
192 return name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
193 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
194 name = name.replace(/_/g, "UN_").replace(/:/g, "CN_").replace(/!/g, "EX_").replace(/\?/g, 'QS_').replace(/@/g, 'AT_');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
195 name = 'tp_' + name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
196 return name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
197 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
198
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
199 function getSymbolPrefix(info, symbols)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
200 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
201 var pre = '';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
202 switch(info.type) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
203 case 'self':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
204
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
205 pre = (new symbol('self', symbols)).toIL() + '->';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
206 break;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
207 case 'parent':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
208 pre = (new symbol('self', symbols)).toIL() + '->header.';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
209 for (var i = 0; i < info.depth; ++i) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
210 pre += (i ? '->' : '') + 'parent';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
211 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
212 pre = '((' + info.selftype + ' *)' + pre + ')->';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
213 break;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
214 case 'upvar':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
215 pre = 'env->';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
216 for (var i = info.startdepth; i < info.depth; ++i) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
217 pre += 'parent->';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
218 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
219 break;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
220 case 'recupvar':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
221 if (info.subtype == 'object') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
222 pre = 'self->env->';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
223 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
224 //TODO: fill this case in if necessary
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
225 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
226 pre += getSymbolPrefix(info.parent);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
227 case 'closedover':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
228 pre = 'myenv->';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
229 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
230 return pre;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
231 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
232
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
233 symbol.prototype.toIL = function(code) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
234 var name = this.cleanName();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
235 var info = this.symbols.find(name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
236 if (!info) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
237 throw new Error('symbol ' + name + ' not found in ' + assignNames.join('<-'));
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
238 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
239 switch (info.type)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
240 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
241 case 'toplevel':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
242 return toplevel.moduleVar(name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
243 case 'self':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
244 if (info.def instanceof lambda) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
245 var self = (new symbol('self', this.symbols)).toIL(code);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
246 var methId = getMethodId(name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
247 code.addInst('call', getMethodSlot(code, self, methId), methId, self);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
248 return code.getRetReg();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
249 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
250 throw new Error('implement me');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
251 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
252 case 'parent':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
253 if (info.def instanceof lambda) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
254 throw new Error('implement me');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
255 var obj = (new symbol('self', this.symbols)).toIL() + '->header.';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
256 for (var i = 0; i < info.depth; ++i) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
257 obj += (i ? '->' : '') + 'parent';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
258 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
259 return 'mcall(' + getMethodId(name) + '/* ' + name + ' */, 1, ' + obj + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
260 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
261 throw new Error('implement me');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
262 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
263 case 'closedover':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
264 var env = code.getEnvReg();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
265 return new offset(env, info.offset)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
266 default:
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
267 throw new Error('implement ' + info.type);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
268 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
269 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
270 symbol.prototype.toILTypeName = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
271 return this.cleanName();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
272 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
273 symbol.prototype.toILLLExpr = function(vars) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
274 var name = this.cleanName();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
275 if (name in vars) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
276 return name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
277 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
278 if (name == 'self') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
279 return 'self';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
280 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
281 var info = this.symbols.find(name, false, true);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
282 var symbols = this.symbols;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
283 while (info && info.type == 'local') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
284 symbols = symbols.parent;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
285 info = symbols.find(name, false, true);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
286 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
287 if (!info) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
288 return name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
289 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
290 if (info.type == 'toplevel') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
291 return toplevel.moduleVar(name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
292 } else if (info.type == 'self') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
293 if (info.isll || !(info.def instanceof lambda)) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
294 return 'self->' + name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
295 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
296 return 'mcall(' + getMethodId(name) + '/* ' + name + ' */, 1, self)';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
297 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
298 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
299 throw new Error('Unsupported reference type ' + info.type + ' for variable ' + name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
300 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
301 symbol.prototype.toILLines = function(vars, needsreturn) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
302 return [ (needsreturn ? 'return (object *)' : '' ) + this.toILLLExpr(vars) + ';' ];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
303 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
304
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
305 var declaredInts = {};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
306
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
307 intlit.prototype.toIL = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
308 var intType = (this.unsigned ? 'u' : '') + 'int' + this.bits;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
309 var str = intType + '_' + (this.val < 0 ? 'neg_' + (0-this.val).toString() : this.val.toString());
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
310 if (!(str in declaredInts)) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
311 toplevelcode += 'obj_' + intType + ' ' + str + ' = {{&obj_' + intType + '_meta, NULL}, ' + this.val.toString() + '};\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
312 declaredInts[str] = true;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
313 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
314 return '((object *)&' + str + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
315 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
316 intlit.prototype.toILLLExpr = function(vars) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
317 return this.val.toString();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
318 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
319 intlit.prototype.toILLines = function(vars, needsreturn) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
320 return [ (needsreturn ? 'return (object *)' : '' ) + this.toILLLExpr(vars) + ';' ];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
321 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
322
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
323 floatlit.prototype.toIL = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
324 return 'make_float(' + this.val.toString() + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
325 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
326 floatlit.prototype.toILLLExpr = function(vars) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
327 return this.val.toString();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
328 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
329 floatlit.prototype.toILLines = function(vars, needsreturn) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
330 return [ (needsreturn ? 'return (object *)' : '' ) + this.toILLLExpr(vars) + ';' ];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
331 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
332
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
333 var declaredStrings = {};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
334 var nextStringId = 0;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
335
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
336 strlit.prototype.toIL = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
337 if (!(this.val in declaredStrings)) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
338 //TODO: get the proper byte length
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
339 toplevelcode += 'string str_' + nextStringId + ' = {{&string_meta, NULL}, ' + this.val.length + ', ' + this.val.length + ', "' + this.val.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r') + '"};\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
340 declaredStrings[this.val] = nextStringId++;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
341 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
342 return '((object *)&str_' + declaredStrings[this.val] + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
343 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
344 strlit.prototype.toILLLExpr = function(vars) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
345 return '"' + this.val.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r') + '"';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
346 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
347 strlit.prototype.toILLines = function(vars, needsreturn) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
348 return [ (needsreturn ? 'return (object *)' : '' ) + this.toILLLExpr(vars) +';' ];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
349 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
350
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
351 listlit.prototype.toIL = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
352 var ret = 'make_list(' + this.val.length;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
353 for (var i = this.val.length-1; i >= 0; i--) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
354 ret += ', ' + this.val[i].toIL();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
355 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
356 return ret + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
357 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
358
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
359 arraylit.prototype.toIL = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
360 var ret = 'make_array(' + this.val.length;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
361 for (var i = 0; i < this.val.length; i++) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
362 ret += ', ' + this.val[i].toIL();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
363 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
364 return ret + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
365 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
366
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
367 funcall.prototype.toIL = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
368 var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
369 if (name == 'foreign') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
370 if ((this.args[0] instanceof lambda) || (this.args[0] instanceof object)) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
371 return null;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
372 } else if(this.args[0] instanceof symbol) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
373 return this.args[0].name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
374 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
375 throw new Error("Unexpected AST type for foreign:");
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
376 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
377 } else if(name == 'llProperty:withType' || name == 'llProperty:withVars:andCode') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
378 return null;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
379 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
380 var args = this.args.slice(0, this.args.length);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
381 if (this.receiver) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
382 args.splice(0, 0, this.receiver);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
383 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
384 var method = false;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
385 var funinfo = this.symbols.find(name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
386 var start = 0;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
387 if (!funinfo || funinfo.def instanceof setter || funinfo.type == 'toplevel') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
388 method = true;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
389 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
390 switch(funinfo.type)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
391 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
392 case 'self':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
393 case 'parent':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
394 var defargs = funinfo.def.args.length;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
395 if (!defargs || funinfo.def.args[0].name != 'self') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
396 defargs ++
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
397 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
398 if (args.length < defargs) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
399 if (funinfo.type == 'self') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
400 args.splice(0, 0, new symbol('self', this.symbols));
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
401 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
402 var obj = (new symbol('self', this.symbols)).toIL() + '->header.';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
403 for (var i = 0; i < funinfo.depth; ++i) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
404 obj += (i ? '->' : '') + 'parent';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
405 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
406 args.splice(0, 0, ', ' + obj);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
407 start = 1;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
408 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
409 } else if(!(args[0] instanceof symbol) || args[0].name != 'self') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
410 funinfo = null;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
411 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
412 method = true;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
413 break;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
414 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
415 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
416 for (var i = start; i < args.length; i++) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
417 args[i] = ', ' + (!i && method ? '(object *)(' : '') + args[i].toIL() + (!i && method ? ')' : '');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
418 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
419 var callpart;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
420 if (method) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
421 if (funinfo && funinfo.type == 'self' && funinfo.def.name) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
422 callpart = funinfo.def.name + '(' + (funinfo.def.symbols.parent.needsenv ? (new symbol('self', this.symbols)).toIL() + '->env' : 'NULL' );
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
423 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
424 callpart = 'mcall(' + getMethodId(name) + '/* ' + name + ' */';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
425 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
426 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
427 var closVar = (new symbol(name, this.symbols)).toIL();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
428 callpart = '((lambda *)' + closVar + ')->func(((lambda *)' + closVar + ')->env';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
429 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
430 return callpart + ', ' + args.length + args.join('') + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
431 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
432 funcall.prototype.toILTypeName = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
433 switch(this.name)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
434 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
435 case 'ptr:':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
436 case 'ptr':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
437 var receiver = this.receiver ? this.receiver : this.args[0];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
438 return receiver.toILTypeName() + ' *';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
439 break;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
440 case 'struct:':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
441 case 'struct':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
442 var receiver = this.receiver ? this.receiver : this.args[0];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
443 return 'struct ' + receiver.toILTypeName();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
444 break;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
445 case 'funptr:':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
446 case 'funptr':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
447 var rettype;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
448 var firstArg;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
449 if (this.receiver) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
450 rettype = this.receiver;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
451 firstArg = 0;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
452 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
453 rettype = this.args[0];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
454 firstArg = 1;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
455 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
456 var argtypes = '';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
457 for (var i = firstArg; i < this.args.length; i++) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
458 if (argtypes) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
459 argtypes += ', '
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
460 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
461 argtypes += this.args[i].toILTypeName();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
462 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
463 return [rettype.toILTypeName() + '(*', ')(' + argtypes + ')'];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
464 break;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
465 default:
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
466 throw new Error('invalid use of funcall expression where a C type name is expected');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
467 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
468 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
469 funcall.prototype.toILLines = function(vars, needsreturn) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
470 var lines = [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
471 var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
472 var args = this.args.slice(0, this.args.length);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
473 if (this.receiver) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
474 args.splice(0, 0, [this.receiver]);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
475 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
476 switch(name)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
477 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
478 case 'if':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
479 lines.push('if (' + this.args[0].toILLLExpr(vars) + ') {');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
480 var blines = this.args[1].toILLines(vars, needsreturn);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
481 for (var i in blines) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
482 lines.push('\t' + blines[i]);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
483 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
484 if (needsreturn) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
485 lines.push('} else {');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
486 lines.push('\t return module_false;');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
487 lines.push('}');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
488 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
489 lines.push('}');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
490 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
491 break;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
492 case 'if:else':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
493 lines.push('if (' + this.args[0].toILLLExpr(vars) + ') {');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
494 var blines = this.args[1].toILLines(vars, needsreturn);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
495 for (var i in blines) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
496 lines.push('\t' + blines[i]);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
497 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
498 lines.push('} else {');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
499 blines = this.args[2].toILLines(vars, needsreturn);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
500 for (var i in blines) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
501 lines.push('\t' + blines[i]);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
502 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
503 lines.push('}');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
504 break;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
505 case 'while:do':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
506 if (needsreturn) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
507 throw new Error("while:do can't be last statement in llMessage code block");
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
508 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
509 lines.push('while (' + this.args[0].toILLLExpr(vars) + ') {');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
510 var blines = this.args[1].toILLines(vars);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
511 for (var i in blines) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
512 lines.push('\t' + blines[i]);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
513 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
514 lines.push('}');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
515 break;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
516 default:
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
517 lines.push( (needsreturn ? 'return (object *)' : '') + this.toILLLExpr(vars) + ';');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
518 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
519 return lines;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
520 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
521
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
522 funcall.prototype.toILLLExpr = function(vars) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
523 var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
524 var args = this.args.slice(0, this.args.length);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
525 if (this.receiver) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
526 if(this.args.length == 0) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
527 if (this.receiver instanceof symbol && this.receiver.name in vars && vars[this.receiver.name].substr(-1) != "*") {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
528 return this.receiver.toILLLExpr(vars) + '.' + this.name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
529 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
530 return this.receiver.toILLLExpr(vars) + '->' + this.name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
531 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
532 } else if (this.args.length == 1 && name[name.length-1] == '!') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
533 if (this.receiver instanceof symbol && this.receiver.name in vars && vars[this.receiver.name].substr(-1) != "*") {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
534 return this.receiver.toILLLExpr(vars) + '.' + this.name.substr(0, name.length-1) + ' = ' + args[0].toILLLExpr(vars);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
535 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
536 return this.receiver.toILLLExpr(vars) + '->' + this.name.substr(0, name.length-1) + ' = ' + args[0].toILLLExpr(vars);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
537 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
538 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
539 args.splice(0, 0, this.receiver);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
540 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
541 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
542 switch(name)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
543 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
544 case 'if':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
545 return '((' + args[0].toILLLExpr(vars) + ') ? (' + args[1].toILLLExpr(vars) + ') : 0)';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
546 case 'if:else':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
547 return '((' + args[0].toILLLExpr(vars) + ') ? (' + args[1].toILLLExpr(vars) + ') : (' + args[2].toILLLExpr(vars) + '))';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
548 case 'while:do':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
549 throw new Error('while:do not allowed in expression context in llMessage block');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
550 case 'addr_of':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
551 return '&(' + args[0].toILLLExpr(vars) + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
552 case 'sizeof':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
553 return 'sizeof(' + args[0].toILTypeName() + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
554 case 'get':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
555 return args[0].toILLLExpr(vars) + '[' + args[1].toILLLExpr(vars) + ']';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
556 case 'set':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
557 return args[0].toILLLExpr(vars) + '[' + args[1].toILLLExpr(vars) + '] = ' + args[2].toILLLExpr(vars);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
558 case 'not':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
559 return '!(' + args[0].toILLLExpr(vars) + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
560 case 'castTo':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
561 return '((' + args[1].toILTypeName() + ')(' + args[0].toILLLExpr(vars) + '))';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
562 case 'mcall':
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
563 if (args[0] instanceof symbol) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
564 args[0] = new intlit(getMethodId(args[0].name));
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
565 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
566 default:
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
567 for (var i in args) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
568 args[i] = args[i].toILLLExpr(vars);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
569 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
570 return name + '(' + args.join(', ') + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
571 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
572 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
573
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
574 function cObject(name) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
575 this.name = name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
576 this.slots = {};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
577 this.properties = [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
578 this.values = [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
579 this.slotvars = {};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
580 this.includes = {};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
581 this.parent = 'NULL';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
582 this.init = [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
583 this.initmsgadded = false;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
584 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
585
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
586 cObject.prototype.addInclude = function(includefile) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
587 this.includes[includefile] = true;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
588 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
589
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
590 cObject.prototype.addMessage = function(msgname, implementation) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
591 var methodid = getMethodId(msgname);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
592 var trunc = methodid & 0xF;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
593 if (!(trunc in this.slots)) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
594 this.slots[trunc] = [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
595 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
596 this.slots[trunc].push([methodid, '\t\t' + implementation.lines.join('\n\t\t') + '\n', msgname]);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
597 if (!(trunc in this.slotvars)) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
598 this.slotvars[trunc] = {};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
599 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
600 for (var varname in implementation.vars) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
601 this.slotvars[trunc][varname] = implementation.vars[varname];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
602 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
603 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
604
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
605 cObject.prototype.addProperty = function(propname, value, type) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
606 if (type != undefined) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
607 this.properties.push([propname, type]);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
608 if (value !== null) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
609 this.values.push(value);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
610 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
611 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
612 var escaped = escapeCName(propname);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
613 this.addMessage(propname, {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
614 vars: {},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
615 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
616 'return self->' + escaped + ';'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
617 ]});
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
618 this.addMessage(propname + '!', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
619 vars: {},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
620 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
621 'self->' + escaped + ' = va_arg(args, object *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
622 'return (object *)self;'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
623 ]});
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
624 this.properties.push(escaped);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
625 this.values.push(value);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
626 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
627 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
628
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
629 cObject.prototype.addInit = function(statement) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
630 this.init.push(statement);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
631 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
632
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
633 cObject.prototype.addImport = function(symbols, source) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
634 this.imported.push(source);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
635 var importNum = imported.length - 1;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
636 if (symbols) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
637 each(symbols, function(i, sym) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
638 this.addMessage(sym.name, {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
639 vars: {},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
640 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
641 'return self->import_' + importNum + '->meta->meth_lookup[method_id & 0xF](method_id, num_args, self->import_' + importNum + ', args);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
642 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
643 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
644 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
645 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
646 //TODO: handle proxying for unimplemented methods
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
647 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
648 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
649
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
650 cObject.prototype.checkInitMsg = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
651 if (!this.initmsgadded && this.init.length) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
652 var init = this.init.slice(0, this.init.length);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
653 init.push('return (object *)self;');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
654 this.addMessage('_init', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
655 vars: {},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
656 lines: init
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
657 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
658 this.initmsgadded = true;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
659 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
660 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
661
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
662 cObject.prototype.populateSymbols = function() {};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
663
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
664 cObject.prototype.toEarlyCDef = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
665 this.checkInitMsg();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
666 var includes = '';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
667 for (var file in this.includes) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
668 includes += '#include ' + file + '\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
669 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
670 var objdef = 'typedef struct ' + this.name + ' {\n\tobject header;\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
671 for (var i in this.properties) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
672 if (this.properties[i] instanceof Array) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
673 if (this.properties[i][1] instanceof Array) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
674 objdef += '\t' + this.properties[i][1][0] + this.properties[i][0] + this.properties[i][1][1] + ';\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
675 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
676 objdef += '\t' + this.properties[i][1] + ' ' + this.properties[i][0] + ';\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
677 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
678 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
679 objdef += '\tobject * ' + this.properties[i] + ';\n'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
680 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
681 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
682 objdef += '} ' + this.name + ';\nobj_meta ' + this.name + '_meta;\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
683 return includes + objdef;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
684 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
685
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
686 cObject.prototype.toILDef = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
687 this.checkInitMsg();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
688 var slotdefs = '';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
689 var metadef = 'obj_meta ' + this.name + '_meta = {sizeof(' + this.name +'), {';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
690 for (var i = 0; i < 16; i++) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
691 if (i) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
692 metadef += ', ';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
693 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
694 if (i in this.slots) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
695 slotdefs += 'object * ' + this.name + '_slot_' + i + '(uint32_t method_id, uint32_t num_params, object * oself, va_list args) {\n\t' +
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
696 this.name + ' *self = (' + this.name + ' *)oself;\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
697 for (var varname in this.slotvars[i]) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
698 if (this.slotvars[i][varname] instanceof Array) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
699 slotdefs += '/*foo*/\t' + this.slotvars[i][varname][0] + ' ' + varname + this.slotvars[i][varname][1] + ';\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
700 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
701 slotdefs += '/*bar*/\t' + this.slotvars[i][varname] + ' ' + varname + ';\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
702 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
703 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
704 if (this.slots[i].length == 1) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
705 slotdefs += '\tif (method_id == ' + this.slots[i][0][0] + ') { /* ' + this.slots[i][0][2] + '*/\n' +
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
706 '\t\t' + this.slots[i][0][1] + '\n' +
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
707 '\t}\n' +
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
708 '\treturn no_impl(method_id, num_params, (object *)self, args);\n}\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
709 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
710 slotdefs += '\tswitch(method_id) {\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
711 for (j in this.slots[i]) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
712 slotdefs += '\t\tcase ' + this.slots[i][j][0] + ': /* ' + this.slots[i][j][2] + '*/\n' +
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
713 '\t\t\t' + this.slots[i][j][1] + '\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
714 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
715 slotdefs += '\t\tdefault:\n' +
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
716 '\t\t\treturn no_impl(method_id, num_params, (object *)self, args);\n\t}\n}\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
717
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
718 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
719 metadef += this.name + '_slot_' + i;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
720 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
721 metadef += 'no_impl';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
722 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
723 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
724 metadef += '}};\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
725 return slotdefs + metadef;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
726 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
727
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
728 cObject.prototype.toILInstance = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
729 this.checkInitMsg();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
730 var ret = 'make_object(&' + this.name + '_meta, ' + this.parent + ', ' + this.values.length + (this.values.length ? ', ' : '') + this.values.join(', ') + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
731 if (this.initmsgadded) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
732 ret = 'mcall(' + getMethodId('_init') + ', 1, ' + ret + ')'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
733 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
734 return ret;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
735 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
736
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
737 cObject.prototype.toIL = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
738 forwarddec += this.toEarlyCDef();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
739 toplevelcode += this.toILDef();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
740 return this.toILInstance();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
741 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
742
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
743 var nextobject = 0;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
744
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
745
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
746 object.prototype.toILObject = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
747 var messages = this.messages;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
748 if (!this.name) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
749 this.name = 'object_' + nextobject++;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
750 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
751 var me = new cObject(this.name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
752 this.symbols.typename = me.name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
753 if (this.symbols.needsenv) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
754 me.addProperty('env', this.symbols.envVar(), 'struct ' + this.symbols.getEnvType() + ' * ');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
755 me.hasenv = true;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
756 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
757 if (this.symbols.needsparent && !(this.symbols.parent instanceof osymbols)) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
758 me.parent = '(object *)(' + (new symbol('self', this.symbols.parent)).toIL() + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
759 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
760 for (var i in messages) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
761 if (messages[i] instanceof funcall) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
762 if (messages[i].name == 'import:' && messages[i].args.length == 1) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
763 me.addImport(false, messages[i].args[0]);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
764 } else if(messages[i].name == 'import:from:' && messages[i].args.length == 2) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
765 var importsyms = [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
766 each(messages[i].args[0].val, function(i, el) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
767 if (!(el instanceof symbol)) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
768 throw new Error('Names in import:from statement must be symbols');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
769 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
770 importsyms.push(el);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
771 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
772 me.addImport(importsyms, messages[i].args[1]);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
773 } else if(messages[i].name == 'llProperty:withType:' && messages[i].args.length == 2) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
774 me.addProperty(messages[i].args[0].name, null, messages[i].args[1].toILTypeName());
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
775 } else if(messages[i].name == 'llMessage:withVars:andCode:' && messages[i].args.length == 3) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
776 var msgname = messages[i].args[0].name
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
777 var rawvars = messages[i].args[1].expressions;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
778 var vars = {};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
779 for(var v in rawvars) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
780 vars[rawvars[v].symbol.name] = rawvars[v].expression.toILTypeName();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
781 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
782 me.addMessage(msgname, {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
783 vars: vars,
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
784 lines: messages[i].args[2].toILLines(vars, true)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
785 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
786 } else if(messages[i].name == 'includeSystemHeader:' && messages[i].args.length == 1) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
787 me.addInclude("<" + messages[i].args[0].val + ">");
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
788 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
789
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
790 throw new Error('Only import and import:from calls allowed in object context. ' + messages[i].name + 'with ' + messages[i].args.length + ' arguments found instead.');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
791 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
792 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
793 messages[i].toILObject(me);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
794 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
795 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
796
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
797 return me;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
798 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
799
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
800 object.prototype.toIL = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
801 return this.toILObject().toIL();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
802 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
803
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
804 var toplevelcode;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
805 var forwarddec;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
806
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
807 function addBinaryOp(cobject, opname, cop, objtype)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
808 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
809 cobject.addMessage(opname, {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
810 vars: {ret: objtype + ' *', argb: objtype +' *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
811 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
812 'argb = va_arg(args, ' + objtype + ' *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
813 'ret = (' + objtype + ' *)make_object(&' + objtype + '_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
814 'ret->num = self->num ' + cop + ' argb->num;',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
815 'return &(ret->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
816 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
817 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
818 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
819
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
820 function addCompOp(cobject, opname, cop, objtype)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
821 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
822 cobject.addMessage(opname, {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
823 vars: {argb: objtype + ' *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
824 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
825 'argb = va_arg(args, ' + objtype + ' *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
826 'if (self->num ' + cop + ' argb->num) {',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
827 ' return ' + toplevel.moduleVar('true') + ';',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
828 '}',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
829 'return ' + toplevel.moduleVar('false') + ';',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
830 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
831 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
832 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
833
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
834 function makeInt(bits, unsigned)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
835 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
836 var typename = 'obj_' + (unsigned ? 'u' : '') + 'int' + bits;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
837 var intObj = new cObject(typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
838 intObj.addProperty('num', null, (unsigned ? 'u' : '') + 'int' + bits +'_t');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
839 addBinaryOp(intObj, 'ADD_', '+', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
840 addBinaryOp(intObj, 'SUB_', '-', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
841 addBinaryOp(intObj, 'MUL_', '*', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
842 addBinaryOp(intObj, 'DIV_', '/', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
843 addBinaryOp(intObj, 'MOD_', '%', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
844 addBinaryOp(intObj, 'or', '|', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
845 addBinaryOp(intObj, 'xor', '^', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
846 addBinaryOp(intObj, 'and', '&', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
847 addBinaryOp(intObj, 'lshift:by', '<<', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
848 addBinaryOp(intObj, 'rshift:by', '>>', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
849 addCompOp(intObj, 'LT_', '<', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
850 addCompOp(intObj, 'GT_', '>', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
851 addCompOp(intObj, 'EQ_', '==', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
852 addCompOp(intObj, 'NEQ_', '!=', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
853 addCompOp(intObj, 'GEQ_', '>=', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
854 addCompOp(intObj, 'LEQ_', '<=', typename);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
855 intObj.addInclude('<string.h>');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
856 //-9223372036854775808
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
857 //01234567890123456789
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
858 intObj.addMessage('string', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
859 vars: {str: 'string *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
860 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
861 'str = (string *)make_object(&string_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
862 'str->data = GC_MALLOC(' + (bits == 64 ? 21 : 12) + ');',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
863 'sprintf(str->data, "%' + (bits == 64 ? 'l' : '') + (unsigned ? 'u' : 'd') + '", self->num);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
864 'str->len = str->bytes = strlen(str->data);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
865 'return &(str->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
866 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
867 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
868 //7FFFFFFFFFFFFFFF
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
869 //01234567890123456789
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
870 intObj.addMessage('hex', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
871 vars: {str: 'string *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
872 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
873 'str = (string *)make_object(&string_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
874 'str->data = GC_MALLOC(' + (bits == 64 ? 17 : 9) + ');',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
875 'sprintf(str->data, "%' + (bits == 64 ? 'l' : '') +'X", self->num);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
876 'str->len = str->bytes = strlen(str->data);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
877 'return &(str->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
878 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
879 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
880 intObj.addMessage('isInteger?', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
881 vars: {},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
882 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
883 'return ' + toplevel.moduleVar('true') + ';'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
884 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
885 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
886 intObj.addMessage('hash', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
887 vars: {},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
888 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
889 'return &(self->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
890 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
891 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
892 intObj.addMessage('signed?', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
893 vars: {},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
894 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
895 'return ' + toplevel.moduleVar(unsigned ? 'false' : 'true') + ';'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
896 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
897 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
898 var sizes = [8, 16, 32, 64];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
899 var destunsigned = [false, true];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
900 for (var i = 0; i < sizes.length; i++) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
901 size = sizes[i];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
902 for (var j = 0; j < destunsigned.length; j++) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
903 uns = destunsigned[j];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
904 if (uns == unsigned && size == bits) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
905 intObj.addMessage((uns ? 'u' : '') + 'int' + size, {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
906 vars: {},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
907 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
908 'return &(self->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
909 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
910 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
911 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
912 var retType = 'obj_' + (uns ? 'u' : '') + 'int' + size;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
913 var retName = 'ret' + (uns ? 'u' : '') + size;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
914 var vars = {};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
915 vars[retName] = retType + ' *';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
916 intObj.addMessage((uns ? 'u' : '') + 'int' + size, {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
917
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
918 vars: vars,
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
919 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
920 retName + ' = ('+retType+' *)make_object(&' + retType +'_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
921 retName + '->num = self->num;',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
922 'return &(' + retName + '->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
923 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
924 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
925 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
926 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
927 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
928
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
929 return intObj;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
930 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
931
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
932 function makeArray()
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
933 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
934 var arrayfile = toplevel.names['array'];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
935 var ast = parseFile(arrayfile.path + '/' + arrayfile.file);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
936 ast.name = 'array';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
937 ast.populateSymbols(toplevel);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
938 return ast.toILObject();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
939 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
940
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
941 function makeString()
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
942 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
943 var arrayfile = toplevel.names['string'];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
944 var ast = parseFile(arrayfile.path + '/' + arrayfile.file);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
945 ast.name = 'string';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
946 ast.populateSymbols(toplevel);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
947 return ast.toILObject();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
948 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
949
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
950 function makelambda()
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
951 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
952 var clos = new cObject('lambda');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
953 clos.addProperty('env', null, 'void *');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
954 clos.addProperty('func', null, 'closure_func');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
955 clos.addMessage('while:do', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
956 vars: {action: 'lambda *', ret: 'object *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
957 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
958 'action = va_arg(args, lambda *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
959 'ret = ' + toplevel.moduleVar('true') + ';',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
960 'while(' + toplevel.moduleVar('true') + ' == ccall(self, 0)) {',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
961 ' ccall(action, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
962 '}',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
963 'return ret;'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
964 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
965 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
966 return clos;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
967 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
968
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
969 function builtinTypes()
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
970 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
971 return [makeInt(64, false), makeInt(32, false), makeInt(16, false), makeInt(8, false),
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
972 makeInt(64, true) , makeInt(32, true), makeInt(16, true), makeInt(8, true),
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
973 makeArray(), makeString(), makelambda()];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
974 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
975
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
976 function addBuiltinModules(toplevel)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
977 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
978 var os = new cObject('mod_obj_os');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
979 os.addInclude('<sys/stat.h>');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
980 os.addInclude('<fcntl.h>');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
981 os.addInclude('<stdlib.h>');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
982 os.addInclude('<time.h>');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
983 os.addInclude('<unistd.h>');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
984 os.addMessage('write', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
985 vars: {str: 'string *', intret: 'obj_int32 *', filedes: 'obj_int32 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
986 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
987 'filedes = va_arg(args, obj_int32 *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
988 'str = va_arg(args, string *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
989 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
990 'intret->num = write(filedes->num, str->data, str->bytes);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
991 'return &(intret->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
992 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
993 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
994 os.addMessage('read', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
995 vars: {str: 'string *', size: 'obj_int32 *', filedes: 'obj_int32 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
996 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
997 'filedes = va_arg(args, obj_int32 *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
998 'size = va_arg(args, obj_int32 *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
999 'str = (string *)make_object(&string_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1000 'str->data = GC_MALLOC_ATOMIC(size->num + 1);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1001 'str->len = str->bytes = read(filedes->num, str->data, size->num);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1002 'if (str->bytes < 0) { str->bytes = str->len = 0; }',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1003 'str->data[str->bytes] = 0;',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1004 'return &(str->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1005 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1006 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1007 os.addMessage('open', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1008 vars: {str: 'string *', flags: 'obj_int32 *', filedes: 'obj_int32 *', perm: 'obj_int32 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1009 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1010 'str = va_arg(args, string *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1011 'flags = va_arg(args, obj_int32 *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1012 'filedes = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1013 'if (num_params == 3) {',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1014 ' filedes->num = open(str->data, flags->num);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1015 '} else if (num_params == 4) {',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1016 ' perm = va_arg(args, obj_int32 *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1017 ' filedes->num = open(str->data, flags->num, perm->num);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1018 '} else {',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1019 ' filedes->num = -1;',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1020 '}',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1021 'return &(filedes->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1022 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1023 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1024 os.addMessage('close', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1025 vars: {filedes: 'obj_int32 *', intret: 'obj_int32 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1026 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1027 'filedes = va_arg(args, obj_int32 *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1028 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1029 'intret->num = close(filedes->num);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1030 'return &(intret->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1031 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1032 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1033 os.addMessage('O_RDONLY', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1034 vars: {intret: 'obj_int32 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1035 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1036 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1037 'intret->num = O_RDONLY;',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1038 'return &(intret->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1039 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1040 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1041 os.addMessage('O_WRONLY', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1042 vars: {intret: 'obj_int32 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1043 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1044 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1045 'intret->num = O_WRONLY;',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1046 'return &(intret->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1047 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1048 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1049 os.addMessage('O_RDWR', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1050 vars: {intret: 'obj_int32 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1051 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1052 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1053 'intret->num = O_RDWR;',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1054 'return &(intret->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1055 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1056 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1057 os.addMessage('O_CREAT', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1058 vars: {intret: 'obj_int32 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1059 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1060 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1061 'intret->num = O_CREAT;',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1062 'return &(intret->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1063 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1064 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1065 os.addMessage('O_APPEND', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1066 vars: {intret: 'obj_int32 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1067 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1068 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1069 'intret->num = O_APPEND;',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1070 'return &(intret->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1071 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1072 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1073 os.addMessage('O_TRUNC', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1074 vars: {intret: 'obj_int32 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1075 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1076 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1077 'intret->num = O_TRUNC;',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1078 'return &(intret->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1079 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1080 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1081 os.addMessage('rand', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1082 vars: {intret: 'obj_int32 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1083 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1084 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1085 'intret->num = rand();',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1086 'return &(intret->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1087 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1088 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1089 os.addMessage('rand64', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1090 vars: {intret64: 'obj_int64 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1091 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1092 'intret64 = (obj_int64 *)make_object(&obj_int64_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1093 'intret64->num = (((int64_t)rand()) << 32 ) | rand();',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1094 'return &(intret64->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1095 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1096 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1097 os.addMessage('srand', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1098 vars: {oseed: 'object *', seed: 'obj_int32 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1099 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1100 'oseed = va_arg(args, object *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1101 'seed = mcall(' + getMethodId("int32") + ', 1, oseed);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1102 'srand(seed->num);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1103 'return &(seed->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1104 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1105 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1106 os.addMessage('time', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1107 vars: {intret64: 'obj_int64 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1108 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1109 'intret64 = (obj_int64 *)make_object(&obj_int64_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1110 'intret64->num = time(NULL);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1111 'return &(intret64->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1112 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1113 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1114 os.addMessage('sleep', {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1115 vars: {osecs: 'object *', secs: 'obj_int32 *', intret: 'obj_int32 *'},
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1116 lines: [
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1117 'osecs = va_arg(args, object *);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1118 'secs = mcall(' + getMethodId("int32") + ', 1, osecs);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1119 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1120 'intret->num = sleep(secs->num);',
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1121 'return &(intret->header);'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1122 ]
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1123 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1124 toplevel.names['os'] = os;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1125 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1126
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1127 modulefile.prototype.toIL = function(){
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1128 return this.ast.toILModuleInstance();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1129 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1130
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1131 function processUsedToplevel(toplevel)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1132 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1133 var alwaysused = ['true', 'false', 'list'];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1134 var ret = '';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1135 var modulenum = 0;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1136 var visited = {};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1137 for (var i in alwaysused) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1138 toplevel.used[alwaysused[i]] = true;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1139 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1140 var newused = Object.keys(toplevel.used);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1141 var allused = newused;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1142 while (newused.length) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1143 for (var i in newused) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1144 debugprint('//---module', newused[i], '--- populate symbols');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1145 forwarddec += 'object * ' + toplevel.moduleVar(newused[i]) + ';\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1146 toplevel.names[newused[i]].populateSymbols(toplevel);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1147 visited[newused[i]] = true;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1148 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1149 newused = [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1150 for (var symbol in toplevel.used) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1151 if (!(symbol in visited)) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1152 debugprint('//found new usage of module', symbol);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1153 newused.push(symbol);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1154 allused.push(symbol);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1155 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1156 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1157 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1158
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1159 for (var i = allused.length-1; i >= 0; i--) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1160 var symbol = allused[i];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1161 debugprint('//---module', symbol, '(' + i +')--- compile');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1162 assignNames.push(symbol);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1163 ret += '\t' + toplevel.moduleVar(symbol) + ' = ' + toplevel.names[symbol].toIL() + ';\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1164 assignNames.pop();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1165 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1166 return ret;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1167 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1168
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1169 function makeCProg(obj)
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1170 {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1171 forwarddec = toplevelcode = '';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1172 assignNames = [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1173 var builtins = builtinTypes();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1174 for (var i in builtins) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1175 forwarddec += builtins[i].toEarlyCDef();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1176 toplevelcode += builtins[i].toILDef();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1177 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1178 addBuiltinModules(toplevel);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1179 debugprint('//------POPULATING SYMBOLS-----');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1180 obj.populateSymbols(toplevel);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1181 var moduleinit = processUsedToplevel(toplevel);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1182 debugprint('//------COMPILING AST-----');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1183 var rest = 'object * mainModule() {\n' + moduleinit + '\tmain_module = ' + obj.toILModuleInstance() + ';\n\treturn main_module;\n}\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1184 var mnames = 'char * methodNames[] = {\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1185 for (var i = 0; i < methodNames.length; i++) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1186 mnames += '\t"' + methodNames[i].replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n") + '",\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1187 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1188 mnames += '};\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1189 return '#include "runtime/proghead.inc"\n' +
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1190 '#define METHOD_ID_MAIN ' + getMethodId('main') + '\n' +
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1191 '#define METHOD_ID_EMPTY ' + getMethodId('empty') + '\n' +
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1192 '#define METHOD_ID_CONS ' + getMethodId(getOpMethodName('|')) + '\n' +
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1193 mnames + forwarddec + toplevelcode + rest + '#include "runtime/progfoot.inc"\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1194 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1195
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1196 object.prototype.toILModule = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1197 return makeCProg(this);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1198 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1199
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1200 object.prototype.toILModuleInstance = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1201 return this.toIL();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1202 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1203
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1204 lambda.prototype.toIL = function(parentcode) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1205 var code = new funCode(this.name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1206 var args = this.args ? this.args.slice(0, this.args.length) : [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1207 var exprs = this.expressions;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1208 var assignPath = assignNames.join('<-');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1209 debugprint('//', this.name, assignPath);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1210 var addedTypeDef = false;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1211 if (Object.keys(this.symbols.closedover).length) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1212 this.symbols.envtype = this.name + '_env';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1213 forwarddec += 'typedef struct ' + this.symbols.envtype + ' ' + this.symbols.envtype + ';\n'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1214 var addedTypeDef = true;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1215 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1216 if (this.selftype) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1217 this.symbols.defineVar('self', this.selftype);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1218 if (args[0] && args[0].cleanName() == 'self') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1219 args.splice(0, 1);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1220 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1221 var offset = 1;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1222 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1223 var offset = 0;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1224 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1225 for (var i = 0; i < args.length; ++i) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1226 var argname = args[i].toIL(code);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1227
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1228 args[i] = (argname.indexOf('->') < 0 ? '\tobject * ' : '\t') + argname + ' = va_arg(args, object *);\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1229 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1230 var compiled = []
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1231 for (var i in exprs) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1232 var js = exprs[i].toIL(code);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1233 if (js) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1234 compiled.push(indent(js));
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1235 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1236 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1237 if (compiled.length) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1238 if (exprs[exprs.length - 1] instanceof assignment) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1239 compiled.push('return ' + exprs[exprs.length - 1].symbol.toIL() + ';');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1240 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1241 compiled[compiled.length-1] = 'return (object *)(' + compiled[compiled.length-1] + ');';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1242 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1243 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1244 exprs = compiled;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1245
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1246 if (Object.keys(this.symbols.closedover).length) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1247 if (!addedTypeDef) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1248 for (var key in this.symbols.closedover) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1249 print(key, ": ", this.symbols.closedover[key]);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1250 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1251 throw new Error('this.symbols.closedover is not empty, but it was when compilation of ' + this.name + ' "' + assignPath + '" started');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1252 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1253 forwarddec += 'struct ' + this.name + '_env {\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1254 if (this.symbols.needsParentEnv) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1255 forwarddec += '\tstruct ' + this.symbols.parentEnvType() + ' * parent;\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1256 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1257 for (var varname in this.symbols.closedover) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1258 if (varname == 'self' && this.selftype) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1259 forwarddec += '\tstruct ' + this.selftype + ' * self;\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1260 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1261 forwarddec += '\tobject * ' + escapeCName(varname) + ';\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1262 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1263 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1264 forwarddec += '};\n'
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1265
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1266 var myenvinit = '\t' + this.name + '_env * myenv = GC_MALLOC(sizeof(' + this.name + '_env));\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1267 if (this.symbols.needsParentEnv) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1268 myenvinit += '\tmyenv->parent = env;\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1269 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1270 this.symbols.envtype = this.name + '_env';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1271 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1272 var myenvinit = '';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1273 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1274 forwarddec += 'object *' + this.name + ' (' + this.symbols.parentEnvType() + ' * env, uint32_t num_args, ...);\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1275
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1276 toplevelcode += '//' + assignPath + "\n";
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1277 toplevelcode += 'object * ' + this.name + ' ( ' + this.symbols.parentEnvType() + ' * env, uint32_t num_args, ...) {\n\tva_list args;\n' + myenvinit + '\tva_start(args, num_args);\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1278 if (this.selftype) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1279 var selfvar = (new symbol('self', this.symbols)).toIL();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1280 if (selfvar == 'self') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1281 toplevelcode += '\t' + this.selftype + ' * self = va_arg(args, ' + this.selftype + ' *);\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1282 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1283 toplevelcode += '\t' + selfvar + ' = va_arg(args, ' + this.selftype + ' *);\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1284 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1285
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1286 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1287 toplevelcode += args.join('') + '\tva_end(args);\n' + exprs.join(';\n\t') + '\n}\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1288
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1289 if (this.selftype) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1290 return this.name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1291 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1292 if (this.symbols.parentEnvType() != 'void') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1293 if (this.symbols.parent.passthruenv) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1294 var envvar = 'env';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1295 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1296 var envvar = 'myenv';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1297 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1298 debugprint('//' + this.name, 'has envvar:', envvar, 'num vars closed over:', Object.keys(this.symbols.closedover).length);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1299 return 'make_lambda(' + envvar + ', (closure_func)' + this.name + ')';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1300 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1301 toplevelcode += 'lambda ' + this.name + '_obj = {{&lambda_meta, NULL}, NULL, ' + this.name + '};\n';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1302 return '((object *)&' + this.name + '_obj)';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1303 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1304 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1305 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1306 lambda.prototype.toILModuleInstance = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1307 this.toIL();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1308 return this.name + '(NULL, 0)';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1309 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1310 lambda.prototype.toILObject = function(typename) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1311 this.selftype = typename;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1312 return this.toIL();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1313 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1314 lambda.prototype.toILModule = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1315 return makeCProg(this);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1316 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1317 lambda.prototype.toILLines = function(vars, needsreturn) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1318 var lines = [];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1319 for (var i in this.args) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1320 var name = this.args[i].name;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1321 if (name[0] == ':') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1322 name = name.substr(1);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1323 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1324 if(name != 'self') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1325 lines.push(name + ' = va_arg(args, ' + vars[name] + ');');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1326 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1327 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1328 for (var i in this.expressions) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1329 var exprlines = this.expressions[i].toILLines(vars, needsreturn && i == this.expressions.length - 1);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1330 for (var j in exprlines) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1331 lines.push('\t' + exprlines[j]);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1332 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1333 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1334 return lines;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1335 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1336 lambda.prototype.toILLLExpr = function(vars) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1337 if (this.expressions.length != 1) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1338 throw new Error('lambda in expression context must have a single statement in llMessage block');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1339 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1340 return this.expressions[0].toILLLExpr(vars);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1341 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1342
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1343 assignment.prototype.toIL = function() {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1344 debugprint('//assignment', this.symbol.name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1345 var existing = this.symbols.find(this.symbol.name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1346 var prefix = '';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1347 assignNames.push(this.symbol.name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1348 var val = this.expression.toIL();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1349 assignNames.pop(this.symbol.name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1350 if (val === null) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1351 return null;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1352 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1353 if (existing.type == 'local' && !existing.isdeclared) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1354 prefix = 'object *';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1355 this.symbols.declareVar(this.symbol.name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1356 debugprint('//declared var', this.symbol.name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1357 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1358 return prefix + this.symbol.toIL() + ' = ' + val;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1359 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1360 assignment.prototype.toILObject = function(cobj) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1361 debugprint('//message definition', this.symbol.name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1362 assignNames.push('#' + this.symbol.name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1363 if (this.expression.toILObject) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1364 var val = this.expression.toILObject(cobj.name);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1365 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1366 var val = this.expression.toIL();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1367 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1368 assignNames.pop();
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1369 if (val === null) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1370 return;
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1371 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1372 if (this.expression instanceof lambda) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1373 var params = ['((object *)self)'];
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1374 var paramget = '';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1375 var messagevars = {};
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1376 for (var i in this.expression.args) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1377 var escaped = escapeCName(this.expression.args[i].cleanName());
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1378 if (escaped != 'self') {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1379 messagevars[escaped] = 'object *';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1380 params.push(escaped);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1381 paramget += escaped + ' = va_arg(args, object *); ';
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1382 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1383 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1384 cobj.addMessage(getOpMethodName(this.symbol.name), {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1385 vars: messagevars,
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1386 lines: [paramget + 'return ' + val + '(' + (cobj.hasenv ? 'self->env' : 'NULL') + ', ' + params.length + (params.length ? ', ' : '') + params.join(', ') + ');']
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1387 });
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1388 } else {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1389 cobj.addProperty(this.symbol.name, val);
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1390 if (this.expression instanceof object && this.expression.symbols.needsparent) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1391 cobj.addInit('self->' + escapeCName(this.symbol.name) + '->parent = (object *)self;');
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1392 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1393 }
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1394 };
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1395 assignment.prototype.toILLines = function(vars, needsreturn) {
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1396 return [(needsreturn ? 'return ' : '') + this.symbol.toILLLExpr(vars) + ' = ' + this.expression.toILLLExpr(vars) + ';']
6fe9343b1400 Some minor work on creating an IL backend based on the C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1397 };