Programming-Language-Design / src / visitor / codegeneration / attribute_grammars / code_templates / AddressCG.txt
AddressCG.txt
Raw
Code function for Variable, ArrayIndexing and FieldAccess (expression l-values)

    address[[ArrayIndexing: exp1 -> exp2 exp3]] =
        address[[exp2]]
        value[[exp3]]
        <pushi> exp1.type.numberOfBytes()
        <muli>
        <addi>

    address[[Variable: exp -> ID]] =
        if(exp.definition.scope==0){ // -> Global variable
            <pusha> exp.definition.offset
        }else{
            <push bp>
            <pushi> exp.definition.offset
            <addi>
        }

    address[[FieldAccess: exp1 -> exp2 ID]]=
        address[[exp2]]
        int offsetRF = 0
        for(RecordField rf : exp2.definition.type.fields)
            if(rf.getName().equals(ID)
                offsetRF = rf.getOffset()
        <pushi> exp2.type.getField(ID).getOffset()
        <addi>