fix bug of big stack cost
This commit is contained in:
2
Makefile
2
Makefile
@ -1,6 +1,7 @@
|
||||
# 定义变量
|
||||
BUILD_DIR := makebuild # 构建目录
|
||||
CMAKE_BUILD_TYPE := Release # 构建类型,可以是 Release 或 Debug
|
||||
BUILTIN_ASM := src/IR/builtin.s # 内置汇编文件
|
||||
|
||||
# 默认目标
|
||||
all: build
|
||||
@ -14,6 +15,7 @@ build:
|
||||
# 运行目标,运行生成的可执行文件
|
||||
run:
|
||||
@cd $(BUILD_DIR) && ./zmxcc /dev/stdin -o /dev/stdout 2>/dev/null
|
||||
@cat $(BUILTIN_ASM) >>/dev/stdout
|
||||
|
||||
# 清理目标
|
||||
clean:
|
||||
|
@ -12,6 +12,7 @@ class IRBuilder : public ASTNodeVirturalVisitor {
|
||||
std::shared_ptr<ModuleItem> prog;
|
||||
std::shared_ptr<TypeDefItem> cur_class;
|
||||
std::shared_ptr<FunctionDefItem> cur_func;
|
||||
std::shared_ptr<BlockItem> cur_alloca_block;
|
||||
std::shared_ptr<BlockItem> cur_block;
|
||||
std::shared_ptr<BlockItem> main_init_block;
|
||||
std::string cur_class_name;
|
||||
|
39
include/naivebackend/naivebackend.h
Normal file
39
include/naivebackend/naivebackend.h
Normal file
@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
#include <ios>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include "IR/IR_basic.h"
|
||||
class RISCVAsmItemBase {
|
||||
public:
|
||||
RISCVAsmItemBase() = default;
|
||||
virtual ~RISCVAsmItemBase() = default;
|
||||
virtual void RecursivePrint(std::ostream &os) const = 0;
|
||||
};
|
||||
namespace NaiveBackend {
|
||||
class RISCVConstStrItem : public RISCVAsmItemBase {
|
||||
public:
|
||||
RISCVConstStrItem() = default;
|
||||
~RISCVConstStrItem() = default;
|
||||
void RecursivePrint(std::ostream &os) const override;
|
||||
};
|
||||
class RISCVGlobalVarItem : public RISCVAsmItemBase {
|
||||
public:
|
||||
RISCVGlobalVarItem() = default;
|
||||
~RISCVGlobalVarItem() = default;
|
||||
void RecursivePrint(std::ostream &os) const override;
|
||||
};
|
||||
class RISCVFuncItem : public RISCVAsmItemBase {
|
||||
public:
|
||||
RISCVFuncItem() = default;
|
||||
~RISCVFuncItem() = default;
|
||||
void RecursivePrint(std::ostream &os) const override;
|
||||
};
|
||||
class RISCVProgItem : public RISCVAsmItemBase {
|
||||
public:
|
||||
RISCVProgItem() = default;
|
||||
~RISCVProgItem() = default;
|
||||
void RecursivePrint(std::ostream &os) const override;
|
||||
};
|
||||
}
|
||||
void GenerateNaiveASM(std::ostream &os, std::shared_ptr<ModuleItem> prog);
|
@ -1,8 +1,9 @@
|
||||
add_subdirectory(ast)
|
||||
add_subdirectory(semantic)
|
||||
add_subdirectory(IR)
|
||||
add_subdirectory(naivebackend)
|
||||
add_executable(zmxcc main.cpp)
|
||||
target_link_libraries(zmxcc semantic argparse IR)
|
||||
target_link_libraries(zmxcc semantic argparse IR naivebackend)
|
||||
set_target_properties(zmxcc PROPERTIES
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
|
||||
)
|
@ -72,7 +72,9 @@ void IRBuilder::ActuralVisit(FuncDef_ASTNode *node) {
|
||||
std::dynamic_pointer_cast<UNConditionJMPAction>(cur_func->init_block->exit_action)->label_full =
|
||||
current_block->label_full;
|
||||
is_in_func_def = true;
|
||||
cur_alloca_block=func_def->init_block;
|
||||
node->func_body->accept(this);
|
||||
cur_alloca_block=main_init_block;
|
||||
is_in_func_def = false;
|
||||
|
||||
if (!(cur_block->exit_action)) {
|
||||
@ -101,6 +103,7 @@ void IRBuilder::ActuralVisit(ClassDef_ASTNode *node) {
|
||||
}
|
||||
|
||||
void IRBuilder::ActuralVisit(Program_ASTNode *node) {
|
||||
cur_alloca_block=main_init_block;
|
||||
for (auto ch : node->sorted_children) {
|
||||
ch->accept(this);
|
||||
}
|
||||
@ -149,7 +152,7 @@ void IRBuilder::ActuralVisit(DefinitionStatement_ASTNode *node) {
|
||||
} else {
|
||||
for (const auto &var : node->vars) {
|
||||
auto var_def = std::make_shared<AllocaAction>();
|
||||
cur_block->actions.push_back(var_def);
|
||||
cur_alloca_block->actions.push_back(var_def);
|
||||
var_def->num = 1;
|
||||
var_def->type = Type_AST2LLVM(node->var_type);
|
||||
var_def->name_full = "%.var.local." + std::to_string(node->current_scope->scope_id) + "." + var.first + ".addrkp";
|
||||
@ -411,7 +414,7 @@ void IRBuilder::ActuralVisit(NewArrayExpr_ASTNode *node) {
|
||||
|
||||
auto dim_info = std::make_shared<AllocaAction>();
|
||||
std::string dim_info_var = "%.var.tmp." + std::to_string(tmp_var_counter++);
|
||||
block->actions.push_back(dim_info);
|
||||
cur_alloca_block->actions.push_back(dim_info);
|
||||
dim_info->num = dims_with_size;
|
||||
dim_info->name_full = dim_info_var;
|
||||
dim_info->type = LLVMIRIntType(32);
|
||||
|
@ -7,4 +7,6 @@ clang-18 -S -emit-llvm --target=riscv32-unknown-elf -O2 -fno-builtin-printf -fno
|
||||
builtin.c -o builtin_intermediate.ll
|
||||
sed 's/_builtin_/.builtin./g;s/string_/string./g;s/array_/array./g' builtin_intermediate.ll > builtin.ll
|
||||
rm builtin_intermediate.ll
|
||||
llc-18 -march=riscv32 -mattr=+m builtin.ll -o builtin.s -O2
|
||||
llc-18 -march=riscv32 -mattr=+m builtin.ll -o builtin_intermediate.s -O2
|
||||
sed 's/Lfunc/builtin_Lfunc/g;s/L.str/builtin_L.str/g' builtin_intermediate.s > builtin.s
|
||||
rm builtin_intermediate.s
|
126
src/IR/builtin.s
126
src/IR/builtin.s
@ -58,8 +58,8 @@
|
||||
lw s4, 8(sp) # 4-byte Folded Reload
|
||||
addi sp, sp, 32
|
||||
ret
|
||||
.Lfunc_end0:
|
||||
.size .builtin.strcat, .Lfunc_end0-.builtin.strcat
|
||||
.builtin_Lfunc_end0:
|
||||
.size .builtin.strcat, .builtin_Lfunc_end0-.builtin.strcat
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -78,8 +78,8 @@ string.length: # @string.length
|
||||
# %bb.2:
|
||||
addi a0, a1, -1
|
||||
ret
|
||||
.Lfunc_end1:
|
||||
.size string.length, .Lfunc_end1-string.length
|
||||
.builtin_Lfunc_end1:
|
||||
.size string.length, .builtin_Lfunc_end1-string.length
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -118,8 +118,8 @@ string.substring: # @string.substring
|
||||
lw s2, 0(sp) # 4-byte Folded Reload
|
||||
addi sp, sp, 16
|
||||
ret
|
||||
.Lfunc_end2:
|
||||
.size string.substring, .Lfunc_end2-string.substring
|
||||
.builtin_Lfunc_end2:
|
||||
.size string.substring, .builtin_Lfunc_end2-string.substring
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -131,16 +131,16 @@ string.parseInt: # @string.parseInt
|
||||
# %bb.0:
|
||||
addi sp, sp, -16
|
||||
sw ra, 12(sp) # 4-byte Folded Spill
|
||||
lui a1, %hi(.L.str)
|
||||
addi a1, a1, %lo(.L.str)
|
||||
lui a1, %hi(.builtin_L.str)
|
||||
addi a1, a1, %lo(.builtin_L.str)
|
||||
addi a2, sp, 8
|
||||
call sscanf
|
||||
lw a0, 8(sp)
|
||||
lw ra, 12(sp) # 4-byte Folded Reload
|
||||
addi sp, sp, 16
|
||||
ret
|
||||
.Lfunc_end3:
|
||||
.size string.parseInt, .Lfunc_end3-string.parseInt
|
||||
.builtin_Lfunc_end3:
|
||||
.size string.parseInt, .builtin_Lfunc_end3-string.parseInt
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -153,8 +153,8 @@ string.ord: # @string.ord
|
||||
add a0, a0, a1
|
||||
lbu a0, 0(a0)
|
||||
ret
|
||||
.Lfunc_end4:
|
||||
.size string.ord, .Lfunc_end4-string.ord
|
||||
.builtin_Lfunc_end4:
|
||||
.size string.ord, .builtin_Lfunc_end4-string.ord
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -164,14 +164,14 @@ string.ord: # @string.ord
|
||||
.type print,@function
|
||||
print: # @print
|
||||
# %bb.0:
|
||||
lui a1, %hi(.L.str.1)
|
||||
addi a1, a1, %lo(.L.str.1)
|
||||
lui a1, %hi(.builtin_L.str.1)
|
||||
addi a1, a1, %lo(.builtin_L.str.1)
|
||||
mv a2, a0
|
||||
mv a0, a1
|
||||
mv a1, a2
|
||||
tail printf
|
||||
.Lfunc_end5:
|
||||
.size print, .Lfunc_end5-print
|
||||
.builtin_Lfunc_end5:
|
||||
.size print, .builtin_Lfunc_end5-print
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -181,14 +181,14 @@ print: # @print
|
||||
.type println,@function
|
||||
println: # @println
|
||||
# %bb.0:
|
||||
lui a1, %hi(.L.str.2)
|
||||
addi a1, a1, %lo(.L.str.2)
|
||||
lui a1, %hi(.builtin_L.str.2)
|
||||
addi a1, a1, %lo(.builtin_L.str.2)
|
||||
mv a2, a0
|
||||
mv a0, a1
|
||||
mv a1, a2
|
||||
tail printf
|
||||
.Lfunc_end6:
|
||||
.size println, .Lfunc_end6-println
|
||||
.builtin_Lfunc_end6:
|
||||
.size println, .builtin_Lfunc_end6-println
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -198,14 +198,14 @@ println: # @println
|
||||
.type printInt,@function
|
||||
printInt: # @printInt
|
||||
# %bb.0:
|
||||
lui a1, %hi(.L.str)
|
||||
addi a1, a1, %lo(.L.str)
|
||||
lui a1, %hi(.builtin_L.str)
|
||||
addi a1, a1, %lo(.builtin_L.str)
|
||||
mv a2, a0
|
||||
mv a0, a1
|
||||
mv a1, a2
|
||||
tail printf
|
||||
.Lfunc_end7:
|
||||
.size printInt, .Lfunc_end7-printInt
|
||||
.builtin_Lfunc_end7:
|
||||
.size printInt, .builtin_Lfunc_end7-printInt
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -215,14 +215,14 @@ printInt: # @printInt
|
||||
.type printlnInt,@function
|
||||
printlnInt: # @printlnInt
|
||||
# %bb.0:
|
||||
lui a1, %hi(.L.str.3)
|
||||
addi a1, a1, %lo(.L.str.3)
|
||||
lui a1, %hi(.builtin_L.str.3)
|
||||
addi a1, a1, %lo(.builtin_L.str.3)
|
||||
mv a2, a0
|
||||
mv a0, a1
|
||||
mv a1, a2
|
||||
tail printf
|
||||
.Lfunc_end8:
|
||||
.size printlnInt, .Lfunc_end8-printlnInt
|
||||
.builtin_Lfunc_end8:
|
||||
.size printlnInt, .builtin_Lfunc_end8-printlnInt
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -240,8 +240,8 @@ toString: # @toString
|
||||
li a0, 15
|
||||
call malloc
|
||||
mv s1, a0
|
||||
lui a0, %hi(.L.str)
|
||||
addi a1, a0, %lo(.L.str)
|
||||
lui a0, %hi(.builtin_L.str)
|
||||
addi a1, a0, %lo(.builtin_L.str)
|
||||
mv a0, s1
|
||||
mv a2, s0
|
||||
call sprintf
|
||||
@ -251,8 +251,8 @@ toString: # @toString
|
||||
lw s1, 4(sp) # 4-byte Folded Reload
|
||||
addi sp, sp, 16
|
||||
ret
|
||||
.Lfunc_end9:
|
||||
.size toString, .Lfunc_end9-toString
|
||||
.builtin_Lfunc_end9:
|
||||
.size toString, .builtin_Lfunc_end9-toString
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -277,8 +277,8 @@ getString: # @getString
|
||||
li a0, 11
|
||||
call malloc
|
||||
mv s3, a0
|
||||
lui a0, %hi(.L.str.4)
|
||||
addi s0, a0, %lo(.L.str.4)
|
||||
lui a0, %hi(.builtin_L.str.4)
|
||||
addi s0, a0, %lo(.builtin_L.str.4)
|
||||
addi a1, sp, 3
|
||||
mv a0, s0
|
||||
call scanf
|
||||
@ -375,8 +375,8 @@ getString: # @getString
|
||||
lw s9, 4(sp) # 4-byte Folded Reload
|
||||
addi sp, sp, 48
|
||||
ret
|
||||
.Lfunc_end10:
|
||||
.size getString, .Lfunc_end10-getString
|
||||
.builtin_Lfunc_end10:
|
||||
.size getString, .builtin_Lfunc_end10-getString
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -388,16 +388,16 @@ getInt: # @getInt
|
||||
# %bb.0:
|
||||
addi sp, sp, -16
|
||||
sw ra, 12(sp) # 4-byte Folded Spill
|
||||
lui a0, %hi(.L.str)
|
||||
addi a0, a0, %lo(.L.str)
|
||||
lui a0, %hi(.builtin_L.str)
|
||||
addi a0, a0, %lo(.builtin_L.str)
|
||||
addi a1, sp, 8
|
||||
call scanf
|
||||
lw a0, 8(sp)
|
||||
lw ra, 12(sp) # 4-byte Folded Reload
|
||||
addi sp, sp, 16
|
||||
ret
|
||||
.Lfunc_end11:
|
||||
.size getInt, .Lfunc_end11-getInt
|
||||
.builtin_Lfunc_end11:
|
||||
.size getInt, .builtin_Lfunc_end11-getInt
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -408,8 +408,8 @@ getInt: # @getInt
|
||||
.builtin.AllocateClassBody: # @.builtin.AllocateClassBody
|
||||
# %bb.0:
|
||||
tail malloc
|
||||
.Lfunc_end12:
|
||||
.size .builtin.AllocateClassBody, .Lfunc_end12-.builtin.AllocateClassBody
|
||||
.builtin_Lfunc_end12:
|
||||
.size .builtin.AllocateClassBody, .builtin_Lfunc_end12-.builtin.AllocateClassBody
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -430,8 +430,8 @@ getInt: # @getInt
|
||||
or a0, a0, a3
|
||||
or a0, a0, a1
|
||||
ret
|
||||
.Lfunc_end13:
|
||||
.size .builtin.GetArrayLength, .Lfunc_end13-.builtin.GetArrayLength
|
||||
.builtin_Lfunc_end13:
|
||||
.size .builtin.GetArrayLength, .builtin_Lfunc_end13-.builtin.GetArrayLength
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -510,8 +510,8 @@ getInt: # @getInt
|
||||
lw s7, 12(sp) # 4-byte Folded Reload
|
||||
addi sp, sp, 48
|
||||
ret
|
||||
.Lfunc_end14:
|
||||
.size .builtin.RecursiveAllocateArray, .Lfunc_end14-.builtin.RecursiveAllocateArray
|
||||
.builtin_Lfunc_end14:
|
||||
.size .builtin.RecursiveAllocateArray, .builtin_Lfunc_end14-.builtin.RecursiveAllocateArray
|
||||
# -- End function
|
||||
.option pop
|
||||
.option push
|
||||
@ -541,35 +541,35 @@ getInt: # @getInt
|
||||
lw s0, 8(sp) # 4-byte Folded Reload
|
||||
addi sp, sp, 16
|
||||
ret
|
||||
.Lfunc_end15:
|
||||
.size .builtin.AllocateArray, .Lfunc_end15-.builtin.AllocateArray
|
||||
.builtin_Lfunc_end15:
|
||||
.size .builtin.AllocateArray, .builtin_Lfunc_end15-.builtin.AllocateArray
|
||||
# -- End function
|
||||
.option pop
|
||||
.type .L.str,@object # @.str
|
||||
.type .builtin_L.str,@object # @.str
|
||||
.section .rodata.str1.1,"aMS",@progbits,1
|
||||
.L.str:
|
||||
.builtin_L.str:
|
||||
.asciz "%d"
|
||||
.size .L.str, 3
|
||||
.size .builtin_L.str, 3
|
||||
|
||||
.type .L.str.1,@object # @.str.1
|
||||
.L.str.1:
|
||||
.type .builtin_L.str.1,@object # @.str.1
|
||||
.builtin_L.str.1:
|
||||
.asciz "%s"
|
||||
.size .L.str.1, 3
|
||||
.size .builtin_L.str.1, 3
|
||||
|
||||
.type .L.str.2,@object # @.str.2
|
||||
.L.str.2:
|
||||
.type .builtin_L.str.2,@object # @.str.2
|
||||
.builtin_L.str.2:
|
||||
.asciz "%s\n"
|
||||
.size .L.str.2, 4
|
||||
.size .builtin_L.str.2, 4
|
||||
|
||||
.type .L.str.3,@object # @.str.3
|
||||
.L.str.3:
|
||||
.type .builtin_L.str.3,@object # @.str.3
|
||||
.builtin_L.str.3:
|
||||
.asciz "%d\n"
|
||||
.size .L.str.3, 4
|
||||
.size .builtin_L.str.3, 4
|
||||
|
||||
.type .L.str.4,@object # @.str.4
|
||||
.L.str.4:
|
||||
.type .builtin_L.str.4,@object # @.str.4
|
||||
.builtin_L.str.4:
|
||||
.asciz "%c"
|
||||
.size .L.str.4, 3
|
||||
.size .builtin_L.str.4, 3
|
||||
|
||||
.ident "Ubuntu clang version 18.1.8 (++20240731024944+3b5b5c1ec4a3-1~exp1~20240731145000.144)"
|
||||
.section ".note.GNU-stack","",@progbits
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "IR/IR.h"
|
||||
#include "naivebackend/naivebackend.h"
|
||||
#include "semantic/semantic.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
@ -28,6 +29,7 @@ int main(int argc, char **argv) {
|
||||
SemanticCheck(fin, ast);
|
||||
auto IR = BuildIR(ast);
|
||||
IR->RecursivePrint(fout);
|
||||
// GenerateNaiveASM(fout, IR);
|
||||
} catch (const SemanticError &err) {
|
||||
std::cout << err.what() << std::endl;
|
||||
return err.GetErrorCode();
|
||||
|
5
src/naivebackend/CMakeLists.txt
Normal file
5
src/naivebackend/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include/naivebackend)
|
||||
|
||||
file(GLOB NAIVE_BACKEND_SOURCES "*.cpp")
|
||||
add_library(naivebackend STATIC ${NAIVE_BACKEND_SOURCES})
|
||||
target_link_libraries(naivebackend PUBLIC ast)
|
4
src/naivebackend/naivebackend.cpp
Normal file
4
src/naivebackend/naivebackend.cpp
Normal file
@ -0,0 +1,4 @@
|
||||
#include "naivebackend.h"
|
||||
void GenerateNaiveASM(std::ostream &os, std::shared_ptr<ModuleItem> prog) {
|
||||
;
|
||||
}
|
Reference in New Issue
Block a user