Radicalc  Check-in [d26be5d653]

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

Overview
Comment:temporary copy of parameters
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:d26be5d653d6bc9b470601bd5918cd6d6688750cbf318d8e3d786c949cfc7194
User & Date: athaudia 2017-08-06 15:44:19
Context
2017-08-06
17:02
changed to stack allocated temporaries... getting really messy, time for rewrite? check-in: bfe305a909 user: athaudia tags: trunk
15:44
temporary copy of parameters check-in: d26be5d653 user: athaudia tags: trunk
2017-08-02
11:40
changed how returns work check-in: c16abe7c58 user: athaudia tags: trunk
Changes

Changes to src/intrinsics.rcs.

29
30
31
32
33
34
35









36
37
38
39
40
41
42
fun div(a:i16, b:i16):i16 stub
fun div(a:i32, b:i32):i32 stub
fun div(a:i64, b:i64):i64 stub
fun div(a:u8,  b:u8): u8  stub
fun div(a:u16, b:u16):u16 stub
fun div(a:u32, b:u32):u32 stub
fun div(a:u64, b:u64):u64 stub










fun is_less(a:i8,  b:i8): bool stub
fun is_less(a:i16, b:i16):bool stub
fun is_less(a:i32, b:i32):bool stub
fun is_less(a:i64, b:i64):bool stub
fun is_less(a:u8,  b:u8): bool stub
fun is_less(a:u16, b:u16):bool stub







>
>
>
>
>
>
>
>
>







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
fun div(a:i16, b:i16):i16 stub
fun div(a:i32, b:i32):i32 stub
fun div(a:i64, b:i64):i64 stub
fun div(a:u8,  b:u8): u8  stub
fun div(a:u16, b:u16):u16 stub
fun div(a:u32, b:u32):u32 stub
fun div(a:u64, b:u64):u64 stub

fun mod(a:i8,  b:i8):i8   stub
fun mod(a:i16, b:i16):i16 stub
fun mod(a:i32, b:i32):i32 stub
fun mod(a:i64, b:i64):i64 stub
fun mod(a:u8,  b:u8): u8  stub
fun mod(a:u16, b:u16):u16 stub
fun mod(a:u32, b:u32):u32 stub
fun mod(a:u64, b:u64):u64 stub

fun is_less(a:i8,  b:i8): bool stub
fun is_less(a:i16, b:i16):bool stub
fun is_less(a:i32, b:i32):bool stub
fun is_less(a:i64, b:i64):bool stub
fun is_less(a:u8,  b:u8): bool stub
fun is_less(a:u16, b:u16):bool stub

Changes to src/llvm.js.

1
2
3
4
5
6
7
8
9
10
11
...
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
...
151
152
153
154
155
156
157



158
159
160
161
162
163
164
...
249
250
251
252
253
254
255
256
257
258
259
260
261




262
263
264
265
266
267
268
...
275
276
277
278
279
280
281

282
283
284
285
286
287
288
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));
}
................................................................................
				outDataType(out, dt);
				out.write(" " + params[0] + ", " + params[1] + "\n");
				return tmp;
			});
		});
	});
}





function outNode(out, node) {
	switch(node.type) {
		case 'funCall':
			var dts = node.params.map((param) => { return param.dataType });
			var intrinsicFunc = getIntrinsic(node.name, dts);
			if(intrinsicFunc) {
				var params = node.params.map((param) => {
					return outNode(out, param);
				});

				return intrinsicFunc(out, params);
			} else {
				var params = node.params.map(function(param) {
					return [outNode(out, param), param.dataType];
				});
				var tmp = newTmp();
				out.write("  ");
			
				if(node.dataType.name != "void") {
					out.write(tmp);
					out.write(" = ");
				}
				out.write("call ");
................................................................................
				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);
................................................................................
			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);
	});
}

................................................................................
	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(") {\n");

	if(fun.dataType.name != 'void') {
		out.write("  %result = alloca ");
		outDataType(out, fun.dataType);
		out.write("\n");



|







 







>
>
>
>






|
|
|
|
>
|

<
<
<
|







 







>
>
>







 







<
|
|
|
|
|
>
>
>
>







 







>







1
2
3
4
5
6
7
8
9
10
11
...
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
...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
...
254
255
256
257
258
259
260

261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
...
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
function dataTypeStr(dataType) {
	return {
		i8:'i8',i16:'i16',i32:'i32',i64:'i64',
		u8:'i8',u16:'i16',u32:'i32',u64:'i64', //todo: change
		bool:'i1',void:'void'
	}[dataType.name];
}

function outDataType(out, dataType) {
	out.write(dataTypeStr(dataType));
}
................................................................................
				outDataType(out, dt);
				out.write(" " + params[0] + ", " + params[1] + "\n");
				return tmp;
			});
		});
	});
}

