Mercurial > repos > tabletprog
annotate cbackend.js @ 373:a694ffa8d461
Filter out compiled programs from the samples and modules directories
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 12 Aug 2015 19:16:59 -0700 |
parents | 6b5096b07dd5 |
children | 368fbc9ea51b |
rev | line source |
---|---|
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 var mainModule; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 var modules = {}; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 var methodIds = {}; |
182
ab7c142090a0
Make method names available at runtime so they can be included in method not implemented error messages
Mike Pavone <pavone@retrodev.com>
parents:
177
diff
changeset
|
5 var methodNames = []; |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
6 var assignNames; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 function getMethodId(methodName) |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 if (!(methodName in methodIds)) { |
182
ab7c142090a0
Make method names available at runtime so they can be included in method not implemented error messages
Mike Pavone <pavone@retrodev.com>
parents:
177
diff
changeset
|
10 methodIds[methodName] = methodNames.length; |
ab7c142090a0
Make method names available at runtime so they can be included in method not implemented error messages
Mike Pavone <pavone@retrodev.com>
parents:
177
diff
changeset
|
11 methodNames.push(methodName); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 return methodIds[methodName]; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 |
122
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
16 function getOpMethodName(opname) |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
17 { |
265
d6a4c9e7716e
Remove remapping of most operators
Michael Pavone <pavone@retrodev.com>
parents:
251
diff
changeset
|
18 var optoMeth = {'&&':'if', '||':'ifnot'}; |
122
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
19 if (opname in optoMeth) { |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
20 return optoMeth[opname]; |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
21 } else { |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
22 return opname; |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
23 } |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
24 } |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
25 |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
26 op.prototype.toC = function(isReceiver) { |
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
27 var method = getOpMethodName(this.op); |
170
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
28 if (this.op == '|') { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
29 return 'mcall(' + getMethodId(method) + '/* operator ' + method + ' */, 2, (object *)' + this.right.toC() + ', ' + this.left.toC() + ')\n'; |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
30 } else { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
31 return 'mcall(' + getMethodId(method) + '/* operator ' + method + ' */, 2, (object *)' + this.left.toC() + ', ' + this.right.toC() + ')\n'; |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
32 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 }; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
34 op.prototype.toCLLExpr = function(vars) { |
177
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
35 var opmap = {'=': '==', 'xor': '^', 'or': '|', 'and': '&'}; |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
36 return this.left.toCLLExpr(vars) + (this.op in opmap ? opmap[this.op] : this.op) + this.right.toCLLExpr(vars); |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
37 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
38 op.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
39 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) + ';']; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
40 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
41 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 function escapeCName(name) |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 { |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
45 if (name == 'self') { |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
46 return name; |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
47 } |
172
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
48 name = name.replace(/_/g, "UN_").replace(/:/g, "CN_").replace(/!/g, "EX_").replace(/\?/g, 'QS_').replace(/@/g, 'AT_'); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 name = 'tp_' + name; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 return name; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 |
54 | 53 function getSymbolPrefix(info, symbols) |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
54 { |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 var pre = ''; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
56 switch(info.type) { |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
57 case 'self': |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
58 |
54 | 59 pre = (new symbol('self', symbols)).toC() + '->'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
60 break; |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
61 case 'parent': |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
62 pre = (new symbol('self', symbols)).toC() + '->header.'; |
32
64f1d516fbfd
Tiny bit of work on closures
Mike Pavone <pavone@retrodev.com>
parents:
31
diff
changeset
|
63 for (var i = 0; i < info.depth; ++i) { |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
64 pre += (i ? '->' : '') + 'parent'; |
32
64f1d516fbfd
Tiny bit of work on closures
Mike Pavone <pavone@retrodev.com>
parents:
31
diff
changeset
|
65 } |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
66 pre = '((' + info.selftype + ' *)' + pre + ')->'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
67 break; |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
68 case 'upvar': |
32
64f1d516fbfd
Tiny bit of work on closures
Mike Pavone <pavone@retrodev.com>
parents:
31
diff
changeset
|
69 pre = 'env->'; |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
70 for (var i = info.startdepth; i < info.depth; ++i) { |
32
64f1d516fbfd
Tiny bit of work on closures
Mike Pavone <pavone@retrodev.com>
parents:
31
diff
changeset
|
71 pre += 'parent->'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 } |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
73 break; |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
74 case 'recupvar': |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
75 if (info.subtype == 'object') { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
76 pre = 'self->env->'; |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
77 } else { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
78 //TODO: fill this case in if necessary |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
79 } |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
80 pre += getSymbolPrefix(info.parent); |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
81 case 'closedover': |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
82 pre = 'myenv->'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 } |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
84 return pre; |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
85 } |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
86 |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
87 symbol.prototype.toC = function() { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
88 var name = this.cleanName(); |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
89 var info = this.symbols.find(name); |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
90 if (!info) { |
172
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
91 throw new Error('symbol ' + name + ' not found in ' + assignNames.join('<-')); |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
92 } |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
93 if (info.type == 'toplevel') { |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
94 return toplevel.moduleVar(name); |
69
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
95 } else if (info.type == 'self' && info.def instanceof lambda) { |
267 | 96 return 'mcall(' + getMethodId(name) + '/* ' + name + ' */, 1, (object *)' + (new symbol('self', this.symbols)).toC() + ')'; |
69
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
97 } else if (info.type == 'parent' && info.def instanceof lambda) { |
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
98 var obj = (new symbol('self', this.symbols)).toC() + '->header.'; |
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
99 for (var i = 0; i < info.depth; ++i) { |
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
100 obj += (i ? '->' : '') + 'parent'; |
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
101 } |
ba032565c7a5
Fix handling of variable style access to self and parent object messages defined with lambdas. Improve test case for this bug to include parent object access as well as self object access.
Mike Pavone <pavone@retrodev.com>
parents:
68
diff
changeset
|
102 return 'mcall(' + getMethodId(name) + '/* ' + name + ' */, 1, ' + obj + ')'; |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
103 } |
54 | 104 return getSymbolPrefix(info, this.symbols) + escapeCName(name); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
105 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
106 symbol.prototype.toCTypeName = function() { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
107 return this.cleanName(); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
108 }; |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
109 symbol.prototype.cSafeName = function() { |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
110 var name = this.cleanName(); |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
111 if (name[0] >= "0" && name[0] <= "9") { |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
112 name = '_tp_' + name; |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
113 } |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
114 return name; |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
115 }; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
116 symbol.prototype.toCLLExpr = function(vars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
117 var name = this.cleanName(); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
118 if (name in vars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
119 return name; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
120 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
121 if (name == 'self') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
122 return 'self'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
123 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
124 var info = this.symbols.find(name, false, true); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
125 var symbols = this.symbols; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
126 while (info && info.type == 'local') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
127 symbols = symbols.parent; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
128 info = symbols.find(name, false, true); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
129 } |
369
6b5096b07dd5
Lame hack to allow LL dialect code to access the module object when its also the parent of the current object
Michael Pavone <pavone@retrodev.com>
parents:
361
diff
changeset
|
130 if (info && info.type == 'parent') { |
6b5096b07dd5
Lame hack to allow LL dialect code to access the module object when its also the parent of the current object
Michael Pavone <pavone@retrodev.com>
parents:
361
diff
changeset
|
131 //parent reference are not currently supported in the LL dialect |
6b5096b07dd5
Lame hack to allow LL dialect code to access the module object when its also the parent of the current object
Michael Pavone <pavone@retrodev.com>
parents:
361
diff
changeset
|
132 //but parent might refer to a module |
6b5096b07dd5
Lame hack to allow LL dialect code to access the module object when its also the parent of the current object
Michael Pavone <pavone@retrodev.com>
parents:
361
diff
changeset
|
133 info = toplevel.find(name); |
6b5096b07dd5
Lame hack to allow LL dialect code to access the module object when its also the parent of the current object
Michael Pavone <pavone@retrodev.com>
parents:
361
diff
changeset
|
134 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
135 if (!info) { |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
136 return this.cSafeName(); |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
137 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
138 if (info.type == 'toplevel') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
139 return toplevel.moduleVar(name); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
140 } else if (info.type == 'self') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
141 if (info.isll || !(info.def instanceof lambda)) { |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
142 return 'self->' + this.cSafeName(); |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
143 } else { |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
144 return 'mcall(' + getMethodId(name) + '/* ' + name + ' */, 1, self)'; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
145 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
146 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
147 throw new Error('Unsupported reference type ' + info.type + ' for variable ' + name); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
148 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
149 symbol.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
150 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) + ';' ]; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
151 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
152 |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
153 var declaredInts = {}; |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
154 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
155 intlit.prototype.toC = function() { |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
156 var intType = (this.unsigned ? 'u' : '') + 'int' + this.bits; |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
157 var str = intType + '_' + (this.val < 0 ? 'neg_' + (0-this.val).toString() : this.val.toString()); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
158 if (!(str in declaredInts)) { |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
159 toplevelcode += 'obj_' + intType + ' ' + str + ' = {{&obj_' + intType + '_meta, NULL}, ' + this.val.toString() + '};\n'; |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
160 declaredInts[str] = true; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
161 } |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
162 return '((object *)&' + str + ')'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
163 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
164 intlit.prototype.toCLLExpr = function(vars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
165 return this.val.toString(); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
166 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
167 intlit.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
168 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) + ';' ]; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
169 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
170 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
171 floatlit.prototype.toC = function() { |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
172 var floatType = this.bits == 32 ? 'float' : 'double'; |
340
7279e21dad68
Fix internal variable name generation for certain float values
Michael Pavone <pavone@retrodev.com>
parents:
336
diff
changeset
|
173 var str = floatType + '_' + (this.val < 0 ? 'neg_' + (0-this.val).toString() : this.val.toString()).replace('.', '_').replace('-', 'neg'); |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
174 if (!(str in declaredInts)) { |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
175 toplevelcode += 'obj_float' + this.bits + ' ' + str + ' = {{&obj_float' + this.bits + '_meta, NULL}, ' + this.val.toString() + '};\n'; |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
176 declaredInts[str] = true; |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
177 } |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
178 return '((object *)&' + str + ')'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
179 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
180 floatlit.prototype.toCLLExpr = function(vars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
181 return this.val.toString(); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
182 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
183 floatlit.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
184 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) + ';' ]; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
185 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
186 |
41 | 187 var declaredStrings = {}; |
188 var nextStringId = 0; | |
189 | |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
190 strlit.prototype.toC = function() { |
41 | 191 if (!(this.val in declaredStrings)) { |
192 //TODO: get the proper byte length | |
146
d8f92ebf1ff6
Fix string literal escaping to allow more than one of each type of escaped character
Mike Pavone <pavone@retrodev.com>
parents:
143
diff
changeset
|
193 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'; |
41 | 194 declaredStrings[this.val] = nextStringId++; |
195 } | |
196 return '((object *)&str_' + declaredStrings[this.val] + ')'; | |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
197 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
198 strlit.prototype.toCLLExpr = function(vars) { |
146
d8f92ebf1ff6
Fix string literal escaping to allow more than one of each type of escaped character
Mike Pavone <pavone@retrodev.com>
parents:
143
diff
changeset
|
199 return '"' + this.val.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r') + '"'; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
200 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
201 strlit.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
202 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) +';' ]; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
203 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
204 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
205 listlit.prototype.toC = function() { |
40
927fd7911a01
Add append message to array
Mike Pavone <pavone@retrodev.com>
parents:
39
diff
changeset
|
206 var ret = 'make_list(' + this.val.length; |
170
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
207 for (var i = this.val.length-1; i >= 0; i--) { |
40
927fd7911a01
Add append message to array
Mike Pavone <pavone@retrodev.com>
parents:
39
diff
changeset
|
208 ret += ', ' + this.val[i].toC(); |
38 | 209 } |
210 return ret + ')'; | |
211 } | |
212 | |
213 arraylit.prototype.toC = function() { | |
40
927fd7911a01
Add append message to array
Mike Pavone <pavone@retrodev.com>
parents:
39
diff
changeset
|
214 var ret = 'make_array(' + this.val.length; |
38 | 215 for (var i = 0; i < this.val.length; i++) { |
40
927fd7911a01
Add append message to array
Mike Pavone <pavone@retrodev.com>
parents:
39
diff
changeset
|
216 ret += ', ' + this.val[i].toC(); |
38 | 217 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
218 return ret + ')'; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
219 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
220 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
221 funcall.prototype.toC = function() { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
222 var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
223 if (name == 'foreign') { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
224 if ((this.args[0] instanceof lambda) || (this.args[0] instanceof object)) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
225 return null; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
226 } else if(this.args[0] instanceof symbol) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
227 return this.args[0].name; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
228 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
229 throw new Error("Unexpected AST type for foreign:"); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
230 } |
211
53cd9c3bcf96
Don't compile quote expressions in C backend for now
Mike Pavone <pavone@retrodev.com>
parents:
182
diff
changeset
|
231 } else if(name == 'llProperty:withType' || name == 'llProperty:withVars:andCode' || name == 'quote') { |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
232 return null; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
233 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
234 var args = this.args.slice(0, this.args.length); |
331
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
235 var method = false; |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
236 var start = 0; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
237 if (this.receiver) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
238 args.splice(0, 0, this.receiver); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
239 method = true; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
240 } else { |
331
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
241 var funinfo = this.symbols.find(name); |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
242 if (!funinfo || funinfo.def instanceof setter || funinfo.type == 'toplevel') { |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
243 method = true; |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
244 } else { |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
245 switch(funinfo.type) |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
246 { |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
247 case 'self': |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
248 case 'parent': |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
249 var defargs = funinfo.def.args.length; |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
250 if (!defargs || funinfo.def.args[0].cleanName() != 'self') { |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
251 defargs ++ |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
252 } |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
253 if (args.length < defargs) { |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
254 if (funinfo.type == 'self') { |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
255 args.splice(0, 0, new symbol('self', this.symbols)); |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
256 } else { |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
257 var obj = (new symbol('self', this.symbols)).toC() + '->header.'; |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
258 for (var i = 0; i < funinfo.depth; ++i) { |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
259 obj += (i ? '->' : '') + 'parent'; |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
260 } |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
261 args.splice(0, 0, ', ' + obj); |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
262 start = 1; |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
263 } |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
264 } else if(!(args[0] instanceof symbol) || args[0].name != 'self') { |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
265 funinfo = null; |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
266 } |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
267 method = true; |
61f5b794d939
Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
268 break; |
72
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
269 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
270 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
271 } |
72
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
272 for (var i = start; i < args.length; i++) { |
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
273 args[i] = ', ' + (!i && method ? '(object *)(' : '') + args[i].toC() + (!i && method ? ')' : ''); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
274 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
275 var callpart; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
276 if (method) { |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
277 if (funinfo && funinfo.type == 'self' && funinfo.def.name) { |
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
278 callpart = funinfo.def.name + '(' + (funinfo.def.symbols.parent.needsenv ? (new symbol('self', this.symbols)).toC() + '->env' : 'NULL' ); |
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
279 } else { |
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
280 callpart = 'mcall(' + getMethodId(name) + '/* ' + name + ' */'; |
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
281 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
282 } else { |
172
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
283 var closVar = (new symbol(name, this.symbols)).toC(); |
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
284 callpart = '((lambda *)' + closVar + ')->func(((lambda *)' + closVar + ')->env'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
285 } |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
286 return callpart + ', ' + args.length + args.join('') + ')'; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
287 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
288 funcall.prototype.toCTypeName = function() { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
289 switch(this.name) |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
290 { |
285
bb1539decd62
Fix const warning in sdl module
Michael Pavone <pavone@retrodev.com>
parents:
282
diff
changeset
|
291 case 'const:': |
bb1539decd62
Fix const warning in sdl module
Michael Pavone <pavone@retrodev.com>
parents:
282
diff
changeset
|
292 case 'const': |
bb1539decd62
Fix const warning in sdl module
Michael Pavone <pavone@retrodev.com>
parents:
282
diff
changeset
|
293 var receiver = this.receiver ? this.receiver : this.args[0]; |
bb1539decd62
Fix const warning in sdl module
Michael Pavone <pavone@retrodev.com>
parents:
282
diff
changeset
|
294 return 'const ' + receiver.toCTypeName(); |
bb1539decd62
Fix const warning in sdl module
Michael Pavone <pavone@retrodev.com>
parents:
282
diff
changeset
|
295 break; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
296 case 'ptr:': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
297 case 'ptr': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
298 var receiver = this.receiver ? this.receiver : this.args[0]; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
299 return receiver.toCTypeName() + ' *'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
300 break; |
143
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
301 case 'struct:': |
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
302 case 'struct': |
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
303 var receiver = this.receiver ? this.receiver : this.args[0]; |
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
304 return 'struct ' + receiver.toCTypeName(); |
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
305 break; |
177
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
306 case 'funptr:': |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
307 case 'funptr': |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
308 var rettype; |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
309 var firstArg; |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
310 if (this.receiver) { |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
311 rettype = this.receiver; |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
312 firstArg = 0; |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
313 } else { |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
314 rettype = this.args[0]; |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
315 firstArg = 1; |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
316 } |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
317 var argtypes = ''; |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
318 for (var i = firstArg; i < this.args.length; i++) { |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
319 if (argtypes) { |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
320 argtypes += ', ' |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
321 } |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
322 argtypes += this.args[i].toCTypeName(); |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
323 } |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
324 return [rettype.toCTypeName() + '(*', ')(' + argtypes + ')']; |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
325 break; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
326 default: |
316
df4b67d8d2bc
Minor improvement to error message about invalid c type expressions
Michael Pavone <pavone@retrodev.com>
parents:
294
diff
changeset
|
327 throw new Error('invalid use of funcall expression where a C type name is expected: ' + this.name); |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
328 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
329 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
330 funcall.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
331 var lines = []; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
332 var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
333 var args = this.args.slice(0, this.args.length); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
334 if (this.receiver) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
335 args.splice(0, 0, [this.receiver]); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
336 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
337 switch(name) |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
338 { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
339 case 'if': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
340 lines.push('if (' + this.args[0].toCLLExpr(vars) + ') {'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
341 var blines = this.args[1].toCLines(vars, needsreturn); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
342 for (var i in blines) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
343 lines.push('\t' + blines[i]); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
344 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
345 if (needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
346 lines.push('} else {'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
347 lines.push('\t return module_false;'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
348 lines.push('}'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
349 } else { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
350 lines.push('}'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
351 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
352 break; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
353 case 'if:else': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
354 lines.push('if (' + this.args[0].toCLLExpr(vars) + ') {'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
355 var blines = this.args[1].toCLines(vars, needsreturn); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
356 for (var i in blines) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
357 lines.push('\t' + blines[i]); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
358 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
359 lines.push('} else {'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
360 blines = this.args[2].toCLines(vars, needsreturn); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
361 for (var i in blines) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
362 lines.push('\t' + blines[i]); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
363 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
364 lines.push('}'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
365 break; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
366 case 'while:do': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
367 if (needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
368 throw new Error("while:do can't be last statement in llMessage code block"); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
369 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
370 lines.push('while (' + this.args[0].toCLLExpr(vars) + ') {'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
371 var blines = this.args[1].toCLines(vars); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
372 for (var i in blines) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
373 lines.push('\t' + blines[i]); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
374 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
375 lines.push('}'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
376 break; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
377 default: |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
378 lines.push( (needsreturn ? 'return (object *)' : '') + this.toCLLExpr(vars) + ';'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
379 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
380 return lines; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
381 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
382 |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
383 funcall.prototype.toCLLExpr = function(vars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
384 var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
385 var args = this.args.slice(0, this.args.length); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
386 if (this.receiver) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
387 if(this.args.length == 0) { |
143
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
388 if (this.receiver instanceof symbol && this.receiver.name in vars && vars[this.receiver.name].substr(-1) != "*") { |
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
389 return this.receiver.toCLLExpr(vars) + '.' + this.name; |
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
390 } else { |
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
391 return this.receiver.toCLLExpr(vars) + '->' + this.name; |
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
392 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
393 } else if (this.args.length == 1 && name[name.length-1] == '!') { |
143
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
394 if (this.receiver instanceof symbol && this.receiver.name in vars && vars[this.receiver.name].substr(-1) != "*") { |
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
395 return this.receiver.toCLLExpr(vars) + '.' + this.name.substr(0, name.length-1) + ' = ' + args[0].toCLLExpr(vars); |
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
396 } else { |
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
397 return this.receiver.toCLLExpr(vars) + '->' + this.name.substr(0, name.length-1) + ' = ' + args[0].toCLLExpr(vars); |
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
398 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
399 } else { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
400 args.splice(0, 0, this.receiver); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
401 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
402 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
403 switch(name) |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
404 { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
405 case 'if': |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
406 return '((' + args[0].toCLLExpr(vars) + ') ? (' + args[1].toCLLExpr(vars) + ') : 0)'; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
407 case 'if:else': |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
408 return '((' + args[0].toCLLExpr(vars) + ') ? (' + args[1].toCLLExpr(vars) + ') : (' + args[2].toCLLExpr(vars) + '))'; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
409 case 'while:do': |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
410 throw new Error('while:do not allowed in expression context in llMessage block'); |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
411 case 'addr_of': |
282
361a449a7235
Add some extra parens in addr_of translation and fix cleaning of names in llMessage parameters
Michael Pavone <pavone@retrodev.com>
parents:
273
diff
changeset
|
412 return '(&(' + args[0].toCLLExpr(vars) + '))'; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
413 case 'sizeof': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
414 return 'sizeof(' + args[0].toCTypeName() + ')'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
415 case 'get': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
416 return args[0].toCLLExpr(vars) + '[' + args[1].toCLLExpr(vars) + ']'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
417 case 'set': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
418 return args[0].toCLLExpr(vars) + '[' + args[1].toCLLExpr(vars) + '] = ' + args[2].toCLLExpr(vars); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
419 case 'not': |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
420 return '!(' + args[0].toCLLExpr(vars) + ')'; |
177
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
421 case 'castTo': |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
422 return '((' + args[1].toCTypeName() + ')(' + args[0].toCLLExpr(vars) + '))'; |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
423 case 'mcall': |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
424 if (args[0] instanceof symbol) { |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
425 args[0] = new intlit(getMethodId(args[0].name)); |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
426 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
427 default: |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
428 for (var i in args) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
429 args[i] = args[i].toCLLExpr(vars); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
430 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
431 return name + '(' + args.join(', ') + ')'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
432 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
433 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
434 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
435 function cObject(name) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
436 this.name = name; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
437 this.slots = {}; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
438 this.properties = []; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
439 this.values = []; |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
440 this.slotvars = {}; |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
441 this.includes = {}; |
59 | 442 this.parent = 'NULL'; |
443 this.init = []; | |
444 this.initmsgadded = false; | |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
445 this.imported = []; |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
446 } |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
447 |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
448 cObject.prototype.addInclude = function(includefile) { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
449 this.includes[includefile] = true; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
450 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
451 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
452 cObject.prototype.addMessage = function(msgname, implementation) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
453 var methodid = getMethodId(msgname); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
454 var trunc = methodid & 0xF; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
455 if (!(trunc in this.slots)) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
456 this.slots[trunc] = []; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
457 } |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
458 this.slots[trunc].push([methodid, '\t\t' + implementation.lines.join('\n\t\t') + '\n', msgname]); |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
459 if (!(trunc in this.slotvars)) { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
460 this.slotvars[trunc] = {}; |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
461 } |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
462 for (var varname in implementation.vars) { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
463 this.slotvars[trunc][varname] = implementation.vars[varname]; |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
464 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
465 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
466 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
467 cObject.prototype.addProperty = function(propname, value, type) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
468 if (type != undefined) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
469 this.properties.push([propname, type]); |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
470 if (value !== null) { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
471 this.values.push(value); |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
472 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
473 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
474 var escaped = escapeCName(propname); |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
475 this.addMessage(propname, { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
476 vars: {}, |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
477 lines: [ |
267 | 478 'return (object *)self->' + escaped + ';' |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
479 ]}); |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
480 this.addMessage(propname + '!', { |
59 | 481 vars: {}, |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
482 lines: [ |
59 | 483 'self->' + escaped + ' = va_arg(args, object *);', |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
484 'return (object *)self;' |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
485 ]}); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
486 this.properties.push(escaped); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
487 this.values.push(value); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
488 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
489 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
490 |
59 | 491 cObject.prototype.addInit = function(statement) { |
492 this.init.push(statement); | |
140
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
493 }; |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
494 |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
495 cObject.prototype.addImport = function(symbols, source) { |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
496 var importNum = this.imported.indexOf(source); |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
497 if (importNum < 0) { |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
498 var importNum = this.imported.length; |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
499 this.imported.push(source); |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
500 this.addInit('self->import_' + importNum + ' = ' + source.toC() + ';'); |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
501 } |
140
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
502 if (symbols) { |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
503 var self = this; |
140
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
504 each(symbols, function(i, sym) { |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
505 self.addMessage(sym.name, { |
140
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
506 vars: {}, |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
507 lines: [ |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
508 'return self->import_' + importNum + '->meta->meth_lookup[method_id & 0xF](method_id, num_params, self->import_' + importNum + ', args);' |
140
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
509 ] |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
510 }); |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
511 }); |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
512 } else { |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
513 //TODO: handle proxying for unimplemented methods |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
514 } |
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
515 }; |
59 | 516 |
517 cObject.prototype.checkInitMsg = function() { | |
518 if (!this.initmsgadded && this.init.length) { | |
519 var init = this.init.slice(0, this.init.length); | |
520 init.push('return (object *)self;'); | |
521 this.addMessage('_init', { | |
522 vars: {}, | |
523 lines: init | |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
524 }); |
59 | 525 this.initmsgadded = true; |
526 } | |
527 } | |
528 | |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
529 cObject.prototype.populateSymbols = function() {}; |
59 | 530 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
531 cObject.prototype.toEarlyCDef = function() { |
59 | 532 this.checkInitMsg(); |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
533 var includes = ''; |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
534 for (var file in this.includes) { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
535 includes += '#include ' + file + '\n'; |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
536 } |
54 | 537 var objdef = 'typedef struct ' + this.name + ' {\n\tobject header;\n'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
538 for (var i in this.properties) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
539 if (this.properties[i] instanceof Array) { |
177
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
540 if (this.properties[i][1] instanceof Array) { |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
541 objdef += '\t' + this.properties[i][1][0] + this.properties[i][0] + this.properties[i][1][1] + ';\n'; |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
542 } else { |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
543 objdef += '\t' + this.properties[i][1] + ' ' + this.properties[i][0] + ';\n'; |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
544 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
545 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
546 objdef += '\tobject * ' + this.properties[i] + ';\n' |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
547 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
548 } |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
549 for (var i in this.imported) { |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
550 objdef += '\tobject * import_' + i + ';\n'; |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
551 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
552 objdef += '} ' + this.name + ';\nobj_meta ' + this.name + '_meta;\n'; |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
553 return includes + objdef; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
554 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
555 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
556 cObject.prototype.toCDef = function() { |
59 | 557 this.checkInitMsg(); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
558 var slotdefs = ''; |
266
75dc7161c1ca
Added object module which provides some basic reflection capabilities
Michael Pavone <pavone@retrodev.com>
parents:
265
diff
changeset
|
559 var methlists = ''; |
75dc7161c1ca
Added object module which provides some basic reflection capabilities
Michael Pavone <pavone@retrodev.com>
parents:
265
diff
changeset
|
560 var methdict = '}, {' |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
561 var metadef = 'obj_meta ' + this.name + '_meta = {sizeof(' + this.name +'), {'; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
562 for (var i = 0; i < 16; i++) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
563 if (i) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
564 metadef += ', '; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
565 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
566 if (i in this.slots) { |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
567 slotdefs += 'object * ' + this.name + '_slot_' + i + '(uint32_t method_id, uint32_t num_params, object * oself, va_list args) {\n\t' + |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
568 this.name + ' *self = (' + this.name + ' *)oself;\n'; |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
569 for (var varname in this.slotvars[i]) { |
177
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
570 if (this.slotvars[i][varname] instanceof Array) { |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
571 slotdefs += '/*foo*/\t' + this.slotvars[i][varname][0] + ' ' + varname + this.slotvars[i][varname][1] + ';\n'; |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
572 } else { |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
573 slotdefs += '/*bar*/\t' + this.slotvars[i][varname] + ' ' + varname + ';\n'; |
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
574 } |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
575 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
576 if (this.slots[i].length == 1) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
577 slotdefs += '\tif (method_id == ' + this.slots[i][0][0] + ') { /* ' + this.slots[i][0][2] + '*/\n' + |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
578 '\t\t' + this.slots[i][0][1] + '\n' + |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
579 '\t}\n' + |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
580 '\treturn no_impl(method_id, num_params, (object *)self, args);\n}\n'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
581 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
582 slotdefs += '\tswitch(method_id) {\n'; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
583 for (j in this.slots[i]) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
584 slotdefs += '\t\tcase ' + this.slots[i][j][0] + ': /* ' + this.slots[i][j][2] + '*/\n' + |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
585 '\t\t\t' + this.slots[i][j][1] + '\n'; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
586 } |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
587 slotdefs += '\t\tdefault:\n' + |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
588 '\t\t\treturn no_impl(method_id, num_params, (object *)self, args);\n\t}\n}\n'; |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
589 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
590 } |
266
75dc7161c1ca
Added object module which provides some basic reflection capabilities
Michael Pavone <pavone@retrodev.com>
parents:
265
diff
changeset
|
591 methlists += 'uint32_t ' + this.name + '_methods_' + i + '[] = {' |
75dc7161c1ca
Added object module which provides some basic reflection capabilities
Michael Pavone <pavone@retrodev.com>
parents:
265
diff
changeset
|
592 for (j in this.slots[i]) { |
75dc7161c1ca
Added object module which provides some basic reflection capabilities
Michael Pavone <pavone@retrodev.com>
parents:
265
diff
changeset
|
593 methlists += this.slots[i][j][0] + ', '; |
75dc7161c1ca
Added object module which provides some basic reflection capabilities
Michael Pavone <pavone@retrodev.com>
parents:
265
diff
changeset
|
594 } |
75dc7161c1ca
Added object module which provides some basic reflection capabilities
Michael Pavone <pavone@retrodev.com>
parents:
265
diff
changeset
|
595 methlists += 'LAST_METHOD};'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
596 metadef += this.name + '_slot_' + i; |
266
75dc7161c1ca
Added object module which provides some basic reflection capabilities
Michael Pavone <pavone@retrodev.com>
parents:
265
diff
changeset
|
597 methdict += (i ? ', ' : '') + this.name + '_methods_' + i; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
598 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
599 metadef += 'no_impl'; |
266
75dc7161c1ca
Added object module which provides some basic reflection capabilities
Michael Pavone <pavone@retrodev.com>
parents:
265
diff
changeset
|
600 methdict += (i ? ', ' : '') + 'NULL'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
601 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
602 } |
266
75dc7161c1ca
Added object module which provides some basic reflection capabilities
Michael Pavone <pavone@retrodev.com>
parents:
265
diff
changeset
|
603 metadef += methdict + '}};\n'; |
75dc7161c1ca
Added object module which provides some basic reflection capabilities
Michael Pavone <pavone@retrodev.com>
parents:
265
diff
changeset
|
604 return slotdefs + methlists + metadef; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
605 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
606 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
607 cObject.prototype.toCInstance = function() { |
59 | 608 this.checkInitMsg(); |
609 var ret = 'make_object(&' + this.name + '_meta, ' + this.parent + ', ' + this.values.length + (this.values.length ? ', ' : '') + this.values.join(', ') + ')'; | |
610 if (this.initmsgadded) { | |
611 ret = 'mcall(' + getMethodId('_init') + ', 1, ' + ret + ')' | |
612 } | |
613 return ret; | |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
614 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
615 |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
616 cObject.prototype.toC = function() { |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
617 forwarddec += this.toEarlyCDef(); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
618 toplevelcode += this.toCDef(); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
619 return this.toCInstance(); |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
620 } |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
621 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
622 var nextobject = 0; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
623 |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
624 |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
625 object.prototype.toCObject = function() { |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
626 var messages = this.messages; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
627 if (!this.name) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
628 this.name = 'object_' + nextobject++; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
629 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
630 var me = new cObject(this.name); |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
631 this.symbols.typename = me.name; |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
632 if (this.symbols.needsenv) { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
633 me.addProperty('env', this.symbols.envVar(), 'struct ' + this.symbols.getEnvType() + ' * '); |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
634 me.hasenv = true; |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
635 } |
59 | 636 if (this.symbols.needsparent && !(this.symbols.parent instanceof osymbols)) { |
637 me.parent = '(object *)(' + (new symbol('self', this.symbols.parent)).toC() + ')'; | |
638 } | |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
639 for (var i in messages) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
640 if (messages[i] instanceof funcall) { |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
641 var msgname = messages[i].name; |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
642 if (msgname[msgname.length-1] == ':') { |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
643 msgname = msgname.substr(0, msgname.length-1); |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
644 } |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
645 if (msgname == 'import' && messages[i].args.length == 1) { |
140
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
646 me.addImport(false, messages[i].args[0]); |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
647 } else if(msgname == 'import:from' && messages[i].args.length == 2) { |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
648 var importsyms = []; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
649 each(messages[i].args[0].val, function(i, el) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
650 if (!(el instanceof symbol)) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
651 throw new Error('Names in import:from statement must be symbols'); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
652 } |
140
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
653 importsyms.push(el); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
654 }); |
140
bf8f75b69048
Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
122
diff
changeset
|
655 me.addImport(importsyms, messages[i].args[1]); |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
656 } else if(msgname == 'llProperty:withType' && messages[i].args.length == 2) { |
177
76e3d4ae1746
Support more bitwise operations and function pointers in llMessages
Mike Pavone <pavone@retrodev.com>
parents:
172
diff
changeset
|
657 me.addProperty(messages[i].args[0].name, null, messages[i].args[1].toCTypeName()); |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
658 } else if(msgname == 'llMessage:withVars:andCode' && messages[i].args.length == 3) { |
265
d6a4c9e7716e
Remove remapping of most operators
Michael Pavone <pavone@retrodev.com>
parents:
251
diff
changeset
|
659 if (messages[i].args[0] instanceof symbol) { |
d6a4c9e7716e
Remove remapping of most operators
Michael Pavone <pavone@retrodev.com>
parents:
251
diff
changeset
|
660 var msgname = messages[i].args[0].name; |
d6a4c9e7716e
Remove remapping of most operators
Michael Pavone <pavone@retrodev.com>
parents:
251
diff
changeset
|
661 } else if (messages[i].args[0] instanceof strlit) { |
d6a4c9e7716e
Remove remapping of most operators
Michael Pavone <pavone@retrodev.com>
parents:
251
diff
changeset
|
662 var msgname = messages[i].args[0].val; |
d6a4c9e7716e
Remove remapping of most operators
Michael Pavone <pavone@retrodev.com>
parents:
251
diff
changeset
|
663 } else { |
d6a4c9e7716e
Remove remapping of most operators
Michael Pavone <pavone@retrodev.com>
parents:
251
diff
changeset
|
664 throw new Error('First argument to llMessage:withVars:andCode must be a symbol or string'); |
d6a4c9e7716e
Remove remapping of most operators
Michael Pavone <pavone@retrodev.com>
parents:
251
diff
changeset
|
665 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
666 var rawvars = messages[i].args[1].expressions; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
667 var vars = {}; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
668 for(var v in rawvars) { |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
669 vars[rawvars[v].symbol.cSafeName()] = rawvars[v].expression.toCTypeName(); |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
670 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
671 me.addMessage(msgname, { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
672 vars: vars, |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
673 lines: messages[i].args[2].toCLines(vars, true) |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
674 }); |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
675 } else if(msgname == 'includeSystemHeader' && messages[i].args.length == 1) { |
320
1debeb21dd47
Allow symbol expressions in includeSystemHeader calls
Michael Pavone <pavone@retrodev.com>
parents:
316
diff
changeset
|
676 if (messages[i].args[0] instanceof strlit) { |
1debeb21dd47
Allow symbol expressions in includeSystemHeader calls
Michael Pavone <pavone@retrodev.com>
parents:
316
diff
changeset
|
677 me.addInclude("<" + messages[i].args[0].val + ">"); |
1debeb21dd47
Allow symbol expressions in includeSystemHeader calls
Michael Pavone <pavone@retrodev.com>
parents:
316
diff
changeset
|
678 } else if(messages[i].args[0] instanceof symbol) { |
1debeb21dd47
Allow symbol expressions in includeSystemHeader calls
Michael Pavone <pavone@retrodev.com>
parents:
316
diff
changeset
|
679 me.addInclude(messages[i].args[0].name); |
1debeb21dd47
Allow symbol expressions in includeSystemHeader calls
Michael Pavone <pavone@retrodev.com>
parents:
316
diff
changeset
|
680 } else { |
1debeb21dd47
Allow symbol expressions in includeSystemHeader calls
Michael Pavone <pavone@retrodev.com>
parents:
316
diff
changeset
|
681 throw new Error('Argument to includeSystemHeader must be a string literal or symbol'); |
1debeb21dd47
Allow symbol expressions in includeSystemHeader calls
Michael Pavone <pavone@retrodev.com>
parents:
316
diff
changeset
|
682 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
683 } else { |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
684 |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
685 throw new Error('Only import and import:from calls allowed in object context. ' + messages[i].name + 'with ' + messages[i].args.length + ' arguments found instead.'); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
686 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
687 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
688 messages[i].toCObject(me); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
689 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
690 } |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
691 |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
692 return me; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
693 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
694 |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
695 object.prototype.toC = function() { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
696 return this.toCObject().toC(); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
697 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
698 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
699 var toplevelcode; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
700 var forwarddec; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
701 |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
702 function addBinaryOp(cobject, opname, cop, objtype) |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
703 { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
704 cobject.addMessage(opname, { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
705 vars: {ret: objtype + ' *', argb: objtype +' *'}, |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
706 lines: [ |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
707 'argb = va_arg(args, ' + objtype + ' *);', |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
708 'ret = (' + objtype + ' *)make_object(&' + objtype + '_meta, NULL, 0);', |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
709 'ret->num = self->num ' + cop + ' argb->num;', |
52
ab6217b8ae4c
Remove some C compiler warnings due to sloppy native code
Mike Pavone <pavone@retrodev.com>
parents:
49
diff
changeset
|
710 'return &(ret->header);' |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
711 ] |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
712 }); |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
713 } |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
714 |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
715 function addCompOp(cobject, opname, cop, objtype) |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
716 { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
717 cobject.addMessage(opname, { |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
718 vars: {argb: objtype + ' *'}, |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
719 lines: [ |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
720 'argb = va_arg(args, ' + objtype + ' *);', |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
721 'if (self->num ' + cop + ' argb->num) {', |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
722 ' return ' + toplevel.moduleVar('true') + ';', |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
723 '}', |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
724 'return ' + toplevel.moduleVar('false') + ';', |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
725 ] |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
726 }); |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
727 } |
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
728 |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
729 function addNumberOps(obj, typename) |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
730 { |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
731 addBinaryOp(obj, '+', '+', typename); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
732 addBinaryOp(obj, '-', '-', typename); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
733 addBinaryOp(obj, '*', '*', typename); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
734 addBinaryOp(obj, '/', '/', typename); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
735 addCompOp(obj, '<', '<', typename); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
736 addCompOp(obj, '>', '>', typename); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
737 addCompOp(obj, '=', '==', typename); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
738 addCompOp(obj, '!=', '!=', typename); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
739 addCompOp(obj, '>=', '>=', typename); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
740 addCompOp(obj, '<=', '<=', typename); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
741 |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
742 obj.addMessage('jsonEncode', { |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
743 vars: {}, |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
744 lines: [ |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
745 'return mcall(' + getMethodId('string') + ', 1, &self->header);' |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
746 ] |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
747 }); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
748 } |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
749 |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
750 function makeInt(bits, unsigned) |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
751 { |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
752 var typename = 'obj_' + (unsigned ? 'u' : '') + 'int' + bits; |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
753 var intObj = new cObject(typename); |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
754 intObj.addProperty('num', null, (unsigned ? 'u' : '') + 'int' + bits +'_t'); |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
755 addNumberOps(intObj, typename); |
265
d6a4c9e7716e
Remove remapping of most operators
Michael Pavone <pavone@retrodev.com>
parents:
251
diff
changeset
|
756 addBinaryOp(intObj, '%', '%', typename); |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
757 addBinaryOp(intObj, 'or', '|', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
758 addBinaryOp(intObj, 'xor', '^', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
759 addBinaryOp(intObj, 'and', '&', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
760 addBinaryOp(intObj, 'lshift:by', '<<', typename); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
761 addBinaryOp(intObj, 'rshift:by', '>>', typename); |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
762 |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
763 intObj.addInclude('<string.h>'); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
764 //-9223372036854775808 |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
765 //01234567890123456789 |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
766 intObj.addMessage('string', { |
43
27a2167663dd
Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents:
42
diff
changeset
|
767 vars: {str: 'string *'}, |
27a2167663dd
Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents:
42
diff
changeset
|
768 lines: [ |
27a2167663dd
Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents:
42
diff
changeset
|
769 'str = (string *)make_object(&string_meta, NULL, 0);', |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
770 'str->data = GC_MALLOC(' + (bits == 64 ? 21 : 12) + ');', |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
771 'sprintf(str->data, "%' + (bits == 64 ? 'l' : '') + (unsigned ? 'u' : 'd') + '", self->num);', |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
772 'str->len = str->bytes = strlen(str->data);', |
52
ab6217b8ae4c
Remove some C compiler warnings due to sloppy native code
Mike Pavone <pavone@retrodev.com>
parents:
49
diff
changeset
|
773 'return &(str->header);' |
43
27a2167663dd
Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents:
42
diff
changeset
|
774 ] |
27a2167663dd
Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents:
42
diff
changeset
|
775 }); |
288
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
776 intObj.addMessage('utf8', { |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
777 vars: {str: 'string *', norm: 'uint32_t'}, |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
778 lines: [ |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
779 'str = (string *)make_object(&string_meta, NULL, 0);', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
780 'str->len = 1;', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
781 'norm = self->num;', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
782 'if (norm < 0x80) {', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
783 ' str->bytes = 1;', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
784 ' str->data = GC_MALLOC(2);', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
785 ' str->data[0] = norm;', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
786 '} else if(norm < 0x800) {', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
787 ' str->bytes = 2;', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
788 ' str->data = GC_MALLOC(3);', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
789 ' str->data[0] = 0xC0 | norm >> 6;', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
790 ' str->data[1] = 0x80 | (norm & 0x3F);', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
791 '} else if(norm < 0x10000) {', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
792 ' str->bytes = 3;', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
793 ' str->data = GC_MALLOC(4);', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
794 ' str->data[0] = 0xE0 | norm >> 12;', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
795 ' str->data[1] = 0x80 | ((norm >> 6) & 0x3F);', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
796 ' str->data[2] = 0x80 | (norm & 0x3F);', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
797 '} else if(norm < 0x10FFFF) {', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
798 ' str->bytes = 4;', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
799 ' str->data = GC_MALLOC(5);', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
800 ' str->data[0] = 0xF0 | norm >> 18;', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
801 ' str->data[1] = 0x80 | ((norm >> 12) & 0x3F);', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
802 ' str->data[2] = 0x80 | ((norm >> 6) & 0x3F);', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
803 ' str->data[3] = 0x80 | (norm & 0x3F);', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
804 '} else {', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
805 ' str->len = str->bytes = 0;', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
806 ' str->data = GC_MALLOC(1);', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
807 '}', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
808 'str->data[str->bytes] = 0;', |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
809 'return &(str->header);' |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
810 ] |
a4c2b31acba7
Add utf8 method to integer types for converting a number into a utf8 string made up of the corresponding character code
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
811 }); |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
812 //7FFFFFFFFFFFFFFF |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
813 //01234567890123456789 |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
814 intObj.addMessage('hex', { |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
815 vars: {str: 'string *'}, |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
816 lines: [ |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
817 'str = (string *)make_object(&string_meta, NULL, 0);', |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
818 'str->data = GC_MALLOC(' + (bits == 64 ? 17 : 9) + ');', |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
819 'sprintf(str->data, "%' + (bits == 64 ? 'l' : '') +'X", self->num);', |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
820 'str->len = str->bytes = strlen(str->data);', |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
821 'return &(str->header);' |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
822 ] |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
823 }); |
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
824 intObj.addMessage('isInteger?', { |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
825 vars: {}, |
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
826 lines: [ |
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
827 'return ' + toplevel.moduleVar('true') + ';' |
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
828 ] |
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
829 }); |
336
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
830 if (bits == 32 && !unsigned) { |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
831 intObj.addMessage('hash', { |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
832 vars: {}, |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
833 lines: [ |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
834 'return &(self->header);' |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
835 ] |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
836 }); |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
837 } else { |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
838 intObj.addMessage('hash', { |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
839 vars: { |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
840 int32ret: 'obj_int32 *' |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
841 }, |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
842 lines: [ |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
843 'int32ret = make_object(&obj_int32_meta, NULL, 0);', |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
844 bits < 64 ? 'int32ret->num = self->num;' : 'int32ret->num = self->num ^ (self->num >> 32);', |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
845 'return &(int32ret->header);' |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
846 ] |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
847 }); |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
848 } |
172
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
849 intObj.addMessage('signed?', { |
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
850 vars: {}, |
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
851 lines: [ |
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
852 'return ' + toplevel.moduleVar(unsigned ? 'false' : 'true') + ';' |
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
853 ] |
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
854 }); |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
855 var sizes = [8, 16, 32, 64]; |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
856 var destunsigned = [false, true]; |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
857 for (var i = 0; i < sizes.length; i++) { |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
858 size = sizes[i]; |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
859 for (var j = 0; j < destunsigned.length; j++) { |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
860 uns = destunsigned[j]; |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
861 if (uns == unsigned && size == bits) { |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
862 intObj.addMessage((uns ? 'u' : '') + 'int' + size, { |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
863 vars: {}, |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
864 lines: [ |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
865 'return &(self->header);' |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
866 ] |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
867 }); |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
868 } else { |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
869 var retType = 'obj_' + (uns ? 'u' : '') + 'int' + size; |
161
fc8eecad71e6
Fix variable name collision in integer objects
Mike Pavone <pavone@retrodev.com>
parents:
156
diff
changeset
|
870 var retName = 'ret' + (uns ? 'u' : '') + size; |
fc8eecad71e6
Fix variable name collision in integer objects
Mike Pavone <pavone@retrodev.com>
parents:
156
diff
changeset
|
871 var vars = {}; |
fc8eecad71e6
Fix variable name collision in integer objects
Mike Pavone <pavone@retrodev.com>
parents:
156
diff
changeset
|
872 vars[retName] = retType + ' *'; |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
873 intObj.addMessage((uns ? 'u' : '') + 'int' + size, { |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
874 |
161
fc8eecad71e6
Fix variable name collision in integer objects
Mike Pavone <pavone@retrodev.com>
parents:
156
diff
changeset
|
875 vars: vars, |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
876 lines: [ |
161
fc8eecad71e6
Fix variable name collision in integer objects
Mike Pavone <pavone@retrodev.com>
parents:
156
diff
changeset
|
877 retName + ' = ('+retType+' *)make_object(&' + retType +'_meta, NULL, 0);', |
fc8eecad71e6
Fix variable name collision in integer objects
Mike Pavone <pavone@retrodev.com>
parents:
156
diff
changeset
|
878 retName + '->num = self->num;', |
fc8eecad71e6
Fix variable name collision in integer objects
Mike Pavone <pavone@retrodev.com>
parents:
156
diff
changeset
|
879 'return &(' + retName + '->header);' |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
880 ] |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
881 }); |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
882 } |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
883 } |
325
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
884 if (size > 16) { |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
885 var retType = 'obj_float' + size; |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
886 var retName = 'retfloat' + size; |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
887 var vars = {}; |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
888 vars[retName] = retType + ' *'; |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
889 intObj.addMessage((size > bits ? 'f' : 'truncF') + size, { |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
890 vars: vars, |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
891 lines: [ |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
892 retName + ' = make_object(&' + retType + '_meta, NULL, 0);', |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
893 retName + '->num = self->num;', |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
894 'return &(' + retName + '->header);' |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
895 ] |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
896 }) |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
897 } |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
898 } |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
899 |
135
f98790d8a53d
Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents:
134
diff
changeset
|
900 return intObj; |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
901 } |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
902 |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
903 function makeFloat(bits) |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
904 { |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
905 var typename = 'obj_float' + bits; |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
906 var floatObj = new cObject(typename); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
907 floatObj.addProperty('num', null, bits == 32 ? 'float' : 'double'); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
908 addNumberOps(floatObj, typename); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
909 |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
910 floatObj.addInclude('<string.h>'); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
911 floatObj.addMessage('string', { |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
912 vars: {str: 'string *'}, |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
913 lines: [ |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
914 'str = (string *)make_object(&string_meta, NULL, 0);', |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
915 //probably overkill, but lets play it safe for now |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
916 'str->data = GC_MALLOC(128);', |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
917 'sprintf(str->data, "%f", self->num);', |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
918 'str->len = str->bytes = strlen(str->data);', |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
919 'return &(str->header);' |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
920 ] |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
921 }); |
336
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
922 |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
923 floatObj.addMessage('hash', { |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
924 vars: { |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
925 intptr: 'int32_t *', |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
926 int32ret: 'obj_int32 *' |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
927 }, |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
928 lines: [ |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
929 'intptr = (int32_t *)&self->num;', |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
930 'int32ret = make_object(&obj_int32_meta, NULL, 0);', |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
931 'int32ret->num = *intptr' + (size == 64 ? ' ^ intptr[1];' : ';'), |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
932 'return &(int32ret->header);' |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
933 ] |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
934 }); |
2a0463c46913
Add hash method to float types and make hash always return a 32-bit int
Michael Pavone <pavone@retrodev.com>
parents:
331
diff
changeset
|
935 |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
936 floatObj.addMessage('f' + bits, { |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
937 vars: {}, |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
938 lines: [ |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
939 'return &(self->header);' |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
940 ] |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
941 }); |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
942 |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
943 floatObj.addMessage('f' + (bits == 32 ? 64 : 32), { |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
944 vars: {trans: 'obj_float' + (bits == 32 ? 64 : 32) + ' *'}, |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
945 lines: [ |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
946 'trans = make_object(&obj_float' + (bits == 32 ? 64 : 32) + '_meta, NULL, 0);', |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
947 'trans->num = self->num;', |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
948 'return &(trans->header);' |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
949 ] |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
950 }); |
325
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
951 var sizes = [8, 16, 32, 64]; |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
952 var prefixes = ['U', '']; |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
953 for (var i = 0; i < sizes.length; i++) { |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
954 for (var j = 0; j < prefixes.length; j++) { |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
955 var ret = 'ret' + i + j; |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
956 var vars = {}; |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
957 var retType = 'obj_' + prefixes[j].toLowerCase() + 'int' + sizes[i]; |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
958 vars[ret] = retType + ' *'; |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
959 floatObj.addMessage('trunc' + prefixes[j] + 'Int' + sizes[i], { |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
960 vars: vars, |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
961 lines: [ |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
962 ret + ' = make_object(&' + retType + '_meta, NULL, 0);', |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
963 ret + '->num = self->num;', |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
964 'return &(' + ret + '->header);' |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
965 ] |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
966 }); |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
967 } |
4a79311dbd29
Added conversion methods between integer and float types
Michael Pavone <pavone@retrodev.com>
parents:
320
diff
changeset
|
968 } |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
969 return floatObj; |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
970 } |
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
971 |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
972 function makeCPointer() |
282
361a449a7235
Add some extra parens in addr_of translation and fix cleaning of names in llMessage parameters
Michael Pavone <pavone@retrodev.com>
parents:
273
diff
changeset
|
973 { |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
974 var cptr = new cObject('cpointer'); |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
975 cptr.addProperty('val', null, 'void *'); |
282
361a449a7235
Add some extra parens in addr_of translation and fix cleaning of names in llMessage parameters
Michael Pavone <pavone@retrodev.com>
parents:
273
diff
changeset
|
976 //cpointer: |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
977 //1234567890 |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
978 cptr.addMessage('string', { |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
979 vars: {ret: 'string *'}, |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
980 lines: [ |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
981 'ret = make_object(&string_meta, NULL, 0);', |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
982 //22 digits for worst case sensible representation |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
983 //10 chars for prefix |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
984 //4 for slop |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
985 //1 for null terminator |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
986 'ret->data = GC_MALLOC_ATOMIC(22+10+4+1);', |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
987 'snprintf(ret->data, 22+10+4, "cpointer: %p", self->val);', |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
988 'ret->data[22+10+4] = 0;', |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
989 'return (object *)ret;' |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
990 ] |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
991 }); |
361
06dceff348ea
llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents:
340
diff
changeset
|
992 cptr.addMessage('address', { |
06dceff348ea
llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents:
340
diff
changeset
|
993 vars: {intret: 'obj_uint64 *'}, |
06dceff348ea
llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents:
340
diff
changeset
|
994 lines: [ |
06dceff348ea
llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents:
340
diff
changeset
|
995 'intret = make_object(&obj_uint64_meta, NULL, 0);', |
06dceff348ea
llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents:
340
diff
changeset
|
996 'intret->num = (uint64_t)self->val;', |
06dceff348ea
llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents:
340
diff
changeset
|
997 'return intret;' |
06dceff348ea
llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents:
340
diff
changeset
|
998 ] |
06dceff348ea
llcompile now has Hacky support for calling C functions using dl to lookup symbols and almost has support string constants
Michael Pavone <pavone@retrodev.com>
parents:
340
diff
changeset
|
999 }) |
273
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
1000 return cptr; |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
1001 } |
0dc7322590da
Make import:from actually work. Fix some macro bugs. Add a cpointer primitive type for storing an opaque pointer to a C object.
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
1002 |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1003 function makeArray() |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1004 { |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1005 var arrayfile = toplevel.names['array']; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1006 var ast = parseFile(arrayfile.path + '/' + arrayfile.file); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1007 ast.name = 'array'; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1008 ast.populateSymbols(toplevel); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1009 return ast.toCObject(); |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1010 } |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1011 |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1012 function makeString() |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1013 { |
87
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
1014 var arrayfile = toplevel.names['string']; |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
1015 var ast = parseFile(arrayfile.path + '/' + arrayfile.file); |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
1016 ast.name = 'string'; |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
1017 ast.populateSymbols(toplevel); |
25bc8a5ab41e
Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents:
84
diff
changeset
|
1018 return ast.toCObject(); |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1019 } |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1020 |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1021 function makelambda() |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1022 { |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1023 var clos = new cObject('lambda'); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1024 clos.addProperty('env', null, 'void *'); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1025 clos.addProperty('func', null, 'closure_func'); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1026 clos.addMessage('while:do', { |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1027 vars: {action: 'lambda *', ret: 'object *'}, |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1028 lines: [ |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1029 'action = va_arg(args, lambda *);', |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1030 'ret = ' + toplevel.moduleVar('true') + ';', |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1031 'while(' + toplevel.moduleVar('true') + ' == ccall(self, 0)) {', |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1032 ' ccall(action, 0);', |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1033 '}', |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1034 'return ret;' |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1035 ] |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1036 }); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1037 return clos; |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1038 } |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1039 |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1040 function builtinTypes() |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1041 { |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1042 return [makeInt(64, false), makeInt(32, false), makeInt(16, false), makeInt(8, false), |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1043 makeInt(64, true) , makeInt(32, true), makeInt(16, true), makeInt(8, true), |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
285
diff
changeset
|
1044 makeFloat(32), makeFloat(64), makeArray(), makeString(), makelambda(), makeCPointer()]; |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1045 } |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1046 |
66
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
61
diff
changeset
|
1047 modulefile.prototype.toC = function(){ |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1048 return this.ast.toCModuleInstance(); |
66
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
61
diff
changeset
|
1049 }; |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
61
diff
changeset
|
1050 |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
1051 function processUsedToplevel(toplevel) |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
1052 { |
329
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1053 var alwaysused = ['true', 'false', 'list']; |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
1054 var ret = ''; |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
1055 var modulenum = 0; |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1056 var visited = {}; |
329
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1057 var newused = alwaysused;//Object.keys(toplevel.used); |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1058 var allused = newused; |
329
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1059 print('//newused = ' + newused.join(', ')); |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1060 while (newused.length) { |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1061 for (var i in newused) { |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1062 debugprint('//---module', newused[i], '--- populate symbols'); |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1063 forwarddec += 'object * ' + toplevel.moduleVar(newused[i]) + ';\n'; |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1064 toplevel.names[newused[i]].populateSymbols(toplevel); |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1065 visited[newused[i]] = true; |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1066 } |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1067 newused = []; |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1068 for (var symbol in toplevel.used) { |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1069 if (!(symbol in visited)) { |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1070 debugprint('//found new usage of module', symbol); |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1071 newused.push(symbol); |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1072 allused.push(symbol); |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1073 } |
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
1074 } |
329
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1075 } |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1076 var compiled = {}; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1077 //true and false depend on each other, but not at module init time |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1078 //force them to have no dependencies |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1079 toplevel.names['true'].dependencies = {}; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1080 toplevel.names['false'].dependencies = {}; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1081 //string and object can be a problem as well and is safe to init them before other things |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1082 toplevel.names['string'].dependencies = {}; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1083 toplevel.names['object'].dependencies = {}; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1084 |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1085 while (allused.length) { |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1086 var nextused = []; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1087 var minUnmet = allused.length; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1088 var minUnmetIdx = -1; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1089 for (var i = 0; i < allused.length; i++) { |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1090 var symbol = allused[i]; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1091 var module = toplevel.names[symbol]; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1092 var unmet = 0; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1093 for (var dependency in module.dependencies) { |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1094 if (!(dependency in compiled) && dependency != symbol) { |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1095 unmet++; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1096 } |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1097 } |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1098 if (unmet) { |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1099 nextused.push(symbol); |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1100 if (unmet < minUnmet) { |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1101 minUnmet = unmet; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1102 minUnmetIdx = i; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1103 } |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1104 } else { |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1105 debugprint('//---module', symbol, '(' + i +')--- compile'); |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1106 debugprint('// dependencies: ' + Object.keys(toplevel.names[symbol].dependencies).join(', ')); |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1107 assignNames.push(symbol); |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1108 ret += '\t' + toplevel.moduleVar(symbol) + ' = ' + toplevel.names[symbol].toC() + ';\n'; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1109 assignNames.pop(); |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1110 compiled[symbol] = true; |
289
befaefdfcd8a
Make sure that true and false modules are initialized before anything else and that the list module is initialized before any modules besides true and false
Michael Pavone <pavone@retrodev.com>
parents:
288
diff
changeset
|
1111 } |
befaefdfcd8a
Make sure that true and false modules are initialized before anything else and that the list module is initialized before any modules besides true and false
Michael Pavone <pavone@retrodev.com>
parents:
288
diff
changeset
|
1112 } |
329
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1113 if (nextused.length == allused.length) { |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1114 var symbol = nextused[minUnmetIdx]; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1115 print('//WARNING: circular module dependency detected, compiling ' + symbol + ' with dependencies ' + Object.keys(toplevel.names[symbol].dependencies).join(', ')); |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1116 print('// but only these dependencies are met: ' + Object.keys(compiled).join(', ')); |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1117 debugprint('//---module', symbol, '(' + i +')--- compile'); |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1118 assignNames.push(symbol); |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1119 ret += '\t' + toplevel.moduleVar(symbol) + ' = ' + toplevel.names[symbol].toC() + ';\n'; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1120 assignNames.pop(); |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1121 compiled[symbol] = true; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1122 nextused[minUnmetIdx] = nextused[nextused.length-1]; |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1123 nextused.pop(); |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1124 } |
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
325
diff
changeset
|
1125 allused = nextused; |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
1126 } |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
1127 return ret; |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
1128 } |
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
1129 |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1130 function makeCProg(obj) |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1131 { |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1132 forwarddec = toplevelcode = ''; |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1133 assignNames = []; |
45
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1134 var builtins = builtinTypes(); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1135 for (var i in builtins) { |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1136 forwarddec += builtins[i].toEarlyCDef(); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1137 toplevelcode += builtins[i].toCDef(); |
2a9c6eed0c70
Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents:
43
diff
changeset
|
1138 } |
54 | 1139 debugprint('//------POPULATING SYMBOLS-----'); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1140 obj.populateSymbols(toplevel); |
48
18ab96287c3a
Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents:
45
diff
changeset
|
1141 var moduleinit = processUsedToplevel(toplevel); |
54 | 1142 debugprint('//------COMPILING AST-----'); |
60
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
1143 var rest = 'object * mainModule() {\n' + moduleinit + '\tmain_module = ' + obj.toCModuleInstance() + ';\n\treturn main_module;\n}\n'; |
182
ab7c142090a0
Make method names available at runtime so they can be included in method not implemented error messages
Mike Pavone <pavone@retrodev.com>
parents:
177
diff
changeset
|
1144 var mnames = 'char * methodNames[] = {\n'; |
ab7c142090a0
Make method names available at runtime so they can be included in method not implemented error messages
Mike Pavone <pavone@retrodev.com>
parents:
177
diff
changeset
|
1145 for (var i = 0; i < methodNames.length; i++) { |
ab7c142090a0
Make method names available at runtime so they can be included in method not implemented error messages
Mike Pavone <pavone@retrodev.com>
parents:
177
diff
changeset
|
1146 mnames += '\t"' + methodNames[i].replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n") + '",\n'; |
ab7c142090a0
Make method names available at runtime so they can be included in method not implemented error messages
Mike Pavone <pavone@retrodev.com>
parents:
177
diff
changeset
|
1147 } |
ab7c142090a0
Make method names available at runtime so they can be included in method not implemented error messages
Mike Pavone <pavone@retrodev.com>
parents:
177
diff
changeset
|
1148 mnames += '};\n'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1149 return '#include "runtime/proghead.inc"\n' + |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1150 '#define METHOD_ID_MAIN ' + getMethodId('main') + '\n' + |
170
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1151 '#define METHOD_ID_EMPTY ' + getMethodId('empty') + '\n' + |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1152 '#define METHOD_ID_CONS ' + getMethodId(getOpMethodName('|')) + '\n' + |
182
ab7c142090a0
Make method names available at runtime so they can be included in method not implemented error messages
Mike Pavone <pavone@retrodev.com>
parents:
177
diff
changeset
|
1153 mnames + forwarddec + toplevelcode + rest + '#include "runtime/progfoot.inc"\n'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1154 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1155 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1156 object.prototype.toCModule = function() { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1157 return makeCProg(this); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1158 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1159 |
60
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
1160 object.prototype.toCModuleInstance = function() { |
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
1161 return this.toC(); |
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
1162 } |
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
1163 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1164 lambda.prototype.toC = function() { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1165 var args = this.args ? this.args.slice(0, this.args.length) : []; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1166 var exprs = this.expressions; |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1167 var assignPath = assignNames.join('<-'); |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1168 debugprint('//', this.name, assignPath); |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1169 var addedTypeDef = false; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1170 if (Object.keys(this.symbols.closedover).length) { |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
1171 this.symbols.envtype = this.name + '_env'; |
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
1172 forwarddec += 'typedef struct ' + this.symbols.envtype + ' ' + this.symbols.envtype + ';\n' |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1173 var addedTypeDef = true; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1174 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1175 if (this.selftype) { |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1176 this.symbols.defineVar('self', this.selftype); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1177 if (args[0] && args[0].cleanName() == 'self') { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1178 args.splice(0, 1); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1179 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1180 var offset = 1; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1181 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1182 var offset = 0; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1183 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1184 for (var i = 0; i < args.length; ++i) { |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1185 var argname = args[i].toC(); |
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.
Michael Pavone <pavone@retrodev.com>
parents:
241
diff
changeset
|
1186 this.symbols.declareVar(args[i].cleanName()); |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1187 args[i] = (argname.indexOf('->') < 0 ? '\tobject * ' : '\t') + argname + ' = va_arg(args, object *);\n'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1188 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1189 var compiled = [] |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1190 for (var i in exprs) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1191 var js = exprs[i].toC(); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1192 if (js) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1193 compiled.push(indent(js)); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1194 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1195 } |
172
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
1196 if (compiled.length) { |
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
1197 if (exprs[exprs.length - 1] instanceof assignment) { |
267 | 1198 compiled.push('return (object *)' + exprs[exprs.length - 1].symbol.toC() + ';'); |
172
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
1199 } else { |
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
1200 compiled[compiled.length-1] = 'return (object *)(' + compiled[compiled.length-1] + ');'; |
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
1201 } |
8d466c5a7dff
Fixed a few bugs. Improved the "symbol not found" error messages. Added a "signed?" method to integer objects
Mike Pavone <pavone@retrodev.com>
parents:
171
diff
changeset
|
1202 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1203 exprs = compiled; |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
1204 |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1205 if (Object.keys(this.symbols.closedover).length) { |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1206 if (!addedTypeDef) { |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1207 for (var key in this.symbols.closedover) { |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1208 print(key, ": ", this.symbols.closedover[key]); |
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1209 } |
156
d6e79885bd3b
Fix compiler bug involving referencing a self method in a method defined before the referenced method
Mike Pavone <pavone@retrodev.com>
parents:
155
diff
changeset
|
1210 throw new Error('this.symbols.closedover is not empty, but it was when compilation of ' + this.name + ' "' + assignPath + '" started'); |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1211 } |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
1212 forwarddec += 'struct ' + this.name + '_env {\n'; |
57
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
1213 if (this.symbols.needsParentEnv) { |
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
1214 forwarddec += '\tstruct ' + this.symbols.parentEnvType() + ' * parent;\n'; |
08ae75d90dc2
Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents:
55
diff
changeset
|
1215 } |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1216 for (var varname in this.symbols.closedover) { |
54 | 1217 if (varname == 'self' && this.selftype) { |
1218 forwarddec += '\tstruct ' + this.selftype + ' * self;\n'; | |
1219 } else { | |
1220 forwarddec += '\tobject * ' + escapeCName(varname) + ';\n'; | |
1221 } | |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1222 } |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
1223 forwarddec += '};\n' |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
1224 |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
1225 var myenvinit = '\t' + this.name + '_env * myenv = GC_MALLOC(sizeof(' + this.name + '_env));\n'; |
58
7b454d100dc8
Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents:
57
diff
changeset
|
1226 if (this.symbols.needsParentEnv) { |
7b454d100dc8
Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents:
57
diff
changeset
|
1227 myenvinit += '\tmyenv->parent = env;\n'; |
7b454d100dc8
Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents:
57
diff
changeset
|
1228 } |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
1229 this.symbols.envtype = this.name + '_env'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1230 } else { |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1231 var myenvinit = ''; |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1232 } |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
1233 forwarddec += 'object *' + this.name + ' (' + this.symbols.parentEnvType() + ' * env, uint32_t num_args, ...);\n'; |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
1234 |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1235 toplevelcode += '//' + assignPath + "\n"; |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
1236 toplevelcode += 'object * ' + this.name + ' ( ' + this.symbols.parentEnvType() + ' * env, uint32_t num_args, ...) {\n\tva_list args;\n' + myenvinit + '\tva_start(args, num_args);\n'; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1237 if (this.selftype) { |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1238 var selfvar = (new symbol('self', this.symbols)).toC(); |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1239 if (selfvar == 'self') { |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1240 toplevelcode += '\t' + this.selftype + ' * self = va_arg(args, ' + this.selftype + ' *);\n'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1241 } else { |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1242 toplevelcode += '\t' + selfvar + ' = va_arg(args, ' + this.selftype + ' *);\n'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1243 } |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
1244 |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1245 } |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1246 toplevelcode += args.join('') + '\tva_end(args);\n' + exprs.join(';\n\t') + '\n}\n'; |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
1247 |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
1248 if (this.selftype) { |
60
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
1249 return this.name; |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
1250 } else { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
1251 if (this.symbols.parentEnvType() != 'void') { |
55
93ddb4ad6fcb
Fix some nested closure bugs
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
1252 if (this.symbols.parent.passthruenv) { |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
1253 var envvar = 'env'; |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
1254 } else { |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
1255 var envvar = 'myenv'; |
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
1256 } |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
1257 debugprint('//' + this.name, 'has envvar:', envvar, 'num vars closed over:', Object.keys(this.symbols.closedover).length); |
60
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
1258 return 'make_lambda(' + envvar + ', (closure_func)' + this.name + ')'; |
121
1a4446f573d3
Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents:
97
diff
changeset
|
1259 } else { |
143
282b8056b702
Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents:
142
diff
changeset
|
1260 toplevelcode += 'lambda ' + this.name + '_obj = {{&lambda_meta, NULL}, NULL, ' + this.name + '};\n'; |
96
84b65ee8b78b
Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents:
95
diff
changeset
|
1261 return '((object *)&' + this.name + '_obj)'; |
34
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1262 } |
a10f1b049193
Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents:
32
diff
changeset
|
1263 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1264 }; |
60
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
1265 lambda.prototype.toCModuleInstance = function() { |
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
1266 this.toC(); |
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
1267 return this.name + '(NULL, 0)'; |
ef3b34c2c0a4
Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents:
59
diff
changeset
|
1268 }; |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1269 lambda.prototype.toCObject = function(typename) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1270 this.selftype = typename; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1271 return this.toC(); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1272 }; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1273 lambda.prototype.toCModule = function() { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1274 return makeCProg(this); |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1275 }; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1276 lambda.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1277 var lines = []; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1278 for (var i in this.args) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1279 var name = this.args[i].name; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1280 if (name[0] == ':') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1281 name = name.substr(1); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1282 } |
282
361a449a7235
Add some extra parens in addr_of translation and fix cleaning of names in llMessage parameters
Michael Pavone <pavone@retrodev.com>
parents:
273
diff
changeset
|
1283 if (name[0] >= "0" && name[0] <= "9") { |
361a449a7235
Add some extra parens in addr_of translation and fix cleaning of names in llMessage parameters
Michael Pavone <pavone@retrodev.com>
parents:
273
diff
changeset
|
1284 name = '_tp_' + name; |
361a449a7235
Add some extra parens in addr_of translation and fix cleaning of names in llMessage parameters
Michael Pavone <pavone@retrodev.com>
parents:
273
diff
changeset
|
1285 } |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1286 if(name != 'self') { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1287 lines.push(name + ' = va_arg(args, ' + vars[name] + ');'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1288 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1289 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1290 for (var i in this.expressions) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1291 var exprlines = this.expressions[i].toCLines(vars, needsreturn && i == this.expressions.length - 1); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1292 for (var j in exprlines) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1293 lines.push('\t' + exprlines[j]); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1294 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1295 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1296 return lines; |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1297 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1298 lambda.prototype.toCLLExpr = function(vars) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1299 if (this.expressions.length != 1) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1300 throw new Error('lambda in expression context must have a single statement in llMessage block'); |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1301 } |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1302 return this.expressions[0].toCLLExpr(vars); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1303 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1304 |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1305 assignment.prototype.toC = function() { |
54 | 1306 debugprint('//assignment', this.symbol.name); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1307 var existing = this.symbols.find(this.symbol.name); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1308 var prefix = ''; |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1309 assignNames.push(this.symbol.name); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1310 var val = this.expression.toC(); |
155
9de2572a34a7
Added some stuff for compiler debugging. Added unsigned integer types. Added integer size conversion methods to integer objects.
Mike Pavone <pavone@retrodev.com>
parents:
146
diff
changeset
|
1311 assignNames.pop(this.symbol.name); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1312 if (val === null) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1313 return null; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1314 } |
38 | 1315 if (existing.type == 'local' && !existing.isdeclared) { |
1316 prefix = 'object *'; | |
1317 this.symbols.declareVar(this.symbol.name); | |
54 | 1318 debugprint('//declared var', this.symbol.name); |
38 | 1319 } |
267 | 1320 var cast = ''; |
1321 if (this.symbol.name == 'self') { | |
1322 //ugly hack alert | |
1323 cast = '(void *)'; | |
1324 } else { | |
1325 cast = '(object *)'; | |
1326 } | |
1327 return prefix + this.symbol.toC() + ' = ' + cast + val; | |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1328 }; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1329 assignment.prototype.toCObject = function(cobj) { |
54 | 1330 debugprint('//message definition', this.symbol.name); |
156
d6e79885bd3b
Fix compiler bug involving referencing a self method in a method defined before the referenced method
Mike Pavone <pavone@retrodev.com>
parents:
155
diff
changeset
|
1331 assignNames.push('#' + this.symbol.name); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1332 if (this.expression.toCObject) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1333 var val = this.expression.toCObject(cobj.name); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1334 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1335 var val = this.expression.toC(); |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1336 } |
156
d6e79885bd3b
Fix compiler bug involving referencing a self method in a method defined before the referenced method
Mike Pavone <pavone@retrodev.com>
parents:
155
diff
changeset
|
1337 assignNames.pop(); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1338 if (val === null) { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1339 return; |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1340 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1341 if (this.expression instanceof lambda) { |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1342 var params = ['((object *)self)']; |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1343 var paramget = ''; |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
1344 var messagevars = {}; |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1345 for (var i in this.expression.args) { |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1346 var escaped = escapeCName(this.expression.args[i].cleanName()); |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1347 if (escaped != 'self') { |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
1348 messagevars[escaped] = 'object *'; |
35
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1349 params.push(escaped); |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1350 paramget += escaped + ' = va_arg(args, object *); '; |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1351 } |
bf5e88f6419d
Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents:
34
diff
changeset
|
1352 } |
122
9820ecd4eed4
Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents:
121
diff
changeset
|
1353 cobj.addMessage(getOpMethodName(this.symbol.name), { |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
1354 vars: messagevars, |
42
4e983fe32047
Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
1355 lines: [paramget + 'return ' + val + '(' + (cobj.hasenv ? 'self->env' : 'NULL') + ', ' + params.length + (params.length ? ', ' : '') + params.join(', ') + ');'] |
37
a6bf4869fcbe
Small refactor of built-in int32 type and added support for more operators on said type
Mike Pavone <pavone@retrodev.com>
parents:
35
diff
changeset
|
1356 }); |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1357 } else { |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1358 cobj.addProperty(this.symbol.name, val); |
59 | 1359 if (this.expression instanceof object && this.expression.symbols.needsparent) { |
72
ab6f24d6945d
Fix determination of whether a method call has an implicit self argument or not. Cleanup C warnings in output.
Mike Pavone <pavone@retrodev.com>
parents:
69
diff
changeset
|
1360 cobj.addInit('self->' + escapeCName(this.symbol.name) + '->parent = (object *)self;'); |
59 | 1361 } |
31
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1362 } |
668f533e5284
Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1363 }; |
84
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1364 assignment.prototype.toCLines = function(vars, needsreturn) { |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1365 return [(needsreturn ? 'return ' : '') + this.symbol.toCLLExpr(vars) + ' = ' + this.expression.toCLLExpr(vars) + ';'] |
9811040704ac
Add support for llMessage:withVars:andCode and llProperty:withType for specifying low level code without having to stick C inside the compiler. Redo array built-in type to use this feature.
Mike Pavone <pavone@retrodev.com>
parents:
79
diff
changeset
|
1366 }; |