annotate cbackend.js @ 171:869399ff7faa

Merge
author Mike Pavone <pavone@retrodev.com>
date Tue, 13 Aug 2013 22:01:00 -0700
parents 18598163e3ef 224071eed9dd
children 8d466c5a7dff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 nextmethodId = 0;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
5 var methodIds = {};
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)) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
10 methodIds[methodName] = nextmethodId++;
121
1a4446f573d3 Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 97
diff changeset
11
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 {
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 146
diff changeset
18 var optoMeth = {'+': 'ADD_', '-': 'SUB_', '*': 'MUL_', '/': 'DIV_', '%': 'MOD_',
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 146
diff changeset
19 '=': 'EQ_', '!=': 'NEQ_', '<': 'LT_', '>': 'GT_', '>=': 'GEQ_', '<=': 'LEQ_',
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 146
diff changeset
20 '.': 'CAT_', '&&':'if', '||':'ifnot', '|': 'CONS_'};
122
9820ecd4eed4 Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 121
diff changeset
21 if (opname in optoMeth) {
9820ecd4eed4 Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 121
diff changeset
22 return optoMeth[opname];
9820ecd4eed4 Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 121
diff changeset
23 } else {
9820ecd4eed4 Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 121
diff changeset
24 return opname;
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 }
9820ecd4eed4 Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 121
diff changeset
27
9820ecd4eed4 Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 121
diff changeset
28 op.prototype.toC = function(isReceiver) {
9820ecd4eed4 Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 121
diff changeset
29 var method = getOpMethodName(this.op);
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 146
diff changeset
30 if (this.op == '|') {
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.right.toC() + ', ' + this.left.toC() + ')\n';
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 146
diff changeset
32 } else {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 146
diff changeset
33 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
34 }
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
35 };
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
36 op.prototype.toCLLExpr = function(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
37 var opmap = {'=': '==', 'xor': '^'};
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
38 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
39 };
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 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
41 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
42 };
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
43
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
44
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
45 function escapeCName(name)
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
46 {
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
47 if (name == 'self') {
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
48 return name;
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
49 }
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 name = name.replace("_", "UN_").replace(":", "CN_").replace("!", "EX_").replace('?', 'QS_').replace('@', 'AT_');
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 name = 'tp_' + name;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 return name;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
55 function getSymbolPrefix(info, symbols)
42
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
56 {
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 var pre = '';
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
58 switch(info.type) {
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
59 case 'self':
121
1a4446f573d3 Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 97
diff changeset
60
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
61 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
62 break;
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
63 case 'parent':
57
08ae75d90dc2 Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents: 55
diff changeset
64 pre = (new symbol('self', symbols)).toC() + '->header.';
32
64f1d516fbfd Tiny bit of work on closures
Mike Pavone <pavone@retrodev.com>
parents: 31
diff changeset
65 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
66 pre += (i ? '->' : '') + 'parent';
32
64f1d516fbfd Tiny bit of work on closures
Mike Pavone <pavone@retrodev.com>
parents: 31
diff changeset
67 }
57
08ae75d90dc2 Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents: 55
diff changeset
68 pre = '((' + info.selftype + ' *)' + pre + ')->';
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
69 break;
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
70 case 'upvar':
32
64f1d516fbfd Tiny bit of work on closures
Mike Pavone <pavone@retrodev.com>
parents: 31
diff changeset
71 pre = 'env->';
57
08ae75d90dc2 Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents: 55
diff changeset
72 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
73 pre += 'parent->';
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 }
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
75 break;
42
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
76 case 'recupvar':
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
77 if (info.subtype == 'object') {
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
78 pre = 'self->env->';
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
79 } else {
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
80 //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
81 }
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
82 pre += getSymbolPrefix(info.parent);
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
83 case 'closedover':
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
84 pre = 'myenv->';
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
85 }
42
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
86 return pre;
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
87 }
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
88
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
89 symbol.prototype.toC = function() {
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
90 var name = this.cleanName();
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
91 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
92 if (!info) {
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
93 throw new Error('symbol ' + name + ' not found');
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
94 }
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
95 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
96 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
97 } else if (info.type == 'self' && 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 return 'mcall(' + getMethodId(name) + '/* ' + name + ' */, 1, ' + (new symbol('self', this.symbols)).toC() + ')';
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 } 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
100 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
101 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
102 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
103 }
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
104 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
105 }
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
106 return getSymbolPrefix(info, this.symbols) + escapeCName(name);
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
107 }
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
108 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
109 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
110 };
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
111 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
112 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
113 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
114 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
115 }
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 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
117 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
118 }
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 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
120 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
121 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
122 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
123 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
124 }
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 if (!info) {
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 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
127 }
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 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
129 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
130 } 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
131 if (info.isll || !(info.def instanceof lambda)) {
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
132 return 'self->' + 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
133 } 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
134 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
135 }
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
136 }
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 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
138 };
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 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
140 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
141 };
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
142
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
143 var declaredInts = {};
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
144
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
145 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
146 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
147 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
148 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
149 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
150 declaredInts[str] = true;
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
151 }
135
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
152 return '((object *)&' + str + ')';
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
153 }
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
154 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
155 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
156 };
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
157 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
158 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
159 };
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
160
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
161 floatlit.prototype.toC = function() {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
162 return 'make_float(' + this.val.toString() + ')';
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 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
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 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
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
41
0558dad9d061 Add basic string support
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
171 var declaredStrings = {};
0558dad9d061 Add basic string support
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
172 var nextStringId = 0;
0558dad9d061 Add basic string support
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
173
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
174 strlit.prototype.toC = function() {
41
0558dad9d061 Add basic string support
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
175 if (!(this.val in declaredStrings)) {
0558dad9d061 Add basic string support
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
176 //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
177 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
0558dad9d061 Add basic string support
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
178 declaredStrings[this.val] = nextStringId++;
0558dad9d061 Add basic string support
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
179 }
0558dad9d061 Add basic string support
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
180 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
181 };
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 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
183 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
184 };
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 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
186 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
187 };
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
188
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
189 listlit.prototype.toC = function() {
40
927fd7911a01 Add append message to array
Mike Pavone <pavone@retrodev.com>
parents: 39
diff changeset
190 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
191 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
192 ret += ', ' + this.val[i].toC();
38
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
193 }
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
194 return ret + ')';
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
195 }
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
196
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
197 arraylit.prototype.toC = function() {
40
927fd7911a01 Add append message to array
Mike Pavone <pavone@retrodev.com>
parents: 39
diff changeset
198 var ret = 'make_array(' + this.val.length;
38
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
199 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
200 ret += ', ' + this.val[i].toC();
38
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
201 }
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
202 return ret + ')';
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
203 }
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 funcall.prototype.toC = function() {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
206 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
207 if (name == 'foreign') {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
208 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
209 return null;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
210 } else if(this.args[0] instanceof symbol) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
211 return this.args[0].name;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
212 } else {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
213 throw new Error("Unexpected AST type for foreign:");
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
214 }
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
215 } else if(name == 'llProperty:withType' || name == 'llProperty:withVars:andCode') {
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
216 return null;
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
217 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
218 var args = this.args.slice(0, this.args.length);
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
219 if (this.receiver) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
220 args.splice(0, 0, this.receiver);
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
221 }
42
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
222 var method = false;
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
223 var funinfo = this.symbols.find(name);
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
224 var start = 0;
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
225 if (!funinfo || funinfo.def instanceof setter || funinfo.type == 'toplevel') {
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
226 method = true;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
227 } else {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
228 switch(funinfo.type)
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
229 {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
230 case 'self':
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
231 case 'parent':
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
232 var defargs = funinfo.def.args.length;
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
233 if (!defargs || funinfo.def.args[0].name != 'self') {
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
234 defargs ++
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
235 }
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
236 if (args.length < defargs) {
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
237 if (funinfo.type == 'self') {
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
238 args.splice(0, 0, new symbol('self', this.symbols));
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
239 } else {
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
240 var obj = (new symbol('self', this.symbols)).toC() + '->header.';
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
241 for (var i = 0; i < funinfo.depth; ++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
242 obj += (i ? '->' : '') + 'parent';
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
243 }
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
244 args.splice(0, 0, ', ' + obj);
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
245 start = 1;
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
246 }
137
101fa04ee9e1 Fix calling methods on objects that are not self when self implements the method being called
Mike Pavone <pavone@retrodev.com>
parents: 135
diff changeset
247 } else if(!(args[0] instanceof symbol) || args[0].name != 'self') {
101fa04ee9e1 Fix calling methods on objects that are not self when self implements the method being called
Mike Pavone <pavone@retrodev.com>
parents: 135
diff changeset
248 funinfo = null;
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
249 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
250 method = true;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
251 break;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
252 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
253 }
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
254 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
255 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
256 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
257 var callpart;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
258 if (method) {
96
84b65ee8b78b Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents: 95
diff changeset
259 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
260 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
261 } else {
84b65ee8b78b Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents: 95
diff changeset
262 callpart = 'mcall(' + getMethodId(name) + '/* ' + name + ' */';
84b65ee8b78b Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents: 95
diff changeset
263 }
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
264 } else {
55
93ddb4ad6fcb Fix some nested closure bugs
Mike Pavone <pavone@retrodev.com>
parents: 54
diff changeset
265 callpart = 'ccall(' + (new symbol(name, this.symbols)).toC();
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
266 }
35
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
267 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
268 };
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
269 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
270 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
271 {
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
272 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
273 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
274 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
275 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
276 break;
143
282b8056b702 Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents: 142
diff changeset
277 case 'struct:':
282b8056b702 Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents: 142
diff changeset
278 case 'struct':
282b8056b702 Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents: 142
diff changeset
279 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
280 return 'struct ' + receiver.toCTypeName();
282b8056b702 Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents: 142
diff changeset
281 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
282 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
283 throw new Error('invalid use of funcall expression where a C type name is expected');
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
284 }
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
285 };
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
286 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
287 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
288 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
289 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
290 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
291 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
292 }
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
293 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
294 {
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
295 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
296 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
297 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
298 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
299 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
300 }
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
301 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
302 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
303 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
304 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
305 } 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
306 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
307 }
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
308 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
309 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
310 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
311 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
312 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
313 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
314 }
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
315 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
316 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
317 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
318 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
319 }
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
320 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
321 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
322 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
323 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
324 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
325 }
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 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
327 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
328 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
329 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
330 }
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 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
332 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
333 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
334 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
335 }
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 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
337 };
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 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
340 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
341 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
342 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
343 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
344 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
345 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
346 } else {
282b8056b702 Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents: 142
diff changeset
347 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
348 }
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
349 } 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
350 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
351 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
352 } else {
282b8056b702 Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents: 142
diff changeset
353 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
354 }
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
355 } 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
356 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
357 }
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 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
360 {
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 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
362 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
363 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
364 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
365 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
366 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
367 case 'addr_of':
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 return '&(' + 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
369 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
370 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
371 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
372 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
373 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
374 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
375 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
376 return '!(' + 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
377 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
378 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
379 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
380 }
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
381 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
382 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
383 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
384 }
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 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
386 }
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 };
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
388
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
389 function cObject(name) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
390 this.name = name;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
391 this.slots = {};
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
392 this.properties = [];
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
393 this.values = [];
35
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
394 this.slotvars = {};
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
395 this.includes = {};
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
396 this.parent = 'NULL';
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
397 this.init = [];
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
398 this.initmsgadded = false;
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
399 }
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
400
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
401 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
402 this.includes[includefile] = true;
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
403 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
404
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
405 cObject.prototype.addMessage = function(msgname, implementation) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
406 var methodid = getMethodId(msgname);
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
407 var trunc = methodid & 0xF;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
408 if (!(trunc in this.slots)) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
409 this.slots[trunc] = [];
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
410 }
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
411 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
412 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
413 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
414 }
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
415 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
416 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
417 }
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
418 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
419
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
420 cObject.prototype.addProperty = function(propname, value, type) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
421 if (type != undefined) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
422 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
423 if (value !== null) {
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
424 this.values.push(value);
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
425 }
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
426 } else {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
427 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
428 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
429 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
430 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
431 'return self->' + escaped + ';'
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
432 ]});
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
433 this.addMessage(propname + '!', {
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
434 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
435 lines: [
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
436 '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
437 '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
438 ]});
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
439 this.properties.push(escaped);
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
440 this.values.push(value);
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
441 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
442 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
443
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
444 cObject.prototype.addInit = function(statement) {
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
445 this.init.push(statement);
140
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
446 };
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
447
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
448 cObject.prototype.addImport = function(symbols, source) {
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
449 this.imported.push(source);
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
450 var importNum = imported.length - 1;
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
451 if (symbols) {
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
452 each(symbols, function(i, sym) {
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
453 this.addMessage(sym.name, {
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
454 vars: {},
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
455 lines: [
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
456 'return self->import_' + importNum + '->meta->meth_lookup[method_id & 0xF](method_id, num_args, self->import_' + importNum + ', args);'
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
457 ]
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
458 });
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
459 });
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
460 } else {
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
461 //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
462 }
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
463 };
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
464
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
465 cObject.prototype.checkInitMsg = function() {
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
466 if (!this.initmsgadded && this.init.length) {
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
467 var init = this.init.slice(0, this.init.length);
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
468 init.push('return (object *)self;');
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
469 this.addMessage('_init', {
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
470 vars: {},
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
471 lines: init
121
1a4446f573d3 Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 97
diff changeset
472 });
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
473 this.initmsgadded = true;
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
474 }
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
475 }
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
476
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
477 cObject.prototype.populateSymbols = function() {};
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
478
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
479 cObject.prototype.toEarlyCDef = function() {
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
480 this.checkInitMsg();
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
481 var includes = '';
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
482 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
483 includes += '#include ' + file + '\n';
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
484 }
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
485 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
486 for (var i in this.properties) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
487 if (this.properties[i] instanceof Array) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
488 objdef += '\t' + this.properties[i][1] + ' ' + this.properties[i][0] + ';\n';
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
489 } else {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
490 objdef += '\tobject * ' + this.properties[i] + ';\n'
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
491 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
492 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
493 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
494 return includes + objdef;
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
495 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
496
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
497 cObject.prototype.toCDef = function() {
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
498 this.checkInitMsg();
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
499 var slotdefs = '';
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
500 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
501 for (var i = 0; i < 16; i++) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
502 if (i) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
503 metadef += ', ';
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
504 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
505 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
506 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
507 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
508 for (var varname in this.slotvars[i]) {
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
509 slotdefs += '\t' + this.slotvars[i][varname] + ' ' + varname + ';\n';
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
510 }
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
511 if (this.slots[i].length == 1) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
512 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
513 '\t\t' + this.slots[i][0][1] + '\n' +
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
514 '\t}\n' +
35
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
515 '\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
516 } else {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
517 slotdefs += '\tswitch(method_id) {\n';
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
518 for (j in this.slots[i]) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
519 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
520 '\t\t\t' + this.slots[i][j][1] + '\n';
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
521 }
35
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
522 slotdefs += '\t\tdefault:\n' +
57
08ae75d90dc2 Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents: 55
diff changeset
523 '\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
524
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
525 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
526 metadef += this.name + '_slot_' + i;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
527 } else {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
528 metadef += 'no_impl';
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
529 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
530 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
531 metadef += '}};\n';
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
532 return slotdefs + metadef;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
533 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
534
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
535 cObject.prototype.toCInstance = function() {
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
536 this.checkInitMsg();
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
537 var ret = 'make_object(&' + this.name + '_meta, ' + this.parent + ', ' + this.values.length + (this.values.length ? ', ' : '') + this.values.join(', ') + ')';
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
538 if (this.initmsgadded) {
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
539 ret = 'mcall(' + getMethodId('_init') + ', 1, ' + ret + ')'
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
540 }
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
541 return ret;
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
542 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
543
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
544 cObject.prototype.toC = function() {
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
545 forwarddec += this.toEarlyCDef();
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
546 toplevelcode += this.toCDef();
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
547 return this.toCInstance();
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
548 }
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
549
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
550 var nextobject = 0;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
551
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
552
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
553 object.prototype.toCObject = function() {
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
554 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
555 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
556 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
557 }
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
558 var me = new cObject(this.name);
57
08ae75d90dc2 Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents: 55
diff changeset
559 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
560 if (this.symbols.needsenv) {
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
561 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
562 me.hasenv = true;
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
563 }
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
564 if (this.symbols.needsparent && !(this.symbols.parent instanceof osymbols)) {
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
565 me.parent = '(object *)(' + (new symbol('self', this.symbols.parent)).toC() + ')';
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
566 }
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
567 for (var i in messages) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
568 if (messages[i] instanceof funcall) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
569 if (messages[i].name == '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
570 me.addImport(false, messages[i].args[0]);
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
571 } else if(messages[i].name == 'import:from:' && messages[i].args.length == 2) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
572 var importsyms = [];
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
573 each(messages[i].args[0].val, function(i, el) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
574 if (!(el instanceof symbol)) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
575 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
576 }
140
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
577 importsyms.push(el);
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
578 });
140
bf8f75b69048 Minor progress towards supporting imports in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 122
diff changeset
579 me.addImport(importsyms, messages[i].args[1]);
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
580 } else if(messages[i].name == 'llProperty:withType:' && messages[i].args.length == 2) {
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
581 me.addProperty(messages[i].args[0].name, null, messages[i].args[1].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
582 } else if(messages[i].name == 'llMessage:withVars:andCode:' && messages[i].args.length == 3) {
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
583 var msgname = messages[i].args[0].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
584 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
585 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
586 for(var v in rawvars) {
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
587 vars[rawvars[v].symbol.name] = rawvars[v].expression.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
588 }
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
589 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
590 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
591 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
592 });
143
282b8056b702 Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents: 142
diff changeset
593 } else if(messages[i].name == 'includeSystemHeader:' && messages[i].args.length == 1) {
282b8056b702 Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents: 142
diff changeset
594 me.addInclude("<" + messages[i].args[0].val + ">");
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
595 } else {
121
1a4446f573d3 Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 97
diff changeset
596
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
597 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
598 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
599 } else {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
600 messages[i].toCObject(me);
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 }
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
603
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
604 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
605 };
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
606
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
607 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
608 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
609 };
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
610
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
611 var toplevelcode;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
612 var forwarddec;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
613
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
614 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
615 {
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
616 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
617 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
618 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
619 '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
620 '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
621 '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
622 '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
623 ]
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
624 });
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
625 }
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
626
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
627 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
628 {
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
629 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
630 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
631 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
632 '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
633 '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
634 ' 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
635 '}',
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
636 '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
637 ]
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
638 });
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
639 }
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
640
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
641 function makeInt(bits, unsigned)
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
642 {
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
643 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
644 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
645 intObj.addProperty('num', null, (unsigned ? 'u' : '') + 'int' + bits +'_t');
135
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
646 addBinaryOp(intObj, 'ADD_', '+', typename);
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
647 addBinaryOp(intObj, 'SUB_', '-', typename);
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
648 addBinaryOp(intObj, 'MUL_', '*', typename);
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
649 addBinaryOp(intObj, 'DIV_', '/', typename);
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
650 addBinaryOp(intObj, 'MOD_', '%', typename);
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
651 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
652 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
653 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
654 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
655 addBinaryOp(intObj, 'rshift:by', '>>', typename);
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
656 addCompOp(intObj, 'LT_', '<', typename);
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
657 addCompOp(intObj, 'GT_', '>', typename);
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
658 addCompOp(intObj, 'EQ_', '==', typename);
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
659 addCompOp(intObj, 'NEQ_', '!=', typename);
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
660 addCompOp(intObj, 'GEQ_', '>=', typename);
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
661 addCompOp(intObj, 'LEQ_', '<=', typename);
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
662 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
663 //-9223372036854775808
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
664 //01234567890123456789
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
665 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
666 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
667 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
668 '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
669 '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
670 '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
671 '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
672 '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
673 ]
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
674 });
135
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
675 //7FFFFFFFFFFFFFFF
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
676 //01234567890123456789
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
677 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
678 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
679 lines: [
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
680 '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
681 '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
682 '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
683 '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
684 '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
685 ]
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
686 });
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
687 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
688 vars: {},
1a4446f573d3 Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 97
diff changeset
689 lines: [
1a4446f573d3 Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 97
diff changeset
690 '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
691 ]
1a4446f573d3 Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 97
diff changeset
692 });
135
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
693 intObj.addMessage('hash', {
79
7f635666c73d Add hash and int32 messages to string. Add hash message to int32. Update compile script
Mike Pavone <pavone@retrodev.com>
parents: 78
diff changeset
694 vars: {},
7f635666c73d Add hash and int32 messages to string. Add hash message to int32. Update compile script
Mike Pavone <pavone@retrodev.com>
parents: 78
diff changeset
695 lines: [
7f635666c73d Add hash and int32 messages to string. Add hash message to int32. Update compile script
Mike Pavone <pavone@retrodev.com>
parents: 78
diff changeset
696 'return &(self->header);'
7f635666c73d Add hash and int32 messages to string. Add hash message to int32. Update compile script
Mike Pavone <pavone@retrodev.com>
parents: 78
diff changeset
697 ]
7f635666c73d Add hash and int32 messages to string. Add hash message to int32. Update compile script
Mike Pavone <pavone@retrodev.com>
parents: 78
diff changeset
698 });
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
699 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
700 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
701 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
702 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
703 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
704 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
705 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
706 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
707 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
708 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
709 '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
710 ]
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
711 });
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
712 } 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
713 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
714 var retName = 'ret' + (uns ? 'u' : '') + size;
fc8eecad71e6 Fix variable name collision in integer objects
Mike Pavone <pavone@retrodev.com>
parents: 156
diff changeset
715 var vars = {};
fc8eecad71e6 Fix variable name collision in integer objects
Mike Pavone <pavone@retrodev.com>
parents: 156
diff changeset
716 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
717 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
718
161
fc8eecad71e6 Fix variable name collision in integer objects
Mike Pavone <pavone@retrodev.com>
parents: 156
diff changeset
719 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
720 lines: [
161
fc8eecad71e6 Fix variable name collision in integer objects
Mike Pavone <pavone@retrodev.com>
parents: 156
diff changeset
721 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
722 retName + '->num = self->num;',
fc8eecad71e6 Fix variable name collision in integer objects
Mike Pavone <pavone@retrodev.com>
parents: 156
diff changeset
723 '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
724 ]
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
725 });
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
726 }
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
727 }
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
728 }
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
729
135
f98790d8a53d Add int64, int16 and int8. Also add hex method to integer types.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
730 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
731 }
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
732
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
733 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
734 {
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
735 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
736 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
737 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
738 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
739 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
740 }
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
741
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
742 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
743 {
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
744 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
745 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
746 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
747 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
748 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
749 }
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
750
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
751 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
752 {
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
753 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
754 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
755 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
756 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
757 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
758 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
759 '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
760 '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
761 '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
762 ' 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
763 '}',
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
764 '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
765 ]
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
766 });
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
767 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
768 }
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
769
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
770 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
771 {
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
772 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
773 makeInt(64, true) , makeInt(32, true), makeInt(16, true), makeInt(8, 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
774 makeArray(), makeString(), makelambda()];
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
775 }
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
776
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
777 function addBuiltinModules(toplevel)
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
778 {
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
779 var os = new cObject('mod_obj_os');
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
780 os.addInclude('<sys/stat.h>');
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
781 os.addInclude('<fcntl.h>');
162
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
782 os.addInclude('<stdlib.h>');
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
783 os.addInclude('<time.h>');
168
224071eed9dd Add os sleep
Mike Pavone <pavone@retrodev.com>
parents: 162
diff changeset
784 os.addInclude('<unistd.h>');
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
785 os.addMessage('write', {
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
786 vars: {str: 'string *', intret: 'obj_int32 *', filedes: 'obj_int32 *'},
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
787 lines: [
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
788 'filedes = va_arg(args, obj_int32 *);',
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
789 'str = va_arg(args, string *);',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
790 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
791 'intret->num = write(filedes->num, str->data, str->bytes);',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
792 'return &(intret->header);'
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
793 ]
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
794 });
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
795 os.addMessage('read', {
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
796 vars: {str: 'string *', size: 'obj_int32 *', filedes: 'obj_int32 *'},
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
797 lines: [
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
798 'filedes = va_arg(args, obj_int32 *);',
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
799 'size = va_arg(args, obj_int32 *);',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
800 'str = (string *)make_object(&string_meta, NULL, 0);',
78
abc6f3d644a4 Use Boehm-GC for garbage collection. Also make no_impl print on stderr rather than standard in and return a non-zero error code.
Mike Pavone <pavone@retrodev.com>
parents: 72
diff changeset
801 'str->data = GC_MALLOC_ATOMIC(size->num + 1);',
162
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
802 'str->len = str->bytes = read(filedes->num, str->data, size->num);',
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
803 'if (str->bytes < 0) { str->bytes = str->len = 0; }',
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
804 'str->data[str->bytes] = 0;',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
805 'return &(str->header);'
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
806 ]
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
807 });
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
808 os.addMessage('open', {
49
f2cda2e6f70e Fix os open to optionally take a file permission bit parameter. Update example to use this parameter. Add support for hex and binary integer literals
Mike Pavone <pavone@retrodev.com>
parents: 48
diff changeset
809 vars: {str: 'string *', flags: 'obj_int32 *', filedes: 'obj_int32 *', perm: 'obj_int32 *'},
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
810 lines: [
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
811 'str = va_arg(args, string *);',
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
812 'flags = va_arg(args, obj_int32 *);',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
813 'filedes = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
49
f2cda2e6f70e Fix os open to optionally take a file permission bit parameter. Update example to use this parameter. Add support for hex and binary integer literals
Mike Pavone <pavone@retrodev.com>
parents: 48
diff changeset
814 'if (num_params == 3) {',
f2cda2e6f70e Fix os open to optionally take a file permission bit parameter. Update example to use this parameter. Add support for hex and binary integer literals
Mike Pavone <pavone@retrodev.com>
parents: 48
diff changeset
815 ' filedes->num = open(str->data, flags->num);',
f2cda2e6f70e Fix os open to optionally take a file permission bit parameter. Update example to use this parameter. Add support for hex and binary integer literals
Mike Pavone <pavone@retrodev.com>
parents: 48
diff changeset
816 '} else if (num_params == 4) {',
f2cda2e6f70e Fix os open to optionally take a file permission bit parameter. Update example to use this parameter. Add support for hex and binary integer literals
Mike Pavone <pavone@retrodev.com>
parents: 48
diff changeset
817 ' perm = va_arg(args, obj_int32 *);',
f2cda2e6f70e Fix os open to optionally take a file permission bit parameter. Update example to use this parameter. Add support for hex and binary integer literals
Mike Pavone <pavone@retrodev.com>
parents: 48
diff changeset
818 ' filedes->num = open(str->data, flags->num, perm->num);',
f2cda2e6f70e Fix os open to optionally take a file permission bit parameter. Update example to use this parameter. Add support for hex and binary integer literals
Mike Pavone <pavone@retrodev.com>
parents: 48
diff changeset
819 '} else {',
f2cda2e6f70e Fix os open to optionally take a file permission bit parameter. Update example to use this parameter. Add support for hex and binary integer literals
Mike Pavone <pavone@retrodev.com>
parents: 48
diff changeset
820 ' filedes->num = -1;',
f2cda2e6f70e Fix os open to optionally take a file permission bit parameter. Update example to use this parameter. Add support for hex and binary integer literals
Mike Pavone <pavone@retrodev.com>
parents: 48
diff changeset
821 '}',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
822 'return &(filedes->header);'
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
823 ]
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
824 });
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
825 os.addMessage('close', {
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
826 vars: {filedes: 'obj_int32 *', intret: 'obj_int32 *'},
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
827 lines: [
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
828 'filedes = va_arg(args, obj_int32 *);',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
829 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
830 'intret->num = close(filedes->num);',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
831 'return &(intret->header);'
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
832 ]
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
833 });
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
834 os.addMessage('O_RDONLY', {
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
835 vars: {intret: 'obj_int32 *'},
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
836 lines: [
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
837 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
838 'intret->num = O_RDONLY;',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
839 'return &(intret->header);'
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
840 ]
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
841 });
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
842 os.addMessage('O_WRONLY', {
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
843 vars: {intret: 'obj_int32 *'},
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
844 lines: [
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
845 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
846 'intret->num = O_WRONLY;',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
847 'return &(intret->header);'
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
848 ]
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
849 });
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
850 os.addMessage('O_RDWR', {
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
851 vars: {intret: 'obj_int32 *'},
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
852 lines: [
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
853 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
854 'intret->num = O_RDWR;',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
855 'return &(intret->header);'
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
856 ]
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
857 });
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
858 os.addMessage('O_CREAT', {
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
859 vars: {intret: 'obj_int32 *'},
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
860 lines: [
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
861 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
862 'intret->num = O_CREAT;',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
863 'return &(intret->header);'
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
864 ]
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
865 });
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
866 os.addMessage('O_APPEND', {
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
867 vars: {intret: 'obj_int32 *'},
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
868 lines: [
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
869 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
870 'intret->num = O_APPEND;',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
871 'return &(intret->header);'
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
872 ]
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
873 });
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
874 os.addMessage('O_TRUNC', {
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
875 vars: {intret: 'obj_int32 *'},
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
876 lines: [
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
877 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
878 'intret->num = O_TRUNC;',
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
879 'return &(intret->header);'
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
880 ]
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
881 });
162
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
882 os.addMessage('rand', {
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
883 vars: {intret: 'obj_int32 *'},
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
884 lines: [
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
885 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
886 'intret->num = rand();',
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
887 'return &(intret->header);'
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
888 ]
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
889 });
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
890 os.addMessage('rand64', {
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
891 vars: {intret64: 'obj_int64 *'},
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
892 lines: [
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
893 'intret64 = (obj_int64 *)make_object(&obj_int64_meta, NULL, 0);',
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
894 'intret64->num = (((int64_t)rand()) << 32 ) | rand();',
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
895 'return &(intret64->header);'
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
896 ]
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
897 });
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
898 os.addMessage('srand', {
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
899 vars: {oseed: 'object *', seed: 'obj_int32 *'},
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
900 lines: [
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
901 'oseed = va_arg(args, object *);',
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
902 'seed = mcall(' + getMethodId("int32") + ', 1, oseed);',
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
903 'srand(seed->num);',
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
904 'return &(seed->header);'
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
905 ]
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
906 });
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
907 os.addMessage('time', {
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
908 vars: {intret64: 'obj_int64 *'},
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
909 lines: [
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
910 'intret64 = (obj_int64 *)make_object(&obj_int64_meta, NULL, 0);',
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
911 'intret64->num = time(NULL);',
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
912 'return &(intret64->header);'
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
913 ]
157cc497a7f1 Add rand srand and time to os module
Mike Pavone <pavone@retrodev.com>
parents: 161
diff changeset
914 });
168
224071eed9dd Add os sleep
Mike Pavone <pavone@retrodev.com>
parents: 162
diff changeset
915 os.addMessage('sleep', {
224071eed9dd Add os sleep
Mike Pavone <pavone@retrodev.com>
parents: 162
diff changeset
916 vars: {osecs: 'object *', secs: 'obj_int32 *', intret: 'obj_int32 *'},
224071eed9dd Add os sleep
Mike Pavone <pavone@retrodev.com>
parents: 162
diff changeset
917 lines: [
224071eed9dd Add os sleep
Mike Pavone <pavone@retrodev.com>
parents: 162
diff changeset
918 'osecs = va_arg(args, object *);',
224071eed9dd Add os sleep
Mike Pavone <pavone@retrodev.com>
parents: 162
diff changeset
919 'secs = mcall(' + getMethodId("int32") + ', 1, osecs);',
224071eed9dd Add os sleep
Mike Pavone <pavone@retrodev.com>
parents: 162
diff changeset
920 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
224071eed9dd Add os sleep
Mike Pavone <pavone@retrodev.com>
parents: 162
diff changeset
921 'intret->num = sleep(secs->num);',
224071eed9dd Add os sleep
Mike Pavone <pavone@retrodev.com>
parents: 162
diff changeset
922 'return &(intret->header);'
224071eed9dd Add os sleep
Mike Pavone <pavone@retrodev.com>
parents: 162
diff changeset
923 ]
224071eed9dd Add os sleep
Mike Pavone <pavone@retrodev.com>
parents: 162
diff changeset
924 });
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
925 toplevel.names['os'] = os;
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
926 }
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
927
66
25b697c91629 Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents: 61
diff changeset
928 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
929 return this.ast.toCModuleInstance();
66
25b697c91629 Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents: 61
diff changeset
930 };
25b697c91629 Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents: 61
diff changeset
931
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
932 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
933 {
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 146
diff changeset
934 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
935 var ret = '';
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
936 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
937 var 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
938 for (var i in alwaysused) {
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
939 toplevel.used[alwaysused[i]] = true;
66
25b697c91629 Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents: 61
diff changeset
940 }
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
941 var newused = Object.keys(toplevel.used);
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
942 var allused = newused;
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
943 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
944 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
945 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
946 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
947 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
948 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
949 }
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
950 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
951 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
952 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
953 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
954 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
955 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
956 }
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
957 }
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
958 }
121
1a4446f573d3 Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 97
diff changeset
959
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
960 for (var i = allused.length-1; i >= 0; i--) {
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
961 var symbol = allused[i];
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
962 debugprint('//---module', symbol, '(' + i +')--- compile');
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
963 assignNames.push(symbol);
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
964 ret += '\t' + toplevel.moduleVar(symbol) + ' = ' + toplevel.names[symbol].toC() + ';\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
965 assignNames.pop();
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
966 }
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
967 return ret;
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
968 }
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
969
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
970 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
971 {
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
972 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
973 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
974 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
975 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
976 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
977 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
978 }
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
979 addBuiltinModules(toplevel);
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
980 debugprint('//------POPULATING SYMBOLS-----');
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
981 obj.populateSymbols(toplevel);
48
18ab96287c3a Add builtin module os containing some baisc POSIX file IO
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
982 var moduleinit = processUsedToplevel(toplevel);
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
983 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
984 var rest = 'object * mainModule() {\n' + moduleinit + '\tmain_module = ' + obj.toCModuleInstance() + ';\n\treturn main_module;\n}\n';
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
985 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
986 '#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
987 '#define METHOD_ID_EMPTY ' + getMethodId('empty') + '\n' +
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 146
diff changeset
988 '#define METHOD_ID_CONS ' + getMethodId(getOpMethodName('|')) + '\n' +
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
989 forwarddec + toplevelcode + rest + '#include "runtime/progfoot.inc"\n';
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
990 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
991
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
992 object.prototype.toCModule = function() {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
993 return makeCProg(this);
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
994 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
995
60
ef3b34c2c0a4 Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents: 59
diff changeset
996 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
997 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
998 }
ef3b34c2c0a4 Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents: 59
diff changeset
999
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1000 lambda.prototype.toC = function() {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1001 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
1002 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
1003 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
1004 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
1005 var addedTypeDef = false;
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
1006 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
1007 this.symbols.envtype = this.name + '_env';
84b65ee8b78b Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents: 95
diff changeset
1008 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
1009 var addedTypeDef = true;
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
1010 }
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1011 if (this.selftype) {
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
1012 this.symbols.defineVar('self', this.selftype);
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1013 if (args[0] && args[0].cleanName() == 'self') {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1014 args.splice(0, 1);
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1015 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1016 var offset = 1;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1017 } else {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1018 var offset = 0;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1019 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1020 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
1021 var argname = args[i].toC();
121
1a4446f573d3 Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 97
diff changeset
1022
35
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
1023 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
1024 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1025 var compiled = []
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1026 for (var i in exprs) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1027 var js = exprs[i].toC();
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1028 if (js) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1029 compiled.push(indent(js));
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1030 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1031 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1032 exprs = compiled;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1033 if (exprs.length) {
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
1034 exprs[exprs.length-1] = 'return (object *)(' + exprs[exprs.length-1] + ');';
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1035 }
121
1a4446f573d3 Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 97
diff changeset
1036
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
1037 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
1038 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
1039 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
1040 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
1041 }
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
1042 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
1043 }
96
84b65ee8b78b Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents: 95
diff changeset
1044 forwarddec += 'struct ' + this.name + '_env {\n';
57
08ae75d90dc2 Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents: 55
diff changeset
1045 if (this.symbols.needsParentEnv) {
08ae75d90dc2 Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents: 55
diff changeset
1046 forwarddec += '\tstruct ' + this.symbols.parentEnvType() + ' * parent;\n';
08ae75d90dc2 Add != operator. Fix more closure bugs.
Mike Pavone <pavone@retrodev.com>
parents: 55
diff changeset
1047 }
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
1048 for (var varname in this.symbols.closedover) {
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
1049 if (varname == 'self' && this.selftype) {
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
1050 forwarddec += '\tstruct ' + this.selftype + ' * self;\n';
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
1051 } else {
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
1052 forwarddec += '\tobject * ' + escapeCName(varname) + ';\n';
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
1053 }
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
1054 }
96
84b65ee8b78b Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents: 95
diff changeset
1055 forwarddec += '};\n'
121
1a4446f573d3 Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 97
diff changeset
1056
96
84b65ee8b78b Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents: 95
diff changeset
1057 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
1058 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
1059 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
1060 }
96
84b65ee8b78b Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents: 95
diff changeset
1061 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
1062 } else {
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
1063 var myenvinit = '';
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
1064 }
96
84b65ee8b78b Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents: 95
diff changeset
1065 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
1066
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
1067 toplevelcode += '//' + assignPath + "\n";
96
84b65ee8b78b Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents: 95
diff changeset
1068 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
1069 if (this.selftype) {
34
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
1070 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
1071 if (selfvar == 'self') {
35
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
1072 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
1073 } else {
35
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
1074 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
1075 }
121
1a4446f573d3 Add isInteger? method to the int32 type in the C backend
Mike Pavone <pavone@retrodev.com>
parents: 97
diff changeset
1076
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1077 }
35
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
1078 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
1079
42
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
1080 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
1081 return this.name;
42
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
1082 } else {
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
1083 if (this.symbols.parentEnvType() != 'void') {
55
93ddb4ad6fcb Fix some nested closure bugs
Mike Pavone <pavone@retrodev.com>
parents: 54
diff changeset
1084 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
1085 var envvar = 'env';
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
1086 } else {
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
1087 var envvar = 'myenv';
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
1088 }
96
84b65ee8b78b Optimize self method calls into static function calls
Mike Pavone <pavone@retrodev.com>
parents: 95
diff changeset
1089 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
1090 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
1091 } else {
143
282b8056b702 Lambda bugfix and some improvments to the llMessage support
Mike Pavone <pavone@retrodev.com>
parents: 142
diff changeset
1092 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
1093 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
1094 }
a10f1b049193 Working closures, but need to rethink method call strategy
Mike Pavone <pavone@retrodev.com>
parents: 32
diff changeset
1095 }
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1096 };
60
ef3b34c2c0a4 Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents: 59
diff changeset
1097 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
1098 this.toC();
ef3b34c2c0a4 Fix populatesymbols for parent property references. Fix lambda-style modules in cbackend.
Mike Pavone <pavone@retrodev.com>
parents: 59
diff changeset
1099 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
1100 };
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1101 lambda.prototype.toCObject = function(typename) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1102 this.selftype = typename;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1103 return this.toC();
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1104 };
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1105 lambda.prototype.toCModule = function() {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1106 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
1107 };
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
1108 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
1109 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
1110 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
1111 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
1112 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
1113 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
1114 }
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
1115 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
1116 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
1117 }
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
1118 }
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
1119 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
1120 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
1121 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
1122 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
1123 }
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
1124 }
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
1125 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
1126 }
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
1127 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
1128 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
1129 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
1130 }
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
1131 return this.expressions[0].toCLLExpr(vars);
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1132 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1133
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1134 assignment.prototype.toC = function() {
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
1135 debugprint('//assignment', this.symbol.name);
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1136 var existing = this.symbols.find(this.symbol.name);
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1137 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
1138 assignNames.push(this.symbol.name);
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1139 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
1140 assignNames.pop(this.symbol.name);
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1141 if (val === null) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1142 return null;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1143 }
38
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
1144 if (existing.type == 'local' && !existing.isdeclared) {
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
1145 prefix = 'object *';
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
1146 this.symbols.declareVar(this.symbol.name);
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
1147 debugprint('//declared var', this.symbol.name);
38
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 37
diff changeset
1148 }
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1149 return prefix + this.symbol.toC() + ' = ' + val;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1150 };
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1151 assignment.prototype.toCObject = function(cobj) {
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 52
diff changeset
1152 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
1153 assignNames.push('#' + this.symbol.name);
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1154 if (this.expression.toCObject) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1155 var val = this.expression.toCObject(cobj.name);
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1156 } else {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1157 var val = this.expression.toC();
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1158 }
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
1159 assignNames.pop();
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1160 if (val === null) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1161 return;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1162 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1163 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
1164 var params = ['((object *)self)'];
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
1165 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
1166 var messagevars = {};
35
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
1167 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
1168 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
1169 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
1170 messagevars[escaped] = 'object *';
35
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
1171 params.push(escaped);
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
1172 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
1173 }
bf5e88f6419d Use a function/method call strategy that actually works
Mike Pavone <pavone@retrodev.com>
parents: 34
diff changeset
1174 }
122
9820ecd4eed4 Add support for implementing operators on user defined objects
Mike Pavone <pavone@retrodev.com>
parents: 121
diff changeset
1175 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
1176 vars: messagevars,
42
4e983fe32047 Fix closures as methods so that private vars work
Mike Pavone <pavone@retrodev.com>
parents: 41
diff changeset
1177 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
1178 });
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1179 } else {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1180 cobj.addProperty(this.symbol.name, val);
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
1181 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
1182 cobj.addInit('self->' + escapeCName(this.symbol.name) + '->parent = (object *)self;');
59
0fd06e077afe Fix object parent
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
1183 }
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1184 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1185 };
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
1186 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
1187 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
1188 };