swayf
12/10/2014 - 10:34 AM

RegTest.hx

(function () { "use strict";
var RegTest = function() {
	this.ram = new Uint8Array(65536);
};
RegTest.main = function() {
	var foo = new RegTest();
	foo.whatIsR0();
};
RegTest.prototype = {
	whatIsR0: function() {
		console.log("r0 is " + this.ram[123]);
	}
	,get_r0: function() {
		return this.ram[123];
	}
	,set_r0: function(value) {
		return this.ram[123] = value;
	}
};
RegTest.main();
})();
package  ;

import haxe.macro.Context;
import haxe.macro.Expr;

class RegisterMacro
{
    macro static public function memoryMappedRegister(fieldName:String,index :Int):Array<Field> {
    	var fields = Context.getBuildFields();
    	
    	var getterName = "get_" + fieldName;
    	var setterName = "set_" + fieldName;
    	var propertyFromMacro = macro : {
    		var $fieldName(get, set) : Int;
    		inline function $getterName() : Int {
    			return ram[ $v{index} ];
            }
    		 inline function $setterName(value:Int) : Int {
                    return ram[$v{index}]=value;
            }
    	};
    	switch (propertyFromMacro) {
    		case TAnonymous(getterFields):
    			fields=fields.concat(getterFields);
    		default:
    			throw 'unreachable';
    	}
	
    return fields;
    }
}
import js.html.Uint8Array;

@:build( RegisterMacro.memoryMappedRegister("r0",123) )
class RegTest
{
	var ram : Uint8Array;
	
	public function new() {
		ram = new Uint8Array(65536);
	}
	
	public function whatIsR0() {
		trace("r0 is "+r0);
	}
    
    static function main() {
        var foo = new RegTest();
        foo.whatIsR0();
    }
}