From f8d884f5f9e000e830242469ce68bdc52e9b3790 Mon Sep 17 00:00:00 2001 From: Jeremy Rand Date: Fri, 13 May 2016 22:56:40 -0400 Subject: [PATCH] Get my first test source file compiling into valid ORCA/M assembly source. --- README.md | 33 ++++++++++++------- .../MCTargetDesc/WDC65816TargetStreamer.cpp | 30 ++++++++++++++++- .../MCTargetDesc/WDC65816TargetStreamer.h | 5 ++- lib/Target/WDC65816/WDC65816AsmPrinter.cpp | 6 ++++ 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d7bd0845..fe67bfe2 100644 --- a/README.md +++ b/README.md @@ -27,22 +27,33 @@ to describe the registers because we don’t have many) and the instruction set. That is in reasonable shape but there is definitely work there too. Right now, you can get the code and compile it. You will be left with a clang -binary that appears to have 65816 support. If you try to compile any C code -with it targeting the 65816 though, the compiler will crash today. But, as of -yesterday, I couldn’t even get a build so that is some progress. +binary that appears to have 65816 support. The following code will compile +successfully: - -What is next? -=========== -My initial goal is to get the compiler to produce assembly for this C code: - - int answerToTheUltimateQuestion(void) + int answer(void) { return 42; // The answer } -Once that is working, then I will try to get more working until it is a usable -compiler. There are _lots_ of issues and problems waiting for me (us?). +You can compile it with this commandline: + + bin/clang --target=wdc65816 -S -o answer.s answer.c + +I have copied that answer.s file to my Apple //GS and can assemble it using +ORCA/M and link it into an executable that works. Of course, it compiles +two opcodes (LDA and RTL) so not very impressive. + + +What is next? +=========== +My next goal is to get the compiler to produce assembly for this C code: + + int myGlobal=42; + + int answer2(void) + { + return myGlobal; // The answer + } How to build the project? diff --git a/lib/Target/WDC65816/MCTargetDesc/WDC65816TargetStreamer.cpp b/lib/Target/WDC65816/MCTargetDesc/WDC65816TargetStreamer.cpp index 8f920194..0b289e1e 100644 --- a/lib/Target/WDC65816/MCTargetDesc/WDC65816TargetStreamer.cpp +++ b/lib/Target/WDC65816/MCTargetDesc/WDC65816TargetStreamer.cpp @@ -59,8 +59,20 @@ void WDC65816TargetAsmStreamer::EmitKeepDirective(StringRef filename) void WDC65816TargetAsmStreamer::EmitSegStartDirective(StringRef filename) { + StringRef trimmedName = trimFilename(filename); + int col = 1; + + OS << "~"; + col++; + OS << trimFilename(filename); - OS << "seg start"; + col += trimmedName.size(); + + while (col < indentlen) { + OS << " "; + col++; + } + OS << " start"; OS << '\n'; } @@ -73,6 +85,22 @@ void WDC65816TargetAsmStreamer::EmitSegEndDirective(void) } +void WDC65816TargetAsmStreamer::EmitFunctionEntryLabel(StringRef function) +{ + int col = 1; + + OS << function; + col += function.size(); + + while (col < indentlen) { + OS << " "; + col++; + } + OS << " entry"; + OS << '\n'; +} + + void WDC65816TargetAsmStreamer::EmitInstruction(StringRef instruction) { instruction = instruction.ltrim(); diff --git a/lib/Target/WDC65816/MCTargetDesc/WDC65816TargetStreamer.h b/lib/Target/WDC65816/MCTargetDesc/WDC65816TargetStreamer.h index cb3dd328..28ec9c46 100644 --- a/lib/Target/WDC65816/MCTargetDesc/WDC65816TargetStreamer.h +++ b/lib/Target/WDC65816/MCTargetDesc/WDC65816TargetStreamer.h @@ -25,17 +25,19 @@ namespace llvm { virtual void EmitSegStartDirective(StringRef filename) = 0; virtual void EmitSegEndDirective(void) = 0; + virtual void EmitFunctionEntryLabel(StringRef function) = 0; virtual void EmitInstruction(StringRef instruction) = 0; }; class WDC65816TargetAsmStreamer : public WDC65816TargetStreamer { formatted_raw_ostream &OS; StringRef indent; + int indentlen; StringRef &trimFilename(StringRef &filename); public: - WDC65816TargetAsmStreamer(formatted_raw_ostream &OS) : OS(OS), indent(" ") {} + WDC65816TargetAsmStreamer(formatted_raw_ostream &OS) : OS(OS), indent(" "), indentlen(11) {} virtual ~WDC65816TargetAsmStreamer(); virtual void EmitCaseDirective(void); @@ -43,6 +45,7 @@ namespace llvm { virtual void EmitSegStartDirective(StringRef filename); virtual void EmitSegEndDirective(void); + virtual void EmitFunctionEntryLabel(StringRef function); virtual void EmitInstruction(StringRef instruction); }; } diff --git a/lib/Target/WDC65816/WDC65816AsmPrinter.cpp b/lib/Target/WDC65816/WDC65816AsmPrinter.cpp index d2dbe4cf..f7d6c7dd 100644 --- a/lib/Target/WDC65816/WDC65816AsmPrinter.cpp +++ b/lib/Target/WDC65816/WDC65816AsmPrinter.cpp @@ -55,6 +55,7 @@ namespace { virtual void EmitStartOfAsmFile(Module &module); virtual void EmitEndOfAsmFile(Module &module); + virtual void EmitFunctionEntryLabel(); virtual void EmitInstruction(const MachineInstr *MI) { SmallString<128> Str; @@ -134,6 +135,11 @@ bool WDC65816AsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum, } +void WDC65816AsmPrinter::EmitFunctionEntryLabel() { + WDC65816TargetStreamer &streamer = getTargetStreamer(); + streamer.EmitFunctionEntryLabel(CurrentFnSym->getName()); +} + // Force static initialization. extern "C" void LLVMInitializeWDC65816AsmPrinter() { RegisterAsmPrinter X(TheWDC65816Target);