From f7d49ec7b506417b10e77ba0d373d85d243c3ed5 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 1 Jan 2024 17:05:49 -0700 Subject: [PATCH] print atom is working along with hello world --- .cargo/config.toml | 3 +++ src/g.rs | 4 +--- stdlib/prelude.lm | 21 ++++++++++++++++++++- 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 000000000..17871dde2 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,3 @@ + +[env] +RUST_TEST_THREADS = "1" diff --git a/src/g.rs b/src/g.rs index a9403f358..291ff39d3 100644 --- a/src/g.rs +++ b/src/g.rs @@ -97,7 +97,7 @@ fn compile_expr(helpers_ctx: &S, program_ctx: &S, e: &S) -> S { let xpg = compile_expr(helpers_ctx, program_ctx, &x); assert_eq!( head(&f).to_string(), "variable" ); let f_name = label_case( &tail(&f).to_string() ); - let call = variable( &format!("\tjmp {}\n", f_name) ); + let call = variable( &format!("\tcall {}\n", f_name) ); let prog = app( head(&xpg), call @@ -152,8 +152,6 @@ pub fn compile(cfg: &str, main_ctx: &S) { for (k,v) in kv_iter(&main_ctx) { let k = k.to_string(); let v = compile_expr(&helpers_ctx, &main_ctx, &v); - println!("compile user {} = {}", k, v); - println!("compile user {} = {}", k, head(&v)); raw_program = app( raw_program, app( diff --git a/stdlib/prelude.lm b/stdlib/prelude.lm index 5859eaaa9..667338c56 100644 --- a/stdlib/prelude.lm +++ b/stdlib/prelude.lm @@ -1,5 +1,18 @@ .text := ( + # internal strlen (%rdi: *char) + _strlen: \n + \t xor %rcx, %rcx \n + _strlen_loop: \n + \t cmpb $0, 0(%rdi) \n + \t jz _strlen_exit \n + \t inc %rcx \n + \t inc %rdi \n + \t jmp _strlen_loop \n + _strlen_exit: \n + \t ret \n + # result is stored in %rcx + # entry points print_s: \n __print_this: \n @@ -8,7 +21,13 @@ __print_this_atom: \n \t cmp $0, %r8 \n \t je __print_this_cons \n - # TODO print .atom + \t mov %r8, %rdi \n + \t call _strlen \n + \t mov %r8, %rsi \n # address of string to output + \t mov %rcx, %rdx \n # length is %rcx + \t mov $1, %rax \n # system call 1 is write + \t mov $1, %rdi \n # file handle 1 is stdout + \t syscall \n # invoke operating system to do the write \t ret \n __print_this_cons: \n