coins.casttohir
Class ToHirC2

java.lang.Object
  extended bycoins.casttohir.ToHirVisit
      extended bycoins.casttohir.ToHirC2

public class ToHirC2
extends ToHirVisit

Do error check, replace symbols, and convert expressions to HIR-C expressions that can be easily treated. Do not do cast for + - * / % << >> & | ^ because such cast should be done after the expansion of assignment operator += -= etc. Cast transformations for function parameter, initializer, and assignment statements are already done. In more detail, do followings.

 Set the type of node.
 Issue warning messages and error messages.
 Replace symbols that can be combined.
 Do following transformations:
   array[index] --> *(decay(array)+index)
   !v --> v==0
   If conditional expression in if-statement, etc. is a scalar
     expression, change it to a comparison expression.
   If comparison expression E or short circuit conditional expression E
     are included in a expression, then change E to E?1:0.
   Change expression statements as follows:
     a


Field Summary
 int fDbgLevel
           
protected  HIR hir
          HIR instance (used to create HIR objects).
protected  BlockStmt nowBlock
          Now processing block (used to create initializer).
protected  Sym sym
          Sym instance (used to create Sym objects).
protected  ToHir toHir
          Offers cooperation with the object of other packages.
 
Constructor Summary
ToHirC2(ToHir tohir)
           
 
Method Summary
protected  Exp atAdd(Exp e)
          At add expression node.
protected  Exp atAddAssign(Exp e)
          At add-assign expression node.
protected  Exp atAddr(Exp e)
          At address expression node.
protected  Exp atAnd(Exp e)
          At and expression node.
protected  Exp atAndAssign(Exp e)
          At and-assign expression node.
protected  Exp atArrow(Exp e)
          At arrow expression node.
protected  Exp atARShift(Exp e)
          At arithmetic R-shift expression node.
protected  Exp atAssign(Exp e)
          At assign expression node.
protected  void atAssignStmt(AssignStmt s)
          At assign statement node.
protected  void atBlock(BlockStmt s)
          At block statement node.
protected  Exp atCall(FunctionExp e)
          At function call expression node.
protected  Exp atCmpEq(Exp e)
          At EQ expression node.
protected  Exp atCmpGe(Exp e)
          At GE expression node.
protected  Exp atCmpGt(Exp e)
          At GT expression node.
protected  Exp atCmpLe(Exp e)
          At LE expression node.
protected  Exp atCmpLt(Exp e)
          At LT expression node.
protected  Exp atCmpNe(Exp e)
          At NE expression node.
protected  Exp atComma(Exp e)
          At comma expression node.
protected  Exp atConst(ConstNode e)
          At constant node.
protected  Exp atContents(Exp e)
          At indirection expression node.
protected  Exp atConv(Exp e)
          At cast expression node.
protected  Exp atDecay(Exp e)
          At decay expression node.
protected  Exp atDiv(Exp e)
          At div expression node.
protected  Exp atDivAssign(Exp e)
          At div-assign expression node.
protected  Exp atElem(ElemNode e)
          At element node.
protected  Exp atEqZero(Exp e)
          At logical-not expression node.
protected  Exp atExpList(ExpListExp e)
          At expression list node.
protected  Exp atExpRepeat(Exp e)
          At expression repeatation node.
protected  void atExpStmt(ExpStmt s)
          At expression statement node.
protected  void atFor(LoopStmt s)
          At for statement node.
protected  void atIf(IfStmt s)
          At if statement node.
protected  Exp atIndex(Exp e)
          At index expression node.
protected  InfStmt atInfStmt(InfStmt pInf)
          atInfStmt parses the pragma body in the form of String and change its symbols to instances of Sym (Var, Subp, Label, Const) and items enclosed in parenthesis to IrList changing its elements to Sym, etc.
protected  void atJump(JumpStmt s)
          At goto statement node.
protected  void atLabeledStmt(LabeledStmt s)
          At labeled statement node.
protected  Exp atLgAnd(Exp e)
          At logical-and expression node.
protected  Exp atLgOr(Exp e)
          At logical-or expression node.
protected  Exp atLShift(Exp e)
          At L-shift expression node.
protected  Exp atLShiftAssign(Exp e)
          At L-shift-assign expression node.
protected  Exp atMod(Exp e)
          At mod expression node.
