Radicalc  Check-in [a35ee94f11]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:split llvm output into seperate .js file
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:a35ee94f11e1f4e87e327ac6a66efa9945ad52023e23212b54338f508b9c67f0
User & Date: athaudia 2017-08-01 10:40:41
Context
2017-08-01
14:16
added intrinsincs check-in: 5bcf572b20 user: athaudia tags: trunk
10:40
split llvm output into seperate .js file check-in: a35ee94f11 user: athaudia tags: trunk
10:25
removed C output check-in: 82a18187dc user: athaudia tags: trunk
Changes

Added src/llvm.js.



































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
function dataTypeStr(dataType) {
	return {
		i8:'i8',i16:'i16',i32:'i32',i64:'i64',
		u8:'i8',u16:'i16',u32:'i32',u64:'i64',
		bool:'i1',void:'void'
	}[dataType.name];
}

function outDataType(out, dataType) {
	out.write(dataTypeStr(dataType));
}

function outFunName(out, fun) {
	out.write("@");
	out.write(fun.name);
	out.write("__");
	out.write(fun.dataType.name);
	fun.params.forEach(function(param){
		out.write("__");
		out.write(param.dataType.name);
	});
}

// 1 and not 0 because 0 parses as false, thanks javascript
var llvmVar = 1;
var llvmTmp = 1;
var llvmLabel = 1;

function varStr(v) {
	if(!v.llvmId)
		v.llvmId = llvmVar++;
	return "%v" + String(v.llvmId);
}

function outVar(out, v) {
	out.write(varStr(v));
}

function newTmp() {
	return "%tmp"+String(llvmTmp++);
}

function newLabel() {
	return "Label"+String(llvmLabel++);
}

function outNode(out, node) {
	switch(node.type) {
		case 'funCall':
			var params = node.params.map(function(param) {
				return [outNode(out, param), param.dataType];
			});
			var tmp = newTmp();
			out.write("  ");

			if(node.name == 'add') {
				out.write(tmp);
				out.write(" = add ");
				outDataType(out, params[0][1]);
				out.write(" ");
				out.write(params[0][0]);
				out.write(", ");
				out.write(params[1][0]);
				out.write("\n");
			} else if(node.name == 'mul') {
				out.write(tmp);
				out.write(" = mul ");
				outDataType(out, params[0][1]);
				out.write(" ");
				out.write(params[0][0]);
				out.write(", ");
				out.write(params[1][0]);
				out.write("\n");
			} else if(node.name == 'is_less_or_eq') {
				out.write(tmp);
				out.write(" = icmp sle "); //todo: signed vs unsigned
				outDataType(out, params[0][1]);
				out.write(" ");
				out.write(params[0][0]);
				out.write(", ");
				out.write(params[1][0]);
				out.write("\n");
			} else {
				if(node.dataType.name != "void") {
					out.write(tmp);
					out.write(" = ");
				}
				out.write("call ");
				outDataType(out, node.dataType);
				out.write(" ");
				outFunName(out, node.funCall);
				out.write("(");
				out.write(params.map(function(param) {
					return dataTypeStr(param[1]) + " " + param[0];
				}).join(", "));
				out.write(")\n");
			}
			return tmp;
		case 'litInt':
			//return llvmDataTypeStr(node.dataType) + " " + node.val;
			return node.val;
		case 'varUse':
			if(node.var.isParam) {
				return varStr(node.var);
			}
			var tmp = newTmp();
			out.write("  ");
			out.write(tmp);
			out.write(" = load ");
			outDataType(out, node.dataType);
			out.write(", ");
			outDataType(out, node.dataType);
			out.write("* ");
			outVar(out, node.var);
			out.write("\n");
			return tmp;
		default:
			out.write("  ; ")
			out.write(node.type);
			out.write(" node not yet implemented\n");
			return "errrrrrrrorrrrr";
	}
}

