|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object scale.clef.Clef2C
public final class Clef2C
A class to generate C code from a Clef AST.
$Id: Clef2C.java,v 1.4 2007-10-04 19:58:02 burrill Exp $
Copyright 2008 by the
Scale Compiler Group,
Department of Computer Science
University of Massachusetts,
Amherst MA. 01003, USA
All Rights Reserved.
This class is used primarily to generate C for declarations. However, it can also generate executable statements from a Clef AST.
Note, that as a side effect, Clef2C may change the Clef tree (for example, Clef2C changes the Clef tree to implement virtual functions).
scale.clef
Constructor Summary | |
---|---|
Clef2C(Emit emit,
SourceLanguage lang)
Construct a C language code generator - output goes to a file. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Clef2C(Emit emit, SourceLanguage lang)
emit
- is the Emit instance used to generate the C program.lang
- the source languageEmit
Method Detail |
---|
public java.lang.String fileExt()
.c
.
public final void codeGen(Node root)
root
- root node of the Clef treepublic void genCastType(Type t)
Person *Person = (Person *) 0;
public java.lang.String convertDeclName(Declaration decl, boolean address)
decl
- is the Declarationaddress
- is true if an address form of the name is neededpublic void genDeclarator(Type t, java.lang.String v)
On the first pass we traverse the type to find the simple
type. On the way back up the clef tree representing the type, we
generate pointer declarators. Then, we make another traversal of
the type tree and generate arrays and functions.
The routine
generates superfluous parens - many could be removed by making
simple checks for precedence relations.
t
- the clef type to generate in Cv
- the name of the defined objectpublic void genDeclaratorFull(Type t, java.lang.String v)
genDeclarator
in that the full procedure
declaration is generated.
public void visitNode(Node n)
visitNode
in interface Predicate
public void visitExceptionDecl(ExceptionDecl d)
visitExceptionDecl
in interface DeclPredicate
public void visitCaseLabelDecl(CaseLabelDecl d)
visitCaseLabelDecl
in interface DeclPredicate
public void visitDeclaration(Declaration n)
visitDeclaration
in interface DeclPredicate
public void visitRoutineDecl(RoutineDecl d)
visitRoutineDecl
in interface DeclPredicate
public void visitRealType(RealType d)
visitRealType
in interface TypePredicate
public void visitRaiseWithType(RaiseWithType d)
visitRaiseWithType
in interface TypePredicate
public void visitRaiseWithObject(RaiseWithObject d)
visitRaiseWithObject
in interface TypePredicate
public void visitRaise(Raise d)
visitRaise
in interface TypePredicate
public void visitNumericType(NumericType d)
visitNumericType
in interface TypePredicate
public void visitCompositeType(CompositeType d)
visitCompositeType
in interface TypePredicate
public void visitAtomicType(AtomicType d)
visitAtomicType
in interface TypePredicate
public void visitAltCase(AltCase d)
visitAltCase
in interface StmtPredicate
public void visitIfStmt(IfStmt d)
visitIfStmt
in interface StmtPredicate
public void visitTestLoopStmt(TestLoopStmt d)
visitTestLoopStmt
in interface StmtPredicate
public void visitLoopStmt(LoopStmt d)
visitLoopStmt
in interface StmtPredicate
public void visitStatement(Statement d)
visitStatement
in interface StmtPredicate
public void visitVarArgOp(VarArgOp d)
visitVarArgOp
in interface ExprPredicate
public void visitTernaryOp(TernaryOp d)
visitTernaryOp
in interface ExprPredicate
public void visitSubstringOp(SubstringOp d)
visitSubstringOp
in interface ExprPredicate
public void visitIncrementOp(IncrementOp d)
visitIncrementOp
in interface ExprPredicate
public void visitHeapOp(HeapOp d)
visitHeapOp
in interface ExprPredicate
public void visitDyadicOp(DyadicOp d)
visitDyadicOp
in interface ExprPredicate
public void visitDeleteOp(DeleteOp d)
visitDeleteOp
in interface ExprPredicate
public void visitDeleteArrayOp(DeleteArrayOp d)
visitDeleteArrayOp
in interface ExprPredicate
public void visitCompoundAssignmentOp(CompoundAssignmentOp d)
visitCompoundAssignmentOp
in interface ExprPredicate
public void visitCallOp(CallOp d)
visitCallOp
in interface ExprPredicate
public void visitAssignmentOp(AssignmentOp d)
visitAssignmentOp
in interface ExprPredicate
public void visitMonadicOp(MonadicOp d)
visitMonadicOp
in interface ExprPredicate
public void visitAllocateSettingFieldsOp(AllocateSettingFieldsOp d)
visitAllocateSettingFieldsOp
in interface ExprPredicate
public void visitAllocatePlacementOp(AllocatePlacementOp d)
visitAllocatePlacementOp
in interface ExprPredicate
public void visitAggregateOp(AggregateOp d)
visitAggregateOp
in interface ExprPredicate
public void visitTypeDecl(TypeDecl td)
TypeDecl
node) to determine the name. We need to do this
because different types place names in different places.
visitTypeDecl
in interface DeclPredicate
public void genTypeDecl(TypeDecl td)
TypeDecl
.
public void visitTypeName(TypeName tn)
visitTypeName
in interface DeclPredicate
public void visitValueDecl(ValueDecl v)
visitValueDecl
in interface DeclPredicate
public void visitLabelDecl(LabelDecl n)
visitLabelDecl
in interface DeclPredicate
public void visitVariableDecl(VariableDecl d)
visitVariableDecl
in interface DeclPredicate
public void visitRenamedVariableDecl(RenamedVariableDecl n)
visitRenamedVariableDecl
in interface DeclPredicate
public void visitEquivalenceDecl(EquivalenceDecl ed)
EQUIVALENCE
relations. The equivalenced array is converted to a C pointer
variable that is initialized to an address using the equivalence
relationship.
visitEquivalenceDecl
in interface DeclPredicate
public void visitFormalDecl(FormalDecl fd)
visitValueDecl
to generate the
declaration). The default value is handled by the caller.
visitFormalDecl
in interface DeclPredicate
fd
- the FormalDecl nodepublic void visitUnknownFormals(UnknownFormals n)
visitUnknownFormals
in interface DeclPredicate
public void visitFieldDecl(FieldDecl fd)
visitFieldDecl
in interface DeclPredicate
public void genRoutineAttributes(RoutineDecl p)
static
) for a function.
public void visitProcedureDecl(ProcedureDecl p)
visitProcedureDecl
in interface DeclPredicate
public void genForwardRoutineDecl(RoutineDecl p)
public void visitForwardProcedureDecl(ForwardProcedureDecl n)
visitForwardProcedureDecl
in interface DeclPredicate
public void visitFileDecl(FileDecl n)
visitFileDecl
in interface DeclPredicate
public void visitEnumElementDecl(EnumElementDecl d)
visitEnumElementDecl
in interface DeclPredicate
public void visitType(Type n)
visitType
in interface TypePredicate
public void visitVoidType(VoidType n)
visitVoidType
in interface TypePredicate
public void visitRefType(RefType ref)
RefType
. No
type code is actually generated for a RefType
node. Instead, the RefType
node may contain attributes
which we need to generate code for. The order that the
attributes are generated depends upon the real type that the
RefType
contains.
visitRefType
in interface TypePredicate
public void visitProcedureType(ProcedureType p)
visitProcedureType
in interface TypePredicate
p
- is the clef node for a ProcedureTypepublic void visitIncompleteType(IncompleteType n)
visitIncompleteType
in interface TypePredicate
public void visitPointerType(PointerType p)
int * const const_pointer const int *pointer_to_const;. Note: we make multple passes over the type. The generated code depends upon which pass.
visitPointerType
in interface TypePredicate
p
- the pointer type clef nodepublic void visitArrayType(ArrayType at)
To generate code for arrays, we may need to make two passes. One the first pass, we generate the element type. On the second pass, we generate the array index expression. In between, we generate the name of the array.
visitArrayType
in interface TypePredicate
at
- is an array typepublic void visitAggregateType(AggregateType agg)
We assume that the aggregate head has already been generated via a call to genTypeNameProlog() and that the enclosing braces are generated.
visitAggregateType
in interface TypePredicate
RecordType
,
UnionType
public void visitComplexType(ComplexType type)
visitComplexType
in interface TypePredicate
public void visitBound(Bound b)
visitBound
in interface TypePredicate
public void visitFixedArrayType(FixedArrayType n)
visitFixedArrayType
in interface TypePredicate
n
- a FixedArray Clef nodepublic void visitAllocArrayType(AllocArrayType n)
visitAllocArrayType
in interface TypePredicate
public void visitRecordType(RecordType rt)
visitRecordType
in interface TypePredicate
visitAggregateType(scale.clef.type.AggregateType)
public void visitFloatType(FloatType type)
visitFloatType
in interface TypePredicate
public void visitIntegerType(IntegerType type)
visitIntegerType
in interface TypePredicate
public void visitSignedIntegerType(SignedIntegerType type)
visitSignedIntegerType
in interface TypePredicate
public void visitUnsignedIntegerType(UnsignedIntegerType type)
visitUnsignedIntegerType
in interface TypePredicate
public void visitFortranCharType(FortranCharType type)
visitFortranCharType
in interface TypePredicate
public void visitEnumerationType(EnumerationType et)
visitEnumerationType
in interface TypePredicate
public void visitUnionType(UnionType ut)
visitUnionType
in interface TypePredicate
public void visitCharacterType(CharacterType ct)
visitCharacterType
in interface TypePredicate
public void visitBooleanType(BooleanType n)
visitBooleanType
in interface TypePredicate
public void visitBlockStmt(BlockStmt stmt)
visitBlockStmt
in interface StmtPredicate
public void visitMultiBranchStmt(MultiBranchStmt s)
visitMultiBranchStmt
in interface StmtPredicate
public void visitIfThenElseStmt(IfThenElseStmt ifstmt)
visitIfThenElseStmt
in interface StmtPredicate
public void visitArithmeticIfStmt(ArithmeticIfStmt stmt)
if (e == 0) goto L_Equal; else if (e < 0) goto L_Less; } else goto L_Greater;
visitArithmeticIfStmt
in interface StmtPredicate
public void visitComputedGotoStmt(ComputedGotoStmt n)
visitComputedGotoStmt
in interface StmtPredicate
public void visitAssignLabelStmt(AssignLabelStmt s)
visitAssignLabelStmt
in interface StmtPredicate
public void visitAssignedGotoStmt(AssignedGotoStmt n)
visitAssignedGotoStmt
in interface StmtPredicate
public void visitCaseStmt(CaseStmt stmt)
visitCaseStmt
in interface StmtPredicate
public void visitSwitchStmt(SwitchStmt stmt)
visitSwitchStmt
in interface StmtPredicate
public void visitWhileLoopStmt(WhileLoopStmt stmt)
visitWhileLoopStmt
in interface StmtPredicate
public void visitRepeatWhileLoopStmt(RepeatWhileLoopStmt stmt)
visitRepeatWhileLoopStmt
in interface StmtPredicate
public void visitRepeatUntilLoopStmt(RepeatUntilLoopStmt stmt)
visitRepeatUntilLoopStmt
in interface StmtPredicate
public void visitDoLoopStmt(DoLoopStmt stmt)
visitDoLoopStmt
in interface StmtPredicate
public void visitForLoopStmt(ForLoopStmt stmt)
visitForLoopStmt
in interface StmtPredicate
public void visitBreakStmt(BreakStmt n)
visitBreakStmt
in interface StmtPredicate
public void visitContinueStmt(ContinueStmt n)
visitContinueStmt
in interface StmtPredicate
public void visitGotoStmt(GotoStmt n)
visitGotoStmt
in interface StmtPredicate
public void visitReturnStmt(ReturnStmt n)
visitReturnStmt
in interface StmtPredicate
public void visitExitStmt(ExitStmt n)
visitExitStmt
in interface StmtPredicate
public void visitEvalStmt(EvalStmt n)
visitEvalStmt
in interface StmtPredicate
public void visitDeclStmt(DeclStmt n)
visitDeclStmt
in interface StmtPredicate
public void visitNullStmt(NullStmt n)
visitNullStmt
in interface StmtPredicate
public void visitLabelStmt(LabelStmt s)
visitLabelStmt
in interface StmtPredicate
public void visitExpression(Expression n)
visitExpression
in interface ExprPredicate
public void visitLiteral(Literal l)
visitLiteral
in interface ExprPredicate
l
- the literal nodepublic void visitStringLiteral(StringLiteral n)
visitStringLiteral
in interface ExprPredicate
public void visitBooleanLiteral(BooleanLiteral n)
visitBooleanLiteral
in interface ExprPredicate
public void visitCharLiteral(CharLiteral n)
visitCharLiteral
in interface ExprPredicate
public void visitIntLiteral(IntLiteral n)
visitIntLiteral
in interface ExprPredicate
public void visitIntArrayLiteral(IntArrayLiteral l)
visitIntArrayLiteral
in interface ExprPredicate
public void visitFloatLiteral(FloatLiteral n)
visitFloatLiteral
in interface ExprPredicate
public void visitComplexLiteral(ComplexLiteral n)
visitComplexLiteral
in interface ExprPredicate
public void visitFloatArrayLiteral(FloatArrayLiteral l)
visitFloatArrayLiteral
in interface ExprPredicate
public void visitSizeofLiteral(SizeofLiteral sl)
visitSizeofLiteral
in interface ExprPredicate
public void visitAddressLiteral(AddressLiteral sl)
visitAddressLiteral
in interface ExprPredicate
public void visitIdAddressOp(IdAddressOp id)
visitIdAddressOp
in interface ExprPredicate
public void visitIdValueOp(IdValueOp id)
visitIdValueOp
in interface ExprPredicate
public void visitIdReferenceOp(IdReferenceOp id)
visitIdReferenceOp
in interface ExprPredicate
public void visitSeriesOp(SeriesOp expr)
visitSeriesOp
in interface ExprPredicate
public void visitParenthesesOp(ParenthesesOp expr)
visitParenthesesOp
in interface ExprPredicate
public void visitAggregationElements(AggregationElements agg)
We use several stages to generate the C code.
visitAggregationElements
in interface ExprPredicate
public void visitAssignSimpleOp(AssignSimpleOp a)
visitAssignSimpleOp
in interface ExprPredicate
public void visitDefOp(DefOp expr)
visitDefOp
in interface ExprPredicate
public void visitPositiveOp(PositiveOp expr)
visitPositiveOp
in interface ExprPredicate
public static char simpleTypeName(java.lang.String name)
public void genIntrinsicOp(java.lang.String op, Type t1, Expression e1, Type t2, Expression e2)
public void visitNegativeOp(NegativeOp expr)
visitNegativeOp
in interface ExprPredicate
public void visitTranscendentalOp(TranscendentalOp expr)
visitTranscendentalOp
in interface ExprPredicate
public void visitTranscendental2Op(Transcendental2Op expr)
visitTranscendental2Op
in interface ExprPredicate
public void visitAbsoluteValueOp(AbsoluteValueOp expr)
visitAbsoluteValueOp
in interface ExprPredicate
public void visitMinimumOp(MinimumOp expr)
visitMinimumOp
in interface ExprPredicate
public void visitMaximumOp(MaximumOp expr)
visitMaximumOp
in interface ExprPredicate
public void visitAdditionOp(AdditionOp expr)
visitAdditionOp
in interface ExprPredicate
public void visitSubtractionOp(SubtractionOp expr)
visitSubtractionOp
in interface ExprPredicate
public void visitMultiplicationOp(MultiplicationOp expr)
visitMultiplicationOp
in interface ExprPredicate
public void visitDivisionOp(DivisionOp expr)
visitDivisionOp
in interface ExprPredicate
public void visitModulusOp(ModulusOp expr)
visitModulusOp
in interface ExprPredicate
public void visitRemainderOp(RemainderOp expr)
visitRemainderOp
in interface ExprPredicate
public void visitExponentiationOp(ExponentiationOp expr)
visitExponentiationOp
in interface ExprPredicate
public void visitPreDecrementOp(PreDecrementOp expr)
visitPreDecrementOp
in interface ExprPredicate
public void visitPreIncrementOp(PreIncrementOp expr)
visitPreIncrementOp
in interface ExprPredicate
public void visitPostDecrementOp(PostDecrementOp expr)
visitPostDecrementOp
in interface ExprPredicate
public void visitPostIncrementOp(PostIncrementOp expr)
visitPostIncrementOp
in interface ExprPredicate
public void visitEqualityOp(EqualityOp expr)
visitEqualityOp
in interface ExprPredicate
public void visitNotEqualOp(NotEqualOp expr)
visitNotEqualOp
in interface ExprPredicate
public void visitGreaterOp(GreaterOp expr)
visitGreaterOp
in interface ExprPredicate
public void visitGreaterEqualOp(GreaterEqualOp expr)
visitGreaterEqualOp
in interface ExprPredicate
public void visitLessOp(LessOp expr)
visitLessOp
in interface ExprPredicate
public void visitLessEqualOp(LessEqualOp expr)
visitLessEqualOp
in interface ExprPredicate
public void visitBitComplementOp(BitComplementOp expr)
visitBitComplementOp
in interface ExprPredicate
public void visitBitAndOp(BitAndOp expr)
visitBitAndOp
in interface ExprPredicate
public void visitBitXorOp(BitXorOp expr)
visitBitXorOp
in interface ExprPredicate
public void visitBitOrOp(BitOrOp expr)
visitBitOrOp
in interface ExprPredicate
public void visitBitShiftOp(BitShiftOp expr)
visitBitShiftOp
in interface ExprPredicate
public void visitMultiplicationAssignmentOp(MultiplicationAssignmentOp expr)
visitMultiplicationAssignmentOp
in interface ExprPredicate
public void visitDivisionAssignmentOp(DivisionAssignmentOp expr)
visitDivisionAssignmentOp
in interface ExprPredicate
public void visitRemainderAssignmentOp(RemainderAssignmentOp expr)
visitRemainderAssignmentOp
in interface ExprPredicate
public void visitAdditionAssignmentOp(AdditionAssignmentOp a)
visitAdditionAssignmentOp
in interface ExprPredicate
public void visitSubtractionAssignmentOp(SubtractionAssignmentOp a)
visitSubtractionAssignmentOp
in interface ExprPredicate
public void visitBitShiftAssignmentOp(BitShiftAssignmentOp a)
visitBitShiftAssignmentOp
in interface ExprPredicate
public void visitBitAndAssignmentOp(BitAndAssignmentOp a)
visitBitAndAssignmentOp
in interface ExprPredicate
public void visitBitXorAssignmentOp(BitXorAssignmentOp a)
visitBitXorAssignmentOp
in interface ExprPredicate
public void visitBitOrAssignmentOp(BitOrAssignmentOp a)
visitBitOrAssignmentOp
in interface ExprPredicate
public void visitNotOp(NotOp expr)
visitNotOp
in interface ExprPredicate
public void visitAndOp(AndOp expr)
visitAndOp
in interface ExprPredicate
public void visitOrOp(OrOp expr)
visitOrOp
in interface ExprPredicate
public void visitAndConditionalOp(AndConditionalOp expr)
visitAndConditionalOp
in interface ExprPredicate
public void visitOrConditionalOp(OrConditionalOp expr)
visitOrConditionalOp
in interface ExprPredicate
public void visitExpressionIfOp(ExpressionIfOp expr)
visitExpressionIfOp
in interface ExprPredicate
public void visitAddressOp(AddressOp expr)
visitAddressOp
in interface ExprPredicate
public void visitDereferenceOp(DereferenceOp expr)
visitDereferenceOp
in interface ExprPredicate
public void visitNilOp(NilOp n)
visitNilOp
in interface ExprPredicate
public void visitThisOp(ThisOp n)
visitThisOp
in interface ExprPredicate
public void visitSelectOp(SelectOp expr)
visitSelectOp
in interface ExprPredicate
public void visitSelectIndirectOp(SelectIndirectOp expr)
visitSelectIndirectOp
in interface ExprPredicate
public void visitSubscriptValueOp(SubscriptValueOp n)
visitSubscriptValueOp
in interface ExprPredicate
public void visitSubscriptAddressOp(SubscriptAddressOp n)
visitSubscriptAddressOp
in interface ExprPredicate
public void visitSubscriptOp(SubscriptOp expr)
visitSubscriptOp
in interface ExprPredicate
public void visitCallFunctionOp(CallFunctionOp fun)
visitCallFunctionOp
in interface ExprPredicate
public void visitTypeConversionOp(TypeConversionOp t)
We do not generate code for cast conversions to
aggregate types since these are illegal in C. For an
ArrayType
, we generate a cast
to a pointer to the array's element type (we should only see this
at function calls).
visitTypeConversionOp
in interface ExprPredicate
public void visitComplexOp(ComplexOp t)
visitComplexOp
in interface ExprPredicate
InternalError
- for errorspublic void visitVaStartOp(VaStartOp vas)
va_start(va_list,
parmN)
construct.
visitVaStartOp
in interface ExprPredicate
public void visitVaArgOp(VaArgOp va)
va_arg(va_list,
type)
construct.
visitVaArgOp
in interface ExprPredicate
public void visitVaCopyOp(VaCopyOp vac)
visitVaCopyOp
in interface ExprPredicate
public void visitStatementOp(StatementOp so)
visitStatementOp
in interface ExprPredicate
public void visitVaEndOp(VaEndOp vae)
va_end(va_list)
construct.
visitVaEndOp
in interface ExprPredicate
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |