Radicalc  Check-in [bfe305a909]

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

Overview
Comment:changed to stack allocated temporaries... getting really messy, time for rewrite?
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:bfe305a909dba573b6c7ee3ff46a338162cd1436147e9207d135b68b568e48d3
User & Date: athaudia 2017-08-06 17:02:49
Context
2017-08-23
17:05
switching to antlr and c# check-in: cb3c0872e3 user: athaudia tags: trunk
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
Changes

Changes to src/llvm.js.

40
41
42
43
44
45
46






















47
48
49
50
51
52
53
...
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
242
243
244
245
246
247
248
249

250
251
252
253
254
255
256
function outVar(out, v) {
	out.write(varStr(v));
}

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























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

var llvmIntrinsics = [];

................................................................................
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 ");
				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");
			}
			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);
			}
			var tmp = newTmp();
			out.write("  " + tmp + " = load ");
			var dt = dataTypeStr(node.dataType);
			out.write(dt + ", " + dt + "* ");
			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("  store ");
				console.log(stmt.fun.dataType);
				outDataType(out, stmt.fun.dataType);
				out.write(" ");
				out.write(String(ret));
				out.write(", ");
				outDataType(out, stmt.fun.dataType);
				out.write("* %result\n")

			}
			out.write("  br label %return_"+stmt.fun.name+"\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");
	}
}







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







 







>
>
|


|







 







|




|

|


>
|

<
|
<
<
<
<
<
<
<
<











>

>



|





>




>




|



>









>



|





>









>
>
|











>







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
...
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
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
function outVar(out, v) {
	out.write(varStr(v));
}

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

function outNewStackTmp(out, dataType, val) {
	tmp = newTmp();
	dts = dataTypeStr(dataType);
	out.write("  " + tmp + " = alloca " + dts + "\n");
	out.write("  store " + dts + " " + val + ", " + dts + "* " + tmp + "\n");
	return tmp;
}

function outLoadVar(out, dataType, v) {
	var tmp = newTmp();
	out.write("  " + tmp + " = load ");
	var dt = dataTypeStr(dataType);
	out.write(dt + ", " + dt + "* " + v); //(not outVar?)
	//outVar(out, v);
	out.write("\n");
	return tmp;
}

function outCopyVar(out, dataType, v) {
	return outNewStackTmp(out, dataType, outLoadVar(out, dataType, v));
}

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

var llvmIntrinsics = [];

................................................................................
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) {
				var v = outCopyVar(out, param.dataType, outNode(out,param));
				var loaded = outLoadVar(out, param.dataType, v);
				return [v, loaded, param.dataType];
			});
			if(intrinsicFunc) {
				tmp = intrinsicFunc(out, params.map((p)=>{return p[1]}));
			} else {
				tmp = newTmp();
				out.write("  ");
			
				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[2]) + " " + param[1];
				}).join(", "));
				out.write(")\n");
			}
			params.forEach((p) => {
				outDestroy(out, p[0], p[2]);
			});
			return outNewStackTmp(out, node.dataType, tmp);
		case 'litInt':
			//return llvmDataTypeStr(node.dataType) + " " + node.val;
			return outNewStackTmp(out, node.dataType, node.val);
			//return node.val;
		case 'varUse':

			return varStr(node.var);








		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':
			outDestroy(out, varStr(stmt.var), stmt.var.dataType);
			var tmp = outNode(out, stmt.expr);
			var loaded = outLoadVar(out, stmt.expr.dataType, tmp);
			out.write("  store ");
			outDataType(out, stmt.dataType);
			out.write(" ");
			out.write(loaded);
			out.write(", ");
			outDataType(out, stmt.dataType);
			out.write("* ");
			outVar(out, stmt.var);
			out.write("\n");
			outDestroy(out, tmp, stmt.expr.dataType);
			break;
		case 'return':
			if(stmt.expr) {
				var ret = outNode(out, stmt.expr);
				var loaded = outLoadVar(out, stmt.expr.dataType, ret);
				out.write("  store ");
				console.log(stmt.fun.dataType);
				outDataType(out, stmt.fun.dataType);
				out.write(" ");
				out.write(loaded);
				out.write(", ");
				outDataType(out, stmt.fun.dataType);
				out.write("* %result\n")
				outDestroy(out, ret, stmt.expr.dataType);
			}
			out.write("  br label %return_"+stmt.fun.name+"\n")
			break;
		case 'stmtExpr':
			outNode(out, stmt.expr);
			break;
		case 'varDef':
			if(stmt.expr) {
				var tmp = outNode(out, stmt.expr);
				var loaded = outLoadVar(out, stmt.expr.dataType, tmp);
				out.write("  store ");
				outDataType(out, stmt.dataType);
				out.write(" ");
				out.write(loaded);
				out.write(", ");
				outDataType(out, stmt.dataType);
				out.write("* ");
				outVar(out, stmt);
				out.write("\n");
				outDestroy(out, tmp, stmt.expr.dataType);
			}
			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);
			var dt = stmt.expr.dataType;
			var dts = dataTypeStr(dt);
			out.write("  br i1 " + outLoadVar(out, dt, 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");
			outDestroy(out, cond, dt);
			break;
		default:
			out.write("  ; ");
			out.write(stmt.type);
			out.write(" statement not yet implemented\n");
	}
}

Changes to src/radicalc.js.

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  	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?
*/







<
<
<
<











217
218
219
220
221
222
223




224
225
226
227
228
229
230
231
232
233
234
  	console.error(`exec error: ${error}`);
  	return;
  }
  console.log(stdout);
});

/* todo:




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