function outStatement(out, stmt) {
	switch(stmt.type) {
		case 'assign':
			var tmp = outNode(out, stmt.expr);
			out.write("  store ");
			outDataType(out, stmt.dataType);
			out.write(" ");
			out.write(String(tmp));
			out.write(", ");
			outDataType(out, stmt.dataType);
			out.write("* ");
			outVar(out, stmt.var);
			out.write("\n");
			break;
		case 'return':
			if(stmt.expr) {
				var ret = outNode(out, stmt.expr);
				out.write("  ret ");
				outDataType(out, stmt.dataType);
				out.write(" ");
				out.write(ret);
				out.write("\n");
			} else {
				out.write("  ret void\n");
			}
			break;
		case 'stmtExpr':
			outNode(out, stmt.expr);
			break;
		case 'varDef':
			if(stmt.expr) {
				var tmp = outNode(out, stmt.expr);
				out.write("  store ");
				outDataType(out, stmt.dataType);
				out.write(" ");
				out.write(String(tmp));
				out.write(", ");
				outDataType(out, stmt.dataType);
				out.write("* ");
				outVar(out, stmt);
				out.write("\n");
			}
			break;
		case 'while':
			var beginLabel = newLabel();
			var innerBeginLabel = newLabel();
			var endLabel = newLabel();
			out.write("  br label %"+beginLabel+"\n");
			out.write(beginLabel+":\n");
			var cond = outNode(out, stmt.expr);
			out.write("  br i1 " + cond + ", label %");
			out.write(innerBeginLabel);
			out.write(", label %");
			out.write(endLabel);
			out.write("\n");
			out.write(innerBeginLabel + ":\n");
			outBlock(out, stmt.block);
			out.write("  br label %");
			out.write(beginLabel);
			out.write("\n");
			out.write(endLabel);
			out.write(":\n");
			break;
		default:
			out.write("  ; ");
			out.write(stmt.type);
			out.write(" statement not yet implemented\n");
	}
}

function outBlock(out, block) {
	for(var key in block.scope.vars) {
		var v = block.scope.vars[key];
		if(!v.isParam) {
			out.write("  ");
			outVar(out, v);
			out.write(" = alloca ");
			outDataType(out, v.dataType);
			out.write("\n");
		}
	}
	block.statements.forEach(function(stmt) {
		outStatement(out, stmt);
	});
}

function outFunction(out, fun) {
	out.write("define ");
	outDataType(out, fun.dataType);
	out.write(" ");
	outFunName(out, fun);
	out.write("(");
	for(var i = 0; i < fun.params.length; ++i) {
		var p = fun.params[i];
		if(i > 0)
			out.write(", ");
		outDataType(out, p.dataType);
		out.write(" ");
		out.write(varStr(p));
	} 
	out.write(")");
	out.write(" {\n");
	outBlock(out, fun.block);
	out.write("}\n\n");
}

function outFunctions(out, funs) {
	funs.forEach(function(fun){
		fun.label = newLabel();
	});
	funs.forEach(function(fun){
		if(!fun.isStub)
			outFunction(out, fun);
	});
}

module.exports = {outFunctions: outFunctions};

Changes to src/radicalc.js.

1
2
3

4
5
6
7
8
9
10
...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450

451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
var grammar = require("./parser.js");
var nearley = require("nearley");
var fs = require("fs");

const { exec } = require('child_process');

function error(str) {
	console.log("error: " + str);
	process.exit(1);
}

................................................................................
			param.isParam = true;
		});
	});
}

////////////////////////////////////////

function llvmDataTypeStr(dataType) {
	return {
		i8:'i8',i16:'i16',i32:'i32',i64:'i64',
		u8:'i8',u16:'i16',u32:'i32',u64:'i64',
		bool:'i1',void:'void'
	}[dataType.name];
}

function llvmOutDataType(out, dataType) {
	out.write(llvmDataTypeStr(dataType));
}

function llvmOutFunName(out, fun) {
	out.write("@");
	out.write(fun.name);
	out.write("__");
	out.write(fun.dataType.name);
	fun.params.forEach(function(param){
		out.write("__");
		out.write(param.dataType.name);
	});
}

// 1 and not 0 because 0 parses as false, thanks javascript
var llvmVar = 1;
var llvmTmp = 1;
var llvmLabel = 1;

