Skip to content

Commit

Permalink
refactor: rewrite loops using for statement
Browse files Browse the repository at this point in the history
  • Loading branch information
v420v committed Dec 15, 2024
1 parent 12c9834 commit b419e11
Show file tree
Hide file tree
Showing 8 changed files with 466 additions and 541 deletions.
891 changes: 436 additions & 455 deletions bootstrap/parser.s

Large diffs are not rendered by default.

21 changes: 6 additions & 15 deletions bootstrap/std.s
Original file line number Diff line number Diff line change
Expand Up @@ -3099,9 +3099,6 @@ strcmp:
pop %rdi
movl %eax, (%rdi)
.L.while.start.51:
movq $1, %rax
cmpq $1, %rax
jne .L.while.end.51
movq $1, %rax
push %rax
leaq -4(%rbp), %rax
Expand Down Expand Up @@ -3129,16 +3126,10 @@ strcmp:
movzbl (%rax), %eax
pop %rdi
cmpl %edi, %eax
setne %al
sete %al
movzbl %al, %eax
cmpq $1, %rax
jne .L.else.52
movq $0, %rax
leave
ret
jmp .L.end.52
.L.else.52:
.L.end.52:
jne .L.while.end.51
movq $0, %rax
push %rax
movq $1, %rax
Expand All @@ -3158,13 +3149,13 @@ strcmp:
sete %al
movzbl %al, %eax
cmpq $1, %rax
jne .L.else.53
jne .L.else.52
movq $1, %rax
leave
ret
jmp .L.end.53
.L.else.53:
.L.end.53:
jmp .L.end.52
.L.else.52:
.L.end.52:
movq $1, %rax
push %rax
leaq -4(%rbp), %rax
Expand Down
2 changes: 1 addition & 1 deletion lib/linux-syscall/linux-syscall.ibu
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
#define SYS_KCMP 312
#define SYS_FINIT_MODULE 313

func syscall(trap u64, a1 u64, a2 u64, a3 u64) i64; // src/builtin.s
func syscall(trap u64, a1 u64, a2 u64, a3 u64) i64; // lib/runtime.s

