wat2c: 优化生成的代码体积, 减少未用局部变量的生成

This commit is contained in:
chai2010
2025-06-02 06:49:04 +08:00
parent d4c915a4a2
commit 5ba9083465
5 changed files with 50 additions and 63 deletions

View File

@@ -42,8 +42,12 @@ type wat2cWorker struct {
scopeLabels []string // 嵌套的label查询, if/block/loop
scopeStackBases []int // if/block/loop, 开始的栈位置
useMathX bool // 是否使用了 math_x 部分函数
trace bool // 调试开关
useMathX bool // 是否使用了 math_x 部分函数
use_R_u32 bool // R_u32
use_R_u16 bool // R_u16
use_R_u8 bool // R_u8
trace bool // 调试开关
}
type inlinedTypeIndex struct {
@@ -107,5 +111,8 @@ func (p *wat2cWorker) BuildCode() (code, header []byte, err error) {
headerCode = bytes.ReplaceAll(headerCode, []byte("\n\n\n"), []byte("\n\n"))
}
return c.Bytes(), headerCode, nil
// 删除空白行
bodyCode := bytes.ReplaceAll(c.Bytes(), []byte("\n\n\n"), []byte("\n\n"))
return bodyCode, headerCode, nil
}

View File

@@ -177,7 +177,7 @@ func (p *wat2cWorker) buildMemory_data(w io.Writer) error {
return nil
}
fmt.Fprintf(w, "void %s_memory_init_data() {\n", p.opt.Prefix)
fmt.Fprintf(w, "\nstatic void %s_memory_init_data() {\n", p.opt.Prefix)
defer fmt.Fprintf(w, "}\n\n")
for _, d := range p.m.Data {

View File

@@ -54,6 +54,10 @@ func (p *wat2cWorker) buildFunc_body(w io.Writer, fn *ast.Func, cRetType string)
}
}
p.use_R_u32 = false
p.use_R_u16 = false
p.use_R_u8 = false
assert(stk.Len() == 0)
for _, ins := range fn.Body.Insts {
if err := p.buildFunc_ins(&bufIns, fn, &stk, ins, 1); err != nil {
@@ -67,17 +71,25 @@ func (p *wat2cWorker) buildFunc_body(w io.Writer, fn *ast.Func, cRetType string)
}
// 固定类型的寄存器
fmt.Fprintf(w, " uint32_t R_u32;\n")
fmt.Fprintf(w, " uint16_t R_u16;\n")
fmt.Fprintf(w, " uint8_t R_u8;\n")
if p.use_R_u32 {
fmt.Fprintf(w, " uint32_t R_u32;\n")
}
if p.use_R_u16 {
fmt.Fprintf(w, " uint16_t R_u16;\n")
}
if p.use_R_u8 {
fmt.Fprintf(w, " uint8_t R_u8;\n")
}
// 栈寄存器(union类型)
fmt.Fprintf(w, " val_t R0")
for i := 1; i < stk.MaxDepth(); i++ {
fmt.Fprintf(w, ", R%d", i)
if stk.MaxDepth() > 0 {
fmt.Fprintf(w, " val_t R0")
for i := 1; i < stk.MaxDepth(); i++ {
fmt.Fprintf(w, ", R%d", i)
}
fmt.Fprintf(w, ";\n")
fmt.Fprintln(w)
}
fmt.Fprintf(w, ";\n")
fmt.Fprintln(w)
// 指令复制到 w
io.Copy(w, &bufIns)
@@ -707,6 +719,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I32Load8S)
sp0 := stk.Pop(token.I32)
ret0 := stk.Push(token.I32)
p.use_R_u8 = true
fmt.Fprintf(w, "%smemcpy(&R_u8, &%s_memory[R%d.i32+%d], 1); R%d.i32 = (int32_t)((int8_t)R_u8);\n",
indent, p.opt.Prefix, ret0, i.Offset, sp0,
)
@@ -714,6 +727,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I32Load8U)
sp0 := stk.Pop(token.I32)
ret0 := stk.Push(token.I32)
p.use_R_u8 = true
fmt.Fprintf(w, "%smemcpy(&R_u8, &%s_memory[R%d.i32+%d], 1); R%d.i32 = (int32_t)((uint8_t)R_u8);\n",
indent, p.opt.Prefix, ret0, i.Offset, sp0,
)
@@ -721,6 +735,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I32Load16S)
sp0 := stk.Pop(token.I32)
ret0 := stk.Push(token.I32)
p.use_R_u16 = true
fmt.Fprintf(w, "%smemcpy(&R_u16, &%s_memory[R%d.i32+%d], 2); R%d.i32 = (int32_t)((int16_t)R_u16);\n",
indent, p.opt.Prefix, ret0, i.Offset, sp0,
)
@@ -728,6 +743,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I32Load16U)
sp0 := stk.Pop(token.I32)
ret0 := stk.Push(token.I32)
p.use_R_u16 = true
fmt.Fprintf(w, "%smemcpy(&R_u16, &%s_memory[R%d.i32+%d], 2); R%d.i32 = (int32_t)((uint16_t)R_u16);\n",
indent, p.opt.Prefix, ret0, i.Offset, sp0,
)
@@ -735,6 +751,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I64Load8S)
sp0 := stk.Pop(token.I32)
ret0 := stk.Push(token.I64)
p.use_R_u8 = true
fmt.Fprintf(w, "%smemcpy(&R_u8, &%s_memory[R%d.i32+%d], 1); R%d.i64 = (int64_t)((int8_t)R_u8);\n",
indent, p.opt.Prefix, ret0, i.Offset, sp0,
)
@@ -742,6 +759,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I64Load8U)
sp0 := stk.Pop(token.I32)
ret0 := stk.Push(token.I64)
p.use_R_u8 = true
fmt.Fprintf(w, "%smemcpy(&R_u8, &%s_memory[R%d.i32+%d], 1); R%d.i64 = (int64_t)((uint8_t)R_u8);\n",
indent, p.opt.Prefix, ret0, i.Offset, sp0,
)
@@ -749,6 +767,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I64Load16S)
sp0 := stk.Pop(token.I32)
ret0 := stk.Push(token.I64)
p.use_R_u16 = true
fmt.Fprintf(w, "%smemcpy(&R_u16, &%s_memory[R%d.i32+%d], 2); R%d.i64 = (int64_t)((int16_t)R_u16);\n",
indent, p.opt.Prefix, ret0, i.Offset, sp0,
)
@@ -756,6 +775,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I64Load16U)
sp0 := stk.Pop(token.I32)
ret0 := stk.Push(token.I64)
p.use_R_u16 = true
fmt.Fprintf(w, "%smemcpy(&R_u16, &%s_memory[R%d.i32+%d], 2); R%d.i64 = (int64_t)((uint16_t)R_u16);\n",
indent, p.opt.Prefix, ret0, i.Offset, sp0,
)
@@ -763,6 +783,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I64Load32S)
sp0 := stk.Pop(token.I32)
ret0 := stk.Push(token.I64)
p.use_R_u32 = true
fmt.Fprintf(w, "%smemcpy(&R_u32, &%s_memory[R%d.i32+%d], 4); R%d.i64 = (int64_t)((int32_t)R_u32);\n",
indent, p.opt.Prefix, ret0, i.Offset, sp0,
)
@@ -770,6 +791,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I64Load32U)
sp0 := stk.Pop(token.I32)
ret0 := stk.Push(token.I64)
p.use_R_u32 = true
fmt.Fprintf(w, "%smemcpy(&R_u32, &%s_memory[R%d.i32+%d], 4); R%d.i64 = (int64_t)((uint32_t)R_u32);\n",
indent, p.opt.Prefix, ret0, i.Offset, sp0,
)
@@ -805,6 +827,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I32Store8)
sp0 := stk.Pop(token.I32)
sp1 := stk.Pop(token.I32)
p.use_R_u8 = true
fmt.Fprintf(w, "%sR_u8 = (uint8_t)((int8_t)(R%d.i32)); memcpy(&%s_memory[R%d.i32+%d], &R_u8, 1);\n",
indent, sp0, p.opt.Prefix, sp1, i.Offset,
)
@@ -812,6 +835,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I32Store16)
sp0 := stk.Pop(token.I32)
sp1 := stk.Pop(token.I32)
p.use_R_u16 = true
fmt.Fprintf(w, "%sR_u16 = (uint16_t)((int16_t)(R%d.i32)); memcpy(&%s_memory[R%d.i32+%d], &R_u16, 2);\n",
indent, sp0, p.opt.Prefix, sp1, i.Offset,
)
@@ -819,6 +843,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I64Store8)
sp0 := stk.Pop(token.I64)
sp1 := stk.Pop(token.I32)
p.use_R_u8 = true
fmt.Fprintf(w, "%sR_u8 = (uint8_t)((int8_t)(R%d.i64)); memcpy(&%s_memory[R%d.i32+%d], &R_u8, 1);\n",
indent, sp0, p.opt.Prefix, sp1, i.Offset,
)
@@ -826,6 +851,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I64Store16)
sp0 := stk.Pop(token.I64)
sp1 := stk.Pop(token.I32)
p.use_R_u16 = true
fmt.Fprintf(w, "%sR_u16 = (uint16_t)((int16_t)(R%d.i64)); memcpy(&%s_memory[R%d.i32+%d], &R_u16, 2);\n",
indent, sp0, p.opt.Prefix, sp1, i.Offset,
)
@@ -833,6 +859,7 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
i := i.(ast.Ins_I64Store32)
sp0 := stk.Pop(token.I64)
sp1 := stk.Pop(token.I32)
p.use_R_u32 = true
fmt.Fprintf(w, "%sR_u32 = (uint32_t)((int32_t)(R%d.i64)); memcpy(&%s_memory[R%d.i32+%d], &R_u32, 4);\n",
indent, sp0, p.opt.Prefix, sp1, i.Offset,
)

