Radicalc  Check-in [82a18187dc]

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

Overview
Comment:removed C output
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:82a18187dc3869d8e09c9ede9e89a158b992c0f28f8da65ddd1ebf89948fc17d
User & Date: athaudia 2017-08-01 10:25:17
Context
2017-08-01
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
02:08
factorial example compiles again, llvm this time check-in: dfefa63301 user: athaudia tags: trunk
Changes

Changes to src/radicalc.js.

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
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
...
354
355
356
357
358
359
360

361
362
363
364
365
366
367
368
369
370
...
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
...
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599

600



601
602


603
				error("variable '"+param.name+"'already exists");
			fun.block.scope.vars[param.name] = param;
			param.isParam = true;
		});
	});
}

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

function cOutDataType(out, dataType) {
	out.write("rcst__");
	out.write(dataType.name);
}

function cOutDataTypePartial(out, dataType) {
	out.write(dataType.name);
}

function cOutFunName(out, fun) {
	out.write("rcsf__");
	out.write(fun.name);
	out.write("__");
	cOutDataTypePartial(out, fun.dataType);
	fun.params.forEach(function(param){
		out.write("__");
		cOutDataTypePartial(out, param.dataType);
	});
}

function cOutFunDef(out, fun) {
	cOutDataType(out, fun.dataType);
	out.write(" ");
	cOutFunName(out, fun);
	out.write("(");
	for(var i = 0; i < fun.params.length; ++i) {
		if(i > 0)
			out.write(", ");
		cOutDataType(out, fun.params[i].dataType);
		out.write(" ");
		out.write(fun.params[i].name);
	}
	out.write(")");
}

function cOutNode(out, node) {
	switch(node.type) {
		case "assign":
			out.write(node.name);
			out.write(" = ");
			cOutNode(out, node.expr);
			out.write(";\n");
			break;
		case "funCall":
			cOutFunName(out, node.funCall);
			out.write("(");
			for(var i = 0; i < node.params.length; ++i) {
				if(i > 0)
					out.write(", ");
				cOutNode(out, node.params[i]);
			}
			out.write(")");
			break;
		case "litInt":
			out.write(node.val);
			break;
		case "return":
			out.write("return ");
			if(node.expr)
				cOutNode(out, node.expr);
			out.write(";\n");
			break;
		case "stmtExpr":
			cOutNode(out, node.expr);
			out.write(";\n");
			break;
		case "varDef":
			if(node.expr) {
				out.write(node.name);
				out.write(" = ");
				cOutNode(out, node.expr);
				out.write(";\n");
			}
			break;
		case "varUse":
			out.write(node.name);
			break;
		case "while":
			out.write("while(");
			cOutNode(out, node.expr);
			out.write(")");
			cOutBlock(out, node.block);
			break;
		default:
			error("'"+node.type+"' not yet implemented in cOutNode");
	}
}

function cOutBlock(out, block) {
	out.write("{\n");
	for(var key in block.scope.vars) {
		var v = block.scope.vars[key];
		if(!v.isParam) {
			cOutDataType(out, v.dataType);
			out.write(" ");
			out.write(v.name);
			out.write(";\n");
		}
	}
	out.write("\n");
	block.statements.forEach(function(statement) {
		cOutNode(out, statement);
	});
	out.write("}\n");
}

function cOutFunctions(out) {
	out.write("#include \"rcs_std.h\"\n\n");
	funs.forEach(function(fun) {
		if(!fun.isStub) {
			cOutFunDef(out, fun);
			out.write(";\n");
		}
	});
	out.write("\n");
	funs.forEach(function(fun) {
		if(!fun.isStub) {
			cOutFunDef(out, fun);
			cOutBlock(out, fun.block);
			out.write ("\n");
		}
	});
	var main = getFun("main", []);
	out.write("int main() {");
	cOutFunName(out, main);
	out.write("();}\n");
}

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

var tacLabel = 0;
function tacNewLabel() {
	return 'label_'+tacLabel++;
}

var llvmTmp = 0;

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];
}
................................................................................
	out.write(fun.dataType.name);
	fun.params.forEach(function(param){
		out.write("__");
		out.write(param.dataType.name);
	});
}


llvmVar = 1;
llvmTmp = 1;
llvmLabel = 1;

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

................................................................................
	out.write(" {\n");
	llvmOutBlock(out, fun.block);
	out.write("}\n\n");
}

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

................................................................................
////////////////////////////////////////

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

var out = fs.createWriteStream("tmp.ll");
//cOutFunctions(out);
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);
});
/*
exec('C:\\apps\\tcc\\tcc.exe -o test.exe tmp.c', (error, stdout, stderr) => {
  if (error) {
  	console.error(`exec error: ${error}`);
  	return;

  }



  console.log(stdout);
});


*/







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


<
<
<
<
<
<
<







 







>
|
|
|







 







|







 







<











<
<
<
<
>
|
>
>
>
|
<
>
>
|
192
193
194
195
196
197
198


































































































































199
200







201
202
203
204
205
206
207
...
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
...
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
				error("variable '"+param.name+"'already exists");
			fun.block.scope.vars[param.name] = param;
			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];
}
................................................................................
	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);
}

................................................................................
	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
*/