compute for's end only once

This commit is contained in:
2026-06-06 19:36:28 +02:00
parent 22874181cd
commit c41c22e936
4 changed files with 10 additions and 8 deletions

View File

@@ -436,12 +436,14 @@ _builtin_environ:
self.compile_expr(env, start)?;
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
self.compile_expr(env, end)?;
let end_offset = env.next_offset;
env.next_offset += 8;
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", end_offset);
emit!(&mut self.output, "{}:", env.loop_begin_label);
emit!(&mut self.output, " mov rax, QWORD [rbp-{}]", offset);
emit!(&mut self.output, " push rax");
self.compile_expr(env, end)?;
emit!(&mut self.output, " pop rcx");
emit!(&mut self.output, " cmp rcx, rax");
emit!(&mut self.output, " mov rcx, QWORD [rbp-{}]", end_offset);
emit!(&mut self.output, " cmp rax, rcx");
emit!(&mut self.output, " jge {}", env.loop_end_label);
self.compile_stmt(env, body)?;
emit!(&mut self.output, "{}:", env.loop_continue_label);

View File

@@ -279,12 +279,13 @@ func io.snprintf[..] : i64
i := 3
n := 0
tmp := _stackalloc(21)
while s[0]
if s[0] == '%'
s += 1
if s[0] == 'd'
tmp := _stackalloc(21)
str.from_i64_buf(tmp, _var_arg(i) as i64)
tmp_len := str.len(tmp as str)
remaining := size - n - 1
@@ -292,7 +293,6 @@ func io.snprintf[..] : i64
n += tmp_len
i += 1
else if s[0] == 'x'
tmp := _stackalloc(17)
str.hex_from_i64_buf(tmp, _var_arg(i) as i64)
tmp_len := str.len(tmp as str)
remaining := size - n - 1