View File

@@ -85,9 +85,6 @@ extern void app_loop();
// func lcdPulseEnable
static void app_lcdPulseEnable() {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0, R1;
R0.i32 = app_E;
@@ -110,9 +107,6 @@ static void app_lcdPulseEnable() {
// func getBitAt (param $v i32) (param $i i32) (result i32)
static int32_t app_getBitAt(int32_t v, int32_t i) {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0, R1;
R0.i32 = v;
@@ -125,9 +119,6 @@ static int32_t app_getBitAt(int32_t v, int32_t i) {
// func get4BitAt (param $v i32) (param $i i32) (result i32)
static int32_t app_get4BitAt(int32_t v, int32_t i) {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0, R1;
R0.i32 = v;
@@ -140,9 +131,6 @@ static int32_t app_get4BitAt(int32_t v, int32_t i) {
// func lcdWrite4bits (param $byteValue i32)
static void app_lcdWrite4bits(int32_t byteValue) {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0, R1, R2;
R0.i32 = app_D4;
@@ -171,9 +159,6 @@ static void app_lcdWrite4bits(int32_t byteValue) {
// func lcdSend (param $value i32) (param $mode i32)
static void app_lcdSend(int32_t value, int32_t mode) {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0, R1;
R0.i32 = app_RS;
@@ -192,9 +177,6 @@ static void app_lcdSend(int32_t value, int32_t mode) {
// func lcdCommand (param $value i32)
static void app_lcdCommand(int32_t value) {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0, R1;
R0.i32 = value;
@@ -205,9 +187,6 @@ static void app_lcdCommand(int32_t value) {
// func LCDSetCursor (param $row i32) (param $col i32)
static void app_LCDSetCursor(int32_t row, int32_t col) {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0, R1;
R0.i32 = row;
@@ -225,9 +204,6 @@ static void app_LCDSetCursor(int32_t row, int32_t col) {
// func LCDWriteChar (param $value i32)
static void app_LCDWriteChar(int32_t value) {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0, R1;
R0.i32 = value;
@@ -238,9 +214,6 @@ static void app_LCDWriteChar(int32_t value) {
// func LCDClear
static void app_LCDClear() {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0;
R0.i32 = 1;
@@ -252,9 +225,6 @@ static void app_LCDClear() {
// func LCDInit
static void app_LCDInit() {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0, R1;
R0.i32 = app_RS;
@@ -306,9 +276,6 @@ static void app_LCDInit() {
// func say_hello (param $row i32) (param $col i32)
static void app_say_hello(int32_t row, int32_t col) {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0, R1;
R0.i32 = row;
@@ -347,20 +314,12 @@ static void app_say_hello(int32_t row, int32_t col) {
// func _start
static void app__start() {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0;
app_LCDInit();
return;
}
// func loop
void app_loop() {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0, R1;
app_LCDClear();
@@ -401,9 +360,9 @@ void app_loop() {
app_delay(R0.i32);
return;
}
void app_memory_init_data() {
}
static void app_memory_init_data() {
}
void app_init() {
static int init_flag = 0;

View File

@@ -38,9 +38,6 @@ extern void app_loop();
// func _start
static void app__start() {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0, R1;
int32_t tmp = 0;
@@ -57,9 +54,6 @@ static void app__start() {
// func loop
void app_loop() {
uint32_t R_u32;
uint16_t R_u16;
uint8_t R_u8;
val_t R0, R1;
R0.i32 = 1024;
@@ -76,9 +70,9 @@ void app_loop() {
app_delay(R0.i32);
return;
}
void app_memory_init_data() {
}
static void app_memory_init_data() {
}
void app_init() {
static int init_flag = 0;