func open(file_name *u8, flags i32, mode i32) i64 {
return syscall(SYS_OPEN, file_name, flags, mode);
Expand Down
11 changes: 2 additions & 9 deletions lib/std/std.ibu
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
#include "std/header.ibu"

func strcmp(a *u8, b *u8) bool {
let i i32 = 0;
while {
if a[i] != b[i] {
return false;
}
for let i i32 = 0; a[i] == b[i]; i++ {
if a[i] == '\0' {
return true;
}
i++;
}
return false;
}
Expand Down Expand Up @@ -131,10 +126,8 @@ func strrchr(str *u8, ch i32) *u8 {
}

func memcpy(dest *u8, source *u8, size i32) *u8 {
let i i32 = 0;
while i < size {
for let i i32 = 0; i < size; i++ {
dest[i] = source[i];
i++;
}
return dest;
}
Expand Down
9 changes: 2 additions & 7 deletions src/codegen/codegen.ibu
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ func func_call_args_to_stack(g *Gen, args *Vec, ty *Type) i32 {

let stack i32 = 0;

let i i32 = args.len - 1;
while 0 <= i {
for let i i32 = args.len - 1; 0 <= i; i-- {
let arg *Node = vec_get(args, i);
gen_expr(g, arg);
gen_push(g, "%rax");
Expand All @@ -149,8 +148,6 @@ func func_call_args_to_stack(g *Gen, args *Vec, ty *Type) i32 {
gen_push_num(g, argc); // push argc
stack = stack + 16;
}

i--;
}

return stack;
Expand Down Expand Up @@ -454,11 +451,9 @@ func gen_expr(g *Gen, node *Node) i32 {
}

func gen_node_block(g *Gen, stmts *Vec) i32 {
let i i32 = 0;
while i < stmts.len {
for let i i32 = 0; i < stmts.len; i++ {
let stmt *Node = vec_get(stmts, i);
gen_stmt(g, stmt);
i++;
}
return 0;
}
Expand Down
57 changes: 15 additions & 42 deletions src/parser/parser.ibu
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ func unkown_type_error(types *Vec, tok *Token, msg *u8) u0 {
eprintf("help: %d possibilities:", types.len);
}

let i i32 = 0;
while i < types.len {
for let i i32 = 0; i < types.len; i++ {
let type *Type = vec_get(types, i);
if i+1 < types.len {
eprintf(" `%s`,", type.name);
} else {
eprintf(" `%s`", type.name);
}
i++;
}

eprintf("\n");
Expand All @@ -54,15 +52,13 @@ func unkown_member_error(members *Vec, tok *Token, msg *u8) u0 {
eprintf("help: %d possibilities:", members.len);
}

let i i32 = 0;
while i < members.len {
for let i i32 = 0; i < members.len; i++ {
let member *Member = vec_get(members, i);
if i+1 < members.len {
eprintf(" `%s`,", member.name);
} else {
eprintf(" `%s`", member.name);
}
i++;
}

eprintf("\n");
Expand All @@ -81,16 +77,12 @@ func leave_scope(p *Parser) u0 {
}

func get_user_defined_type(p *Parser, name *u8) *Type {
let i i32 = 0;

while i < p.user_defined_types.len {
for let i i32 = 0; i < p.user_defined_types.len; i++ {
let type *Type = vec_get(p.user_defined_types, i);
if strcmp(type.name, name) {
return type;
}
i++;
}

return nil;
}

Expand Down Expand Up @@ -205,12 +197,10 @@ func cmp_type_vec(vec1 *Vec, vec2 *Vec) bool {
return false;
}

let i i32 = 0;
while i < vec1.len {
for let i i32 = 0; i < vec1.len; i++ {
if !cmp_type(vec_get(vec1, i), vec_get(vec2, i)) {
return false;
}
i++;
}

return true;
Expand Down Expand Up @@ -246,54 +236,44 @@ func cmp_func_params(p1 *Vec, p2 *Vec) bool {
return false;
}

let i i32 = 0;
while i < p1.len {
for let i i32 = 0; i < p1.len; i++ {
if !cmp_func_param(vec_get(p1, i), vec_get(p2, i)) {
return false;
}
i++;
}

return true;
}

func find_locals(p *Parser, name *u8) *Object {
let scope *Scope = p.scope;
while scope != nil {
for let scope *Scope = p.scope; scope != nil; scope = scope.outer {
let var_scope *VarScope = scope.var_scope;
while var_scope != nil {
if strcmp(name, var_scope.name) {
return var_scope.obj;
}
var_scope = var_scope.outer;
}
scope = scope.outer;
}
return nil;
}

func find_locals_current_scope(p *Parser, name *u8) *Object {
let scope *Scope = p.scope;
if scope != nil {
let var_scope *VarScope = scope.var_scope;
while var_scope != nil {
if strcmp(name, var_scope.name) {
return var_scope.obj;
}
var_scope = var_scope.outer;
// REVIEW: if p.scope == nil { return nil; }

for let var_scope *VarScope = p.scope.var_scope; var_scope != nil; var_scope = var_scope.outer {
if strcmp(name, var_scope.name) {
return var_scope.obj;
}
}
return nil;
}

func find_globals(p *Parser, name *u8) *Object {
let object *Object = p.globals;

while object != nil {
for let object *Object = p.globals; object != nil; object = object.next {
if strcmp(object.name, name) {
return object;
}
object = object.next;
}

return nil;
Expand Down Expand Up @@ -343,11 +323,9 @@ func add_type_block(p *Parser, nodes *Vec) u0 {
if nodes == nil {
return;
}
let i i32 = 0;
while i < nodes.len {
for let i i32 = 0; i < nodes.len; i++ {
let node *Node = vec_get(nodes, i);
add_type(p, node);
i++;
}
}

Expand Down Expand Up @@ -596,13 +574,11 @@ func parse_number(p *Parser) *Node {
}

func search_struct_member(members *Vec, exp_member_name *u8) *Member {
let i i32 = 0;
while i < members.len {
for let i i32 = 0; i < members.len; i++ {
let member *Member = vec_get(members, i);
if strcmp(member.name, exp_member_name) {
return member;
}
i++;
}
return nil;
}
Expand Down Expand Up @@ -1298,16 +1274,14 @@ func new_member(name *u8, ty *Type) *Member {

func calc_sizof_struct_members(members *Vec) i32 {
let offset i32 = 0;
let i i32 = 0;

while i < members.len {
for let i i32 = 0; i < members.len; i++ {
let member *Member = vec_get(members, i);
if member.ty.size <= 8 && align_to(offset, 8) < align_to(offset + member.ty.size, 8) {
offset = align_to(offset, 8);
}
member.offset = offset;
offset = offset + member.ty.size;
i++;
}

return align_to(offset, 8);
Expand Down Expand Up @@ -1493,4 +1467,3 @@ func parse(p *Parser) u0 {
}
}
}

4 changes: 1 addition & 3 deletions src/preprocessor/preprocessor.ibu
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ func new_macro(name *u8, tok *Token) *Macro {
}

func find_macro(name *u8, p *Preprocessor) *Macro {
let m *Macro = p.macros;
while m != nil {
for let m *Macro = p.macros; m != nil; m = m.next {
if strcmp(m.name, name) {
return m;
}
m = m.next;
}
return nil;
}
Expand Down
12 changes: 3 additions & 9 deletions src/tokenizer/tokenizer.ibu
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,10 @@ func new_tokenizer(file_name *u8, program *u8) *Tokenizer {
}

func starts_with(a *u8, b *u8) bool {
let i i32 = 0;
while i < strlen(b) {
for let i i32 = 0; i < strlen(b); i++ {
if a[i] != b[i] {
return false;
}
i++;
}
return true;
}
Expand All @@ -102,10 +100,8 @@ func tokenizer_next(t *Tokenizer) u0 {
}

func tokenizer_next_n(t *Tokenizer, n i32) u0 {
let i i32 = 0;
while i < n {
for let i i32 = 0; i < n; i++ {
tokenizer_next(t);
i++;
}
}

Expand All @@ -126,16 +122,14 @@ func new_token(t *Tokenizer, kind TokenKind) *Token {
}

func tokenize_punct(t *Tokenizer) *Token {
let i i32 = 0;
while i < 34 {
for let i i32 = 0; i < 34; i++ {
if starts_with(t.program, t.puncts[i]) {
let tok *Token = new_token(t, TK_PUNCT);
tok.len = strlen(t.puncts[i]);
tok.lit = strndup(t.puncts[i], tok.len);
tokenizer_next_n(t, tok.len);
return tok;
}
i++;
}
return nil;
}
Expand Down

0 comments on commit b419e11

Please sign in to comment.