protected  Exp atModAssign(Exp e)
          At mod-assign expression node.
protected  Exp atMul(Exp e)
          At mul expression node.
protected  Exp atMulAssign(Exp e)
          At mul-assign expression node.
protected  Exp atNeg(Exp e)
          At negative expression node.
protected  Exp atNot(Exp e)
          At not expression node.
protected  Exp atOffset(Exp e)
          At offset(difference of address) expression node.
protected  Exp atOr(Exp e)
          At or expression node.
protected  Exp atOrAssign(Exp e)
          At or-assign expression node.
protected  Exp atPost(int op, Exp e)
          At post-operator expression node.
protected  Exp atPre(int op, Exp e)
          At pre-operator expression node.
protected  Exp atQual(Exp e)
          At member-access expression node.
protected  void atReturn(ReturnStmt s)
          At return statement node.
protected  Exp atRShift(Exp e)
          At arithmetic R-shift expression node.
protected  Exp atRShiftAssign(Exp e)
          At R-shift-assign expression node.
protected  Exp atSelect(Exp e)
          At selection expression node.
protected  void atSetDataStmt(SetDataStmt s)
          At datacode statement node.
protected  Exp atSub(Exp e)
          At sub expression node.
protected  Exp atSubAssign(Exp e)
          At sub-assign expression node.
protected  Exp atSubp(SubpNode e)
          At function node.
protected  SubpDefinition atSubpDefinition(SubpDefinition s)
          At block statement node.
protected  Exp atSubs(Exp e)
          At subscript expression node.
protected  void atSwitch(SwitchStmt s)
          At switch statement node.
protected  Exp atUndecay(Exp e)
          At undecay expression node.
protected  void atUntil(LoopStmt s)
          At do-while statement node.
protected  Exp atVar(VarNode e)
          At variable node.
protected  void atWhile(LoopStmt s)
          At while statement node.
protected  Exp atXor(Exp e)
          At xor expression node.
protected  Exp atXorAssign(Exp e)
          At xor-assign expression node.
protected  boolean inInitBlock()
          Return true if now processing in the initialization block.
protected  void message(int level, java.lang.String mes)
          Output debug message.
(package private)  IrList processPragmaItem(ParseString pParseString, java.lang.String pNextItem, IrList pList)
           
(package private)  Exp visitExp(Exp e)
           
 void visitProgram()
          Visit HIR program tree.
(package private)  void visitProgram(Program program)
          Visit HIR program tree.
(package private)  void visitStmt(Stmt s)
          Call appropriate method by operator of statement node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

toHir

protected final ToHir toHir
Offers cooperation with the object of other packages.


hir

protected final HIR hir
HIR instance (used to create HIR objects).


sym

protected final Sym sym
Sym instance (used to create Sym objects).


nowBlock

protected BlockStmt nowBlock
Now processing block (used to create initializer).


fDbgLevel

public final int fDbgLevel
Constructor Detail

ToHirC2

public ToHirC2(ToHir tohir)
Method Detail

message

protected void message(int level,
                       java.lang.String mes)
Output debug message.

Overrides:
message in class ToHirVisit
Parameters:
level - Debug level.
mes - Debug message.

atIf

protected void atIf(IfStmt s)
At if statement node.

Overrides:
atIf in class ToHirVisit
Parameters:
s - IfStmt

atWhile

protected void atWhile(LoopStmt s)
At while statement node.

Overrides:
atWhile in class ToHirVisit
Parameters:
s - LoopStmt

atFor

protected void atFor(LoopStmt s)
At for statement node. If conditional expression contains expantion, it is put in the loop of 'for'.

Overrides:
atFor in class ToHirVisit
Parameters:
s - LoopStmt

atUntil

protected void atUntil(LoopStmt s)
At do-while statement node. If conditional expression contains expantion, it is put in the loop of 'do-while'.

Overrides:
atUntil in class ToHirVisit
Parameters:
s - LoopStmt

atSwitch

protected void atSwitch(SwitchStmt s)
At switch statement node. If conditional expression contains backword expantion, it is put out in front of 'switch'.

Overrides:
atSwitch in class ToHirVisit
Parameters:
s - SwitchStmt

atReturn

protected void atReturn(ReturnStmt s)
At return statement node.

Overrides:
atReturn in class ToHirVisit
Parameters:
s - ReturnStmt

atExpStmt

