java - Object oriented three address code generation -


i'm working on project related compiler design. need generate 3 address code java-based language , implies use of objects , scopes. if can me generating tac following example (or refer me tutorial):

class {     int x;     string y;      public a(int x, string y) {        this.x = x;        this.y = y;     } } 

import a;  class b {     int f;     foo;      public b() {        this.foo = null;        this.f = -1;     }      public boolean createfoo() {        this.foo = new a(0, "tac example");        return true;     }       public static void main() {        b bar = new b();        baz = new a(666, "tac generation");        bar.createfoo();        bar.foo.y = "hello world";        if(bar.foo.x == 666)            return;        bar.foo.x = baz.x;                }         } 

first of need aware of "object layout" or in words how objects in memory (ram) in run-time. there no standard this, of compilers create object in similar manner. assume execution occur on x86 (32bit) machine pointers 4b or 32bits, while 64bit (x64) machine, pointers 8b. object "a" this: first 4bytes of "a" object pointer virtual pointer table. next 4bytes or offset 4 8 store "int x". offset 8 12 store pointer "string y". virtual pointer table class can empty or pointer on offset 0 in object can null - depends on compiler. class "b" situation similar. offset 0 store address of vpt (virtual pointer table), offset 4 "int f" , offset 8 pointer "a foo". in vpt of b "createfoo" address stored @ offset 0, method in b class. lets make implementation:

_b.createfoo:     beginfunc 12    // stack frame size = 3 registers * sizeof( each_register )     _t0 = 8         // size of object     pushparam _t0   // memory ammount asking     _t1 = lcall _alloc  // allocate memory     popparams 4     //  clear stack     _t2 =     *(_t1) = _t2    // load vpt     *(_t1 + 4) = 0  // initialize _a.x     *(_t1 + 8) = "tac example"  // initialize _a.foo     *(this + 8) = _t1     return 1     endfunc 

now let's implement main:

_b.main:     beginfunc 68    // 15 * 4 + 2 * 4      _t0 = 8         // size of b object     pushparam _t0   // memory amount need     _t1 = lcall _alloc  // allocate memory     popparams 4     //  clear stack     _t2 = b     *(_t1) = _t2    // load vpt     *(_t1 + 4) = 0  // initialize _b.foo     *(_t1 + 8) = -1 // initialize _b.f     bar = _t1     _t3 = 8         // size of object     pushparam _t3   // memory ammount asking     _t4 = lcall _alloc  // allocate memory     popparams 4     //  clear stack     _t5 =     *(_t4) = _t5    // load vpt     *(_t4 + 4) = 666    // initialize _a.x     *(_t4 + 8) = "tac generation"   // initialize _a.foo     baz = _t4     _t6 = *(bar)    // address of _b.vpt     _t7 = *(_t6)    // address of _b.createfoo     pushparam bar   // createfoo     acall _t7       // call _b.createfoo     popparams 4     // clear stack     _t8 = *(bar + 8)    // _b.foo     _t9 = *(_t8 + 8)    // _b.foo.y     *(_t9) = "hello world"  // set _b.foo.y value     _t10 = *(bar + 8)   // _b.foo     _t11 = *(_t10 + 4)  // _b.foo.x     _t12 = _t11 == 666  // test _b.foo.x equal 666     ifz _t12 goto _l0   // if not equal continue _l0     return     _l0:     _t13 = *(bar + 8)   // _b.foo     _t14 = _t13 + 4     // address of _b.foo.x     _t15 = *(baz + 4)   // _a.x     *(_t14) = _t15      // set _b.foo.x     endfunc 

as can see not hard, there work do. hope helps.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -