Get my first test source file compiling into valid ORCA/M assembly source.

This commit is contained in:
Jeremy Rand 2016-05-13 22:56:40 -04:00
parent 4cc95195f4
commit f8d884f5f9
4 changed files with 61 additions and 13 deletions

View File

@ -27,22 +27,33 @@ to describe the registers because we dont have many) and the instruction set.
That is in reasonable shape but there is definitely work there too. 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 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 binary that appears to have 65816 support. The following code will compile
with it targeting the 65816 though, the compiler will crash today. But, as of successfully:
yesterday, I couldnt even get a build so that is some progress.
int answer(void)
What is next?
===========
My initial goal is to get the compiler to produce assembly for this C code:
int answerToTheUltimateQuestion(void)
{ {
return 42; // The answer return 42; // The answer
} }
Once that is working, then I will try to get more working until it is a usable You can compile it with this commandline:
compiler. There are _lots_ of issues and problems waiting for me (us?).
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? How to build the project?

View File

@ -59,8 +59,20 @@ void WDC65816TargetAsmStreamer::EmitKeepDirective(StringRef filename)
void WDC65816TargetAsmStreamer::EmitSegStartDirective(StringRef filename) void WDC65816TargetAsmStreamer::EmitSegStartDirective(StringRef filename)
{ {
StringRef trimmedName = trimFilename(filename);
int col = 1;
OS << "~";
col++;
OS << trimFilename(filename); OS << trimFilename(filename);
OS << "seg start"; col += trimmedName.size();
while (col < indentlen) {
OS << " ";
col++;
}
OS << " start";
OS << '\n'; 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) void WDC65816TargetAsmStreamer::EmitInstruction(StringRef instruction)
{ {
instruction = instruction.ltrim(); instruction = instruction.ltrim();

View File

@ -25,17 +25,19 @@ namespace llvm {
virtual void EmitSegStartDirective(StringRef filename) = 0; virtual void EmitSegStartDirective(StringRef filename) = 0;
virtual void EmitSegEndDirective(void) = 0; virtual void EmitSegEndDirective(void) = 0;
virtual void EmitFunctionEntryLabel(StringRef function) = 0;
virtual void EmitInstruction(StringRef instruction) = 0; virtual void EmitInstruction(StringRef instruction) = 0;
}; };
class WDC65816TargetAsmStreamer : public WDC65816TargetStreamer { class WDC65816TargetAsmStreamer : public WDC65816TargetStreamer {
formatted_raw_ostream &OS; formatted_raw_ostream &OS;
StringRef indent; StringRef indent;
int indentlen;
StringRef &trimFilename(StringRef &filename); StringRef &trimFilename(StringRef &filename);
public: public:
WDC65816TargetAsmStreamer(formatted_raw_ostream &OS) : OS(OS), indent(" ") {} WDC65816TargetAsmStreamer(formatted_raw_ostream &OS) : OS(OS), indent(" "), indentlen(11) {}
virtual ~WDC65816TargetAsmStreamer(); virtual ~WDC65816TargetAsmStreamer();
virtual void EmitCaseDirective(void); virtual void EmitCaseDirective(void);
@ -43,6 +45,7 @@ namespace llvm {
virtual void EmitSegStartDirective(StringRef filename); virtual void EmitSegStartDirective(StringRef filename);
virtual void EmitSegEndDirective(void); virtual void EmitSegEndDirective(void);
virtual void EmitFunctionEntryLabel(StringRef function);
virtual void EmitInstruction(StringRef instruction); virtual void EmitInstruction(StringRef instruction);
}; };
} }

View File

@ -55,6 +55,7 @@ namespace {
virtual void EmitStartOfAsmFile(Module &module); virtual void EmitStartOfAsmFile(Module &module);
virtual void EmitEndOfAsmFile(Module &module); virtual void EmitEndOfAsmFile(Module &module);
virtual void EmitFunctionEntryLabel();
virtual void EmitInstruction(const MachineInstr *MI) { virtual void EmitInstruction(const MachineInstr *MI) {
SmallString<128> Str; 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. // Force static initialization.
extern "C" void LLVMInitializeWDC65816AsmPrinter() { extern "C" void LLVMInitializeWDC65816AsmPrinter() {
RegisterAsmPrinter<WDC65816AsmPrinter> X(TheWDC65816Target); RegisterAsmPrinter<WDC65816AsmPrinter> X(TheWDC65816Target);