protected void atExpStmt(ExpStmt s)
At expression statement node.

Overrides:
atExpStmt in class ToHirVisit
Parameters:
s - ExpStmt

atSetDataStmt

protected void atSetDataStmt(SetDataStmt s)
At datacode statement node.

Overrides:
atSetDataStmt in class ToHirVisit
Parameters:
s - SetDataStmt

atVar

protected Exp atVar(VarNode e)
At variable node.

Overrides:
atVar in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atSubp

protected Exp atSubp(SubpNode e)
At function node.

Overrides:
atSubp in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atSubs

protected Exp atSubs(Exp e)
At subscript expression node.

Overrides:
atSubs in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atCall

protected Exp atCall(FunctionExp e)
At function call expression node.

Overrides:
atCall in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atAdd

protected Exp atAdd(Exp e)
At add expression node.
 Process
   arithmetic +/- arithmetic  (normal case)
   pointer +/- integral
   (The case pointer - pointer is treated in atOffset.)
 

Overrides:
atAdd in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atSub

protected Exp atSub(Exp e)
At sub expression node.

Overrides:
atSub in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atMul

protected Exp atMul(Exp e)
At mul expression node. (OP_DIV also come here) Both operands should be arithmetic type.

Overrides:
atMul in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atDiv

protected Exp atDiv(Exp e)
At div expression node.

Overrides:
atDiv in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atMod

protected Exp atMod(Exp e)
At mod expression node. Both operands should be arithmetic type.

Overrides:
atMod in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atAnd

protected Exp atAnd(Exp e)
At and expression node.

Overrides:
atAnd in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atOr

protected Exp atOr(Exp e)
At or expression node.

Overrides:
atOr in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atXor

protected Exp atXor(Exp e)
At xor expression node.

Overrides:
atXor in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atCmpEq

protected Exp atCmpEq(Exp e)
At EQ expression node.

Overrides:
atCmpEq in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atCmpNe

protected Exp atCmpNe(Exp e)
At NE expression node.

Overrides:
atCmpNe in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atCmpGt

protected Exp atCmpGt(Exp e)
At GT expression node.

Overrides:
atCmpGt in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atCmpGe

protected Exp atCmpGe(Exp e)
At GE expression node.

Overrides:
atCmpGe in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atCmpLt

protected Exp atCmpLt(Exp e)
At LT expression node.

Overrides:
atCmpLt in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atCmpLe

protected Exp atCmpLe(Exp e)
At LE expression node.

Overrides:
atCmpLe in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atLShift

protected Exp atLShift(Exp e)
At L-shift expression node.

Overrides:
atLShift in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atRShift

protected Exp atRShift(Exp e)
At arithmetic R-shift expression node.

Overrides:
atRShift in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atARShift

protected Exp atARShift(Exp e)
At arithmetic R-shift expression node.

Overrides:
atARShift in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atNot

protected Exp atNot(Exp e)
At not expression node.

Overrides:
atNot in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atNeg

protected Exp atNeg(Exp e)
At negative expression node.

Overrides:
atNeg in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atAddr

protected Exp atAddr(Exp e)
At address expression node.

Overrides:
atAddr in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atConv

protected Exp atConv(Exp e)
At cast expression node.

Overrides:
atConv in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atContents

protected Exp atContents(Exp e)
At indirection expression node.

Overrides:
atContents in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atAssign

protected Exp atAssign(Exp e)
At assign expression node.

Overrides:
atAssign in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atOffset

protected Exp atOffset(Exp e)
At offset(difference of address) expression node. Both operands should be pointer and their pointed types should be compatible with each other disregarding type qualifier.

Overrides:
atOffset in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atLgAnd

protected Exp atLgAnd(Exp e)
At logical-and expression node.

Overrides:
atLgAnd in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atLgOr

protected Exp atLgOr(Exp e)
At logical-or expression node.

Overrides:
atLgOr in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atSelect

protected Exp atSelect(Exp e)
At selection expression node.

Overrides:
atSelect in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atComma

protected Exp atComma(Exp e)
At comma expression node.

Overrides:
atComma in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atEqZero

protected Exp atEqZero(Exp e)
At logical-not expression node.

Overrides:
atEqZero in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atPre

protected Exp atPre(int op,
                    Exp e)
At pre-operator expression node.

Overrides:
atPre in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atPost

