Radicalc  Check-in [dfefa63301]

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

Overview
Comment:factorial example compiles again, llvm this time
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:dfefa63301a75c4bc68ddd40a38db4e51f1bd23505b69f711ef35b53b971d2df
User & Date: athaudia 2017-08-01 02:08:51
Context
2017-08-01
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
00:59
compiling with llvm? check-in: 5ca6304291 user: athaudia tags: trunk
Changes

Changes to src/radicalc.js.

76
77
78
79
80
81
82

83
84
85
86
87
88
89
...
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
...
393
394
395
396
397
398
399












400
401
402
403
404
405
406
...
422
423
424
425
426
427
428




















429
430
431
432
433
434
435
	error("can't find variable '"+name+"'");
}

function annotate(node) {
	switch(node.type) {
		case "assign":
			annotate(node.expr);

			node.dataType = node.expr.dataType;
			if(node.dataType != getVar(node.scope, node.name).dataType)
				error("trying to assign wrong data type");
			break;
		case "funCall":
			node.params.forEach(function(param){
				annotate(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("* ");
................................................................................
			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(", ");
				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");
	}
}








>







 







>











>
>
>
>
>
>
>
>


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




<
>
>
>
>







 







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







 







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







76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
...
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
...
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
...
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
	error("can't find variable '"+name+"'");
}

function annotate(node) {
	switch(node.type) {
		case "assign":
			annotate(node.expr);
			node.var = getVar(node.scope, node.name);
			node.dataType = node.expr.dataType;
			if(node.dataType != getVar(node.scope, node.name).dataType)
				error("trying to assign wrong data type");
			break;
		case "funCall":
			node.params.forEach(function(param){
				annotate(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);
}

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("* ");
................................................................................
			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(", ");
				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");
	}
}

Changes to src/test.rcs.

12
13
14
15
16
17
18
19
20
21
		i = i + 1
	end
	return result
end

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







|
<

12
13
14
15
16
17
18
19

20
		i = i + 1
	end
	return result
end

fun main():i32
	var i = 0
	return factorial(3*3)

end