-
JavaCC컴파일러/코드 생성 2013. 7. 27. 17:20
- 어셈블리 오브젝트
Literal
<- IntegerLiteral
<- Symbol
<- BaseSymbol
<- NamedSymbol
<- UnnamedSymbol
<- SuffixedSymbol
Operand // 인스트럭션의 오퍼랜드
<- ImmediateValue
<- MemoryReference
<- DirectMemoryReference
전역 변수?
문자열 리터럴?
<- IndirectMemoryReference <= Addr
인수
(offset, %ebp)
지역 변수?
<- Register
<- AbsoluteAddress
Assembly
<- Comment
<- Directive
<- Instruction
<- Label
- x86 어셈블리 DSL
AssemblyCode
private List<Assembly> assemblies = new ArrayList<Assembly>();
CodeGenerator
.imm() // ImmediateValue
.ax() // Register
.al() // Register
.bx() // Register
.cx() // Register
.cl() // Register
.dx() // Register
.mem() // DirectMemoryReference|IndirectMemoryReference
AssemblyCode
._file()
._text()
._section()
.comment()
._globl()
._type()
.label()
._size()
.push()
.add()
.sub()
.imul()
.cltd()
.idiv()
.mov()
.div()
.and()
.or()
.xor()
.sal()
.shr()
.sar()
.cmp()
.sete()
.setne()
.setg()
.setge()
.setl()
.setle()
.seta()
.setae()
.setb()
.setbe()
.movzx()
*
private AssemblyCode CodeGenerator::generateAssemblyCode(IR ir) {
AssemblyCode file = newAssemblyCode();
file._file(ir.fileName());
if (ir.isGlobalVariableDefined()) { // compileGlobalVariable
generateDataSection(file, ir.definedGlobalVariables());
}
if (ir.isStringLiteralDefined()) { // compileStringLiteral
generateReadOnlyDataSection(file, ir.constantTable());
}
if (ir.isFunctionDefined()) { // compileFunction
generateTextSection(file, ir.definedFunctions());
}
if (ir.isCommonSymbolDefined()) { // compileCommonSymbol
generateCommonSymbols(file, ir.definedCommonSymbols());
}
if (options.isPositionIndependent()) { // PICThunk
PICThunk(file, GOTBaseReg());
}
return file;
}
public AssemblyCode CodeGenerator::generate(IR ir) {
locateSymbols(ir);
return generateAssemblyCode(ir); // compileIR
}
public AssemblyCode Compiler::generateAssembly(IR ir, Options opts) {
return opts.codeGenerator(errorHandler).generate(ir);
}
public void Compiler::compile(String srcPath, String destPath, Options opts) throws CompileException {
AST ast = parseFile(srcPath, opts);
TypeTable types = opts.typeTable();
AST sem = semanticAnalyze(ast, types, opts);
IR ir = new IRGenerator(errorHandler).generate(sem, types);
String asm = generateAssembly(ir, opts);
writeFile(destPath, asm);
}
public void Compiler::build(List<SourceFile> srcs, Options opts) throws CompileException {
for (SourceFile src :srcs) {
compile(src.path(), opts.asmFileNameOf(src), opts);
assemble(src.path(), opts.objFileNameOf(src), opts);
}
link(opts);
}