Radicalc  Check-in [5ca6304291]

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

Overview
Comment:compiling with llvm?
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:5ca6304291372290cb54f1b57ea516ec9b39a78ae44264c08dfaebc4ed67b77c
User & Date: athaudia 2017-08-01 00:59:57
Context
2017-08-01
02:08
factorial example compiles again, llvm this time check-in: dfefa63301 user: athaudia tags: trunk
00:59
compiling with llvm? check-in: 5ca6304291 user: athaudia tags: trunk
2017-07-30
18:52
it compiles :o check-in: 7e142f1cb4 user: athaudia tags: trunk
Changes

Changes to src/radicalc.js.

21
22
23
24
25
26
27
28
29
30


31
32
33
34
35
36
37
..
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

108
109
110
111
112
113
114
...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
...
320
321
322
323
324
325
326




























































































































































327
328
329

330
331


332








333
334
335
336
337
338
339

	console.log("ambiguity found");
	process.exit(1);
}

var tree = p.results[0];
var funs = [];
var dataTypes = {
	void:{name:"void"},bool:{name:"bool"},
	i8:{name:"i8"},i16:{name:"i16"},i32:{name:"i32"},i64:{name:"i64"},
	u8:{name:"u8"},u16:{name:"u16"},u32:{name:"u32"},u64:{name:"u64"}


};

function getDataType(str) {
	if(dataTypes[str] == null) {
		console.log("error: Data type '" + str + "' doesn't exist");
		process.exit(-1);
	}
................................................................................
			node.dataType = node.funCall.dataType;
			break;
		case "litInt":
			node.dataType = dataTypes.i32;
			break;
		case "return":
			if(node.expr == null) {
				if(node.fun.dataType)
					error("can't void return in function with return type");
			}
			else {
				annotate(node.expr);
				node.dataType = node.expr.dataType;
				if(node.dataType != node.fun.dataType)
					error("wrong datatype in return");
			}

		case "stmtExpr":
			annotate(node.expr);
			break;
		case "varDef":
			if(node.dataTypeStr == null && node.expr == null)
				error("vardef of "+node.name+" has no datatype");
			if(node.expr)
................................................................................
				error("variable '"+param.name+"'already exists");
			fun.block.scope.vars[param.name] = param;
			param.isParam = true;
		});
	});
}

function cOutFunction(out, fun) {
	
}

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

function cOutDataTypePartial(out, dataType) {
................................................................................
	});
	var main = getFun("main", []);
	out.write("int main() {");
	cOutFunName(out, main);
	out.write("();}\n");
}





























































































































































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

var out = fs.createWriteStream("tmp.c");
cOutFunctions(out);


out.end();








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);
});








|
|
|
>
>







 







|








>







 







|
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



>
|
|
>
>

>
>
>
>
>
>
>
>







>
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
..
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
...
191
192
193
194
195
196
197
198


199
200
201
202
203
204
205
...
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
	console.log("ambiguity found");
	process.exit(1);
}

var tree = p.results[0];
var funs = [];
var dataTypes = {
	void:{name:"void",size:0},bool:{name:"bool",size:1},
	i8: {name:"i8", size:1},i16:{name:"i16",size:2},
	i32:{name:"i32",size:4},i64:{name:"i64",size:8},
	u8: {name:"u8", size:1},u16:{name:"u16",size:2},
	u32:{name:"u32",size:4},u64:{name:"u64",size:8}
};

function getDataType(str) {
	if(dataTypes[str] == null) {
		console.log("error: Data type '" + str + "' doesn't exist");
		process.exit(-1);
	}
................................................................................
			node.dataType = node.funCall.dataType;
			break;
		case "litInt":
			node.dataType = dataTypes.i32;
			break;
		case "return":
			if(node.expr == null) {
				if(node.fun.dataType.name != "void")
					error("can't void return in function with return type");
			}
			else {
				annotate(node.expr);
				node.dataType = node.expr.dataType;
				if(node.dataType != node.fun.dataType)
					error("wrong datatype in return");
			}
			break;
		case "stmtExpr":
			annotate(node.expr);
			break;
		case "varDef":
			if(node.dataTypeStr == null && node.expr == null)
				error("vardef of "+node.name+" has no datatype");
			if(node.expr)
................................................................................
				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) {
................................................................................
	});
	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];
}

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);
	});
}

llvmVar = 1;
llvmTmp = 1;

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

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

function llvmOutNode(out, node) {
	switch(node.type) {
		case 'litInt':
			//return llvmDataTypeStr(node.dataType) + " " + node.val;
			return node.val;
		case 'varUse':
			var tmp = "%tmp"+String(llvmTmp++);
			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 '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;
		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 = 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);
});
*/

Changes to src/test.rcs.

10
11
12
13
14
15
16
17

18

19
	while i <= n
		result = result * i
		i = i + 1
	end
	return result
end

fun main()

	print(factorial(12))

end







|
>

>

10
11
12
13
14
15
16
17
18
19
20
21
	while i <= n
		result = result * i
		i = i + 1
	end
	return result
end

fun main():i32
	var i = 0
	print(factorial(12))
	return 123
end

Added src/test2.bat.







>
>
>
1
2
3
@echo OFF
tmp.exe
echo %errorlevel%