function llvmVarStr(v) {
	if(!v.llvmId)
		v.llvmId = llvmVar++;
	return "%v" + String(v.llvmId);
}

function llvmOutVar(out, v) {
	out.write(llvmVarStr(v));
}

function llvmNewTmp() {
	return "%tmp"+String(llvmTmp++);
}

function llvmNewLabel() {
	return "Label"+String(llvmLabel++);
}

function llvmOutNode(out, node) {
	switch(node.type) {
		case 'funCall':
			var params = node.params.map(function(param) {
				return [llvmOutNode(out, param), param.dataType];
			});
			var tmp = llvmNewTmp();
			out.write("  ");

			if(node.name == 'add') {
				out.write(tmp);
				out.write(" = add ");
				llvmOutDataType(out, params[0][1]);
				out.write(" ");
				out.write(params[0][0]);
				out.write(", ");
				out.write(params[1][0]);
				out.write("\n");
			} else if(node.name == 'mul') {
				out.write(tmp);
				out.write(" = mul ");
				llvmOutDataType(out, params[0][1]);
				out.write(" ");
				out.write(params[0][0]);
				out.write(", ");
				out.write(params[1][0]);
				out.write("\n");
			} else if(node.name == 'is_less_or_eq') {
				out.write(tmp);
				out.write(" = icmp sle "); //todo: signed vs unsigned
				llvmOutDataType(out, params[0][1]);
				out.write(" ");
				out.write(params[0][0]);
				out.write(", ");
				out.write(params[1][0]);
				out.write("\n");
			} else {
				if(node.dataType.name != "void") {
					out.write(tmp);
					out.write(" = ");
				}
				out.write("call ");
				llvmOutDataType(out, node.dataType);
				out.write(" ");
				llvmOutFunName(out, node.funCall);
				out.write("(");
				out.write(params.map(function(param) {
					return llvmDataTypeStr(param[1]) + " " + param[0];
				}).join(", "));
				out.write(")\n");
			}
			return tmp;
		case 'litInt':
			//return llvmDataTypeStr(node.dataType) + " " + node.val;
			return node.val;
		case 'varUse':
			if(node.var.isParam) {
				return llvmVarStr(node.var);
			}
			var tmp = llvmNewTmp();
			out.write("  ");
			out.write(tmp);
			out.write(" = load ");
			llvmOutDataType(out, node.dataType);
			out.write(", ");
			llvmOutDataType(out, node.dataType);
			out.write("* ");
			llvmOutVar(out, node.var);
			out.write("\n");
			return tmp;
		default:
			out.write("  ; ")
			out.write(node.type);
			out.write(" node not yet implemented\n");
			return "errrrrrrrorrrrr";
	}
}

function llvmOutStatement(out, stmt) {
	switch(stmt.type) {
		case 'assign':
			var tmp = llvmOutNode(out, stmt.expr);
			out.write("  store ");
			llvmOutDataType(out, stmt.dataType);
			out.write(" ");
			out.write(String(tmp));
			out.write(", ");
			llvmOutDataType(out, stmt.dataType);
			out.write("* ");
			llvmOutVar(out, stmt.var);
			out.write("\n");
			break;
		case 'return':
			if(stmt.expr) {
				var ret = llvmOutNode(out, stmt.expr);
				out.write("  ret ");
				llvmOutDataType(out, stmt.dataType);
				out.write(" ");
				out.write(ret);
				out.write("\n");
			} else {
				out.write("  ret void\n");
			}
			break;
		case 'stmtExpr':
			llvmOutNode(out, stmt.expr);
			break;
		case 'varDef':
			if(stmt.expr) {
				var tmp = llvmOutNode(out, stmt.expr);
				out.write("  store ");
				llvmOutDataType(out, stmt.dataType);
				out.write(" ");
				out.write(String(tmp));
				out.write(", ");
				llvmOutDataType(out, stmt.dataType);
				out.write("* ");
				llvmOutVar(out, stmt);
				out.write("\n");
			}
			break;
		case 'while':
			var beginLabel = llvmNewLabel();
			var innerBeginLabel = llvmNewLabel();
			var endLabel = llvmNewLabel();
			out.write("  br label %"+beginLabel+"\n");
			out.write(beginLabel+":\n");
			var cond = llvmOutNode(out, stmt.expr);
			out.write("  br i1 " + cond + ", label %");
			out.write(innerBeginLabel);
			out.write(", label %");
			out.write(endLabel);
			out.write("\n");
			out.write(innerBeginLabel + ":\n");
			llvmOutBlock(out, stmt.block);
			out.write("  br label %");
			out.write(beginLabel);
			out.write("\n");
			out.write(endLabel);
			out.write(":\n");
			break;
		default:
			out.write("  ; ");
			out.write(stmt.type);
			out.write(" statement not yet implemented\n");
	}
}

