diff --git a/src/smallcc/smallcc-main.cpp b/src/smallcc/smallcc-main.cpp index 0ec8b06..7d01dfe 100644 --- a/src/smallcc/smallcc-main.cpp +++ b/src/smallcc/smallcc-main.cpp @@ -21,11 +21,27 @@ std::string getInputFile(llvm::StringRef filename) { int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv, "smallcc compiler\n"); - - outs() << " .globl main\n"; - outs() << "main:\n"; - printf(" li a0, %d\n", atoi(inputParsedString.c_str())); - printf(" ret\n"); - + outs() << ".globl main\n"; + std::string main= "main:\n" + " addi sp, sp, -16\n " + " sw ra, 12(sp)\n" + " sw s0, 8(sp)\n" + " addi s0, sp, 16\n" + " li a0, 0\n" + " sw a0, -16(s0)\n" + " sw a0, -12(s0)\n" + " lui a0, %hi(.LC0)\n" + " addi a0, a0, %lo(.LC0)\n" + " call printf\n" + " lw a0, -16(s0)\n" + " lw ra, 12(sp)\n" + " lw s0, 8(sp)\n" + " addi sp, sp, 16\n" + " ret\n"; + outs() << main; + outs() << ".section .rodata \n"; + outs() << ".align 3\n"; + printf("%s", ".LC0:\n"); + printf(" .string \"%d\"\n", atoi(inputParsedString.c_str())); // TODO: add } diff --git a/test/smallcc/input.c b/test/smallcc/input.c index a26fa66..82f16b0 100644 --- a/test/smallcc/input.c +++ b/test/smallcc/input.c @@ -1,7 +1,9 @@ -// RUN: smallcc -input-string="12" >& /tmp/test.s | -// RUN: /tmp/test.s FileCheck %s +//smallcc -input-string="123" > /tmp/test.s +// riscv64-unknown-linux-gnu-gcc /tmp/test.s -static -o /tmp/a.out +// qemu-riscv64 /tmp/a.out +// echo $? > /tmp/test.log >& /dev/null || true +// RUN: smallcc -input-string="123" > /tmp/test.s +// RUN: riscv64-unknown-linux-gnu-gcc /tmp/test.s -static -o /tmp/a.out +// RUN: qemu-riscv64 /tmp/a.out | FileCheck %s -// CHECK: .globl main -// CHECK-NEXT: main: -// CHECK-NEXT: li a0, 12 -// CHECK-NEXT: ret +// CHECK: 123