changeset 177:76e3d4ae1746

Support more bitwise operations and function pointers in llMessages
author Mike Pavone <pavone@retrodev.com>
date Sat, 24 Aug 2013 09:54:47 -0700
parents ab204751d1e7
children e823e104b845
files cbackend.js
diffstat 1 files changed, 34 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/cbackend.js	Sat Aug 24 09:53:20 2013 -0700
+++ b/cbackend.js	Sat Aug 24 09:54:47 2013 -0700
@@ -34,7 +34,7 @@
 	}
 };
 op.prototype.toCLLExpr = function(vars) {
-	var opmap = {'=': '==', 'xor': '^'};
+	var opmap = {'=': '==', 'xor': '^', 'or': '|', 'and': '&'};
 	return this.left.toCLLExpr(vars) + (this.op in opmap ? opmap[this.op] : this.op) + this.right.toCLLExpr(vars);
 };
 op.prototype.toCLines = function(vars, needsreturn) {
@@ -280,6 +280,26 @@
 		var receiver = this.receiver ? this.receiver : this.args[0];
 		return 'struct ' + receiver.toCTypeName();
 		break;
+	case 'funptr:':
+	case 'funptr':
+		var rettype;
+		var firstArg;
+		if (this.receiver) {
+			rettype = this.receiver;
+			firstArg = 0;
+		} else {
+			rettype = this.args[0];
+			firstArg = 1;
+		}
+		var argtypes = '';
+		for (var i = firstArg; i < this.args.length; i++) {
+			if (argtypes) {
+				argtypes += ', '
+			}
+			argtypes += this.args[i].toCTypeName();
+		}
+		return [rettype.toCTypeName() + '(*', ')(' + argtypes + ')'];
+		break;
 	default:
 		throw new Error('invalid use of funcall expression where a C type name is expected');
 	}
@@ -375,6 +395,8 @@
 		return args[0].toCLLExpr(vars) + '[' + args[1].toCLLExpr(vars) + '] = ' + args[2].toCLLExpr(vars);
 	case 'not':
 		return '!(' + args[0].toCLLExpr(vars) + ')';
+	case 'castTo':
+		return '((' + args[1].toCTypeName() + ')(' + args[0].toCLLExpr(vars) + '))';
 	case 'mcall':
 		if (args[0] instanceof symbol) {
 			args[0] = new intlit(getMethodId(args[0].name));
@@ -486,7 +508,11 @@
 	var objdef =  'typedef struct ' + this.name + ' {\n\tobject header;\n';
 	for (var i in this.properties) {
 		if (this.properties[i] instanceof Array) {
-			objdef += '\t' + this.properties[i][1] + ' ' + this.properties[i][0] + ';\n';
+			if (this.properties[i][1] instanceof Array) {
+				objdef += '\t' + this.properties[i][1][0] + this.properties[i][0] + this.properties[i][1][1] + ';\n';
+			} else {
+				objdef += '\t' + this.properties[i][1] + ' ' + this.properties[i][0] + ';\n';
+			}
 		} else {
 			objdef += '\tobject * ' + this.properties[i] + ';\n'
 		}
@@ -507,7 +533,11 @@
 			slotdefs += 'object * ' + this.name + '_slot_' + i + '(uint32_t method_id, uint32_t num_params, object * oself, va_list args) {\n\t' +
 				this.name + ' *self = (' + this.name + ' *)oself;\n';
 			for (var varname in this.slotvars[i]) {
-				slotdefs += '\t' + this.slotvars[i][varname] + ' ' + varname + ';\n';
+				if (this.slotvars[i][varname] instanceof Array) {
+					slotdefs += '/*foo*/\t' + this.slotvars[i][varname][0] + ' ' + varname + this.slotvars[i][varname][1] + ';\n';
+				} else {
+					slotdefs += '/*bar*/\t' + this.slotvars[i][varname] + ' ' + varname + ';\n';
+				}
 			}
 			if (this.slots[i].length == 1) {
 				slotdefs += '\tif (method_id == ' + this.slots[i][0][0] + ') { /* ' + this.slots[i][0][2] + '*/\n' +
@@ -579,7 +609,7 @@
 				});
 				me.addImport(importsyms, messages[i].args[1]);
 			} else if(messages[i].name == 'llProperty:withType:' && messages[i].args.length == 2) {
-				me.addProperty(messages[i].args[0].name, null, messages[i].args[1].toCTypeName())
+				me.addProperty(messages[i].args[0].name, null, messages[i].args[1].toCTypeName());
 			} else if(messages[i].name == 'llMessage:withVars:andCode:' && messages[i].args.length == 3) {
 				var msgname = messages[i].args[0].name
 				var rawvars = messages[i].args[1].expressions;