protected Exp atPost(int op,
                     Exp e)
At post-operator expression node.

Overrides:
atPost in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atAddAssign

protected Exp atAddAssign(Exp e)
At add-assign expression node.
 Process
   arithmetic += arithmetic
   pointer += integral
 

Overrides:
atAddAssign in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atSubAssign

protected Exp atSubAssign(Exp e)
At sub-assign expression node.

Overrides:
atSubAssign in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atMulAssign

protected Exp atMulAssign(Exp e)
At mul-assign expression node.
   arithmetic *= arithmetic
 

Overrides:
atMulAssign in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atDivAssign

protected Exp atDivAssign(Exp e)
At div-assign expression node.

Overrides:
atDivAssign in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atModAssign

protected Exp atModAssign(Exp e)
At mod-assign expression node.

Overrides:
atModAssign in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atLShiftAssign

protected Exp atLShiftAssign(Exp e)
At L-shift-assign expression node.

Overrides:
atLShiftAssign in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atRShiftAssign

protected Exp atRShiftAssign(Exp e)
At R-shift-assign expression node.

Overrides:
atRShiftAssign in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atAndAssign

protected Exp atAndAssign(Exp e)
At and-assign expression node.

Overrides:
atAndAssign in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atOrAssign

protected Exp atOrAssign(Exp e)
At or-assign expression node.

Overrides:
atOrAssign in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atXorAssign

protected Exp atXorAssign(Exp e)
At xor-assign expression node.

Overrides:
atXorAssign in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

atExpList

protected Exp atExpList(ExpListExp e)
At expression list node.

Overrides:
atExpList in class ToHirVisit
Parameters:
e - Exp
Returns:
Exp

inInitBlock

protected boolean inInitBlock()
Return true if now processing in the initialization block.

Returns:
boolean

visitProgram

public final void visitProgram()
Visit HIR program tree.


visitProgram

final void visitProgram(Program program)
Visit HIR program tree.

Parameters:
program - Program

visitStmt

final void visitStmt(Stmt s)
Call appropriate method by operator of statement node.

Parameters:
s - Visited statement.

atBlock

protected void atBlock(BlockStmt s)
At block statement node.

Parameters:
s - BlockStmt

atLabeledStmt

protected void atLabeledStmt(LabeledStmt s)
At labeled statement node.

Parameters:
s - LabeledStmt

atAssignStmt

protected void atAssignStmt(AssignStmt s)
At assign statement node.

Parameters:
s - AssignStmt

atJump

protected void atJump(JumpStmt s)
At goto statement node.

Parameters:
s - JumpStmt

visitExp

final Exp visitExp(Exp e)

atConst

protected Exp atConst(ConstNode e)
At constant node.

Parameters:
e - Exp
Returns:
Exp

atElem

protected Exp atElem(ElemNode e)
At element node.

Parameters:
e - Exp
Returns:
Exp

atIndex

protected Exp atIndex(Exp e)
At index expression node.

Parameters:
e - Exp
Returns:
Exp

atQual

protected Exp atQual(Exp e)
At member-access expression node.

Parameters:
e - Exp
Returns:
Exp

atArrow

protected Exp atArrow(Exp e)
At arrow expression node.

Parameters:
e - Exp
Returns:
Exp

atDecay

protected Exp atDecay(Exp e)
At decay expression node.

Parameters:
e - Exp
Returns:
Exp

atUndecay

protected Exp atUndecay(Exp e)
At undecay expression node.

Parameters:
e - Exp
Returns:
Exp

atExpRepeat

protected Exp atExpRepeat(Exp e)
At expression repeatation node.

Parameters:
e - Exp
Returns:
Exp

atSubpDefinition

protected SubpDefinition atSubpDefinition(SubpDefinition s)
At block statement node.

Parameters:
s - BlockStmt

atInfStmt

protected InfStmt atInfStmt(InfStmt pInf)
atInfStmt parses the pragma body in the form of String and change its symbols to instances of Sym (Var, Subp, Label, Const) and items enclosed in parenthesis to IrList changing its elements to Sym, etc. This may be called twice for the same pragma. In the second call, return pInf unchanged because it is already in the final form.

Returns:
InfStmt having Sym elements and (nested) IrList.

processPragmaItem

IrList processPragmaItem(ParseString pParseString,
                         java.lang.String pNextItem,
                         IrList pList)