function outDestroy(out, tmp, dataType) {
	out.write("  ;" + "destroying " + dataTypeStr(dataType) + " " + tmp + "\n");
}

function outNode(out, node) {
	switch(node.type) {
		case 'funCall':
			var dts = node.params.map((param) => { return param.dataType });
			var intrinsicFunc = getIntrinsic(node.name, dts);
			var tmp;
			var params = node.params.map(function(param) {
				return [outNode(out, param), param.dataType];
			});
			if(intrinsicFunc) {
				tmp = intrinsicFunc(out, params.map((p)=>{return p[0]}));
			} else {



				tmp = newTmp();
				out.write("  ");
			
				if(node.dataType.name != "void") {
					out.write(tmp);
					out.write(" = ");
				}
				out.write("call ");
................................................................................
				outFunName(out, node.funCall);
				out.write("(");
				out.write(params.map(function(param) {
					return dataTypeStr(param[1]) + " " + param[0];
				}).join(", "));
				out.write(")\n");
			}
			params.forEach((p) => {
				outDestroy(out, p[0], p[1]);
			});
			return tmp;
		case 'litInt':
			//return llvmDataTypeStr(node.dataType) + " " + node.val;
			return node.val;
		case 'varUse':
			if(node.var.isParam) {
				return varStr(node.var);
................................................................................
			out.write(" statement not yet implemented\n");
	}
}

function outBlock(out, block) {
	for(var key in block.scope.vars) {
		var v = block.scope.vars[key];

		out.write("  ");
		outVar(out, v);
		out.write(" = alloca ");
		outDataType(out, v.dataType);
		out.write("\n");
		if(v.isParam) {
			var dts = dataTypeStr(v.dataType)
			var vs = varStr(v);
			out.write("  store "+dts+" "+vs+"_par"+", "+dts+"* "+vs+"\n");
		}
	}
	block.statements.forEach(function(stmt) {
		outStatement(out, stmt);
	});
}

................................................................................
	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("_par");
	} 
	out.write(") {\n");

	if(fun.dataType.name != 'void') {
		out.write("  %result = alloca ");
		outDataType(out, fun.dataType);
		out.write("\n");

Changes to src/radicalc.js.

204
205
206
207
208
209
210
211
212

213
214
215
216
217
218
219
220
221
222




223
224







225
226
initFunctionScopes();
preAnnotate();
annotateFunctions();

llvm.initIntrinsics(dataTypes);
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 "C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.14393.0\\ucrt\\x64\\ucrt.lib" /entry:main__i32 /subsystem:console && test2.bat', (error, stdout, stderr) => {
  if (error) {
  	console.error(`exec error: ${error}`);
  	return;
  }
  console.log(stdout);
});

/* todo:




 - modulo operator
 - unsigned intrinsics







 - strings?
*/







|

>










>
>
>
>


>
>
>
>
>
>
>


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
initFunctionScopes();
preAnnotate();
annotateFunctions();

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

exec('opt -O3 tmp.ll -S -o tmp.s && llc tmp.s -o tmp.o -filetype=obj && lld-link tmp.o "C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.14393.0\\ucrt\\x64\\ucrt.lib" /entry:main__i32 /subsystem:console && test2.bat', (error, stdout, stderr) => {
  if (error) {
  	console.error(`exec error: ${error}`);
  	return;
  }
  console.log(stdout);
});

/* todo:
 - change temporaries to be stack allocated variables...
   let llvm optimiser handle that, and just write a single generic way of
   doing things; always copy before passing to function and delete after
 - copy params into temporary stack allocated variables
 - modulo operator
 - unsigned intrinsics
 - destructors... when to call them?
   - whenever assigning to variable, first destruct what's in it
   - temporaries... when?
     - end of current statement, or...
     - as soon as it's not needed anymore
 - difference between variables, llvm-temporaries and radicalc-temporaries?
 - copy before returning?
 - strings?
*/