function llvmOutBlock(out, block) {
	for(var key in block.scope.vars) {
		var v = block.scope.vars[key];
		if(!v.isParam) {
			out.write("  ");
			llvmOutVar(out, v);
			out.write(" = alloca ");
			llvmOutDataType(out, v.dataType);
			out.write("\n");
		}
	}
	block.statements.forEach(function(stmt) {
		llvmOutStatement(out, stmt);
	});
}

function llvmOutFunction(out, fun) {
	out.write("define ");
	llvmOutDataType(out, fun.dataType);
	out.write(" ");
	llvmOutFunName(out, fun);
	out.write("(");
	for(var i = 0; i < fun.params.length; ++i) {
		var p = fun.params[i];
		if(i > 0)
			out.write(", ");
		llvmOutDataType(out, p.dataType);
		out.write(" ");
		out.write(llvmVarStr(p));
	} 
	out.write(")");
	out.write(" {\n");
	llvmOutBlock(out, fun.block);
	out.write("}\n\n");
}

function llvmOutFunctions(out) {
	funs.forEach(function(fun){
		fun.label = llvmNewLabel();
	});
	funs.forEach(function(fun){
		if(!fun.isStub)
			llvmOutFunction(out, fun);
	});
}

////////////////////////////////////////

initFunctionScopes();
preAnnotate();
annotateFunctions();

var out = fs.createWriteStream("tmp.ll");
llvmOutFunctions(out);
//llvmOutFunctions(process.stdout);
out.end();

exec('opt -O3 tmp.ll -S -o tmp.s && llc tmp.s -o tmp.o -filetype=obj && lld-link tmp.o /entry:main__i32 /subsystem:console && test2.bat', (error, stdout, stderr) => {
  if (error) {
  	console.error(`exec error: ${error}`);
  	return;
  }
  console.log(stdout);
});
/*
*/


/* todo:
 - implement add, sub, div, mul and relational operators correctly
   - check signedness
 - seperate into multiple files
 - figure out how to link against standard functions
*/



>







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<





|
|

>







<
<
<







1
2
3
4
5
6
7
8
9
10
11
...
195
196
197
198
199
200
201


















































































































































































































































202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217



218
219
220
221
222
223
224
var grammar = require("./parser.js");
var nearley = require("nearley");
var fs = require("fs");
var llvm = require("./llvm.js");
const { exec } = require('child_process');

function error(str) {
	console.log("error: " + str);
	process.exit(1);
}

................................................................................
			param.isParam = true;
		});
	});
}

////////////////////////////////////////



















































































































































































































































initFunctionScopes();
preAnnotate();
annotateFunctions();

var out = fs.createWriteStream("tmp.ll");
llvm.outFunctions(out, funs);
//llvm.outFunctions(process.stdout, funs);
out.end();

exec('opt -O3 tmp.ll -S -o tmp.s && llc tmp.s -o tmp.o -filetype=obj && lld-link tmp.o /entry:main__i32 /subsystem:console && test2.bat', (error, stdout, stderr) => {
  if (error) {
  	console.error(`exec error: ${error}`);
  	return;
  }
  console.log(stdout);
});




/* todo:
 - implement add, sub, div, mul and relational operators correctly
   - check signedness
 - seperate into multiple files
 - figure out how to link against standard functions
*/