io.printf, please dont look at it
This commit is contained in:
@@ -165,7 +165,11 @@ impl Analyzer {
|
||||
{
|
||||
// its a function (defined/builtin/extern)
|
||||
if let Some(arity) = self.functions.get(&callee_name.lexeme) {
|
||||
if *arity >= 0 && *arity != args.len() as i32 {
|
||||
if *arity >= 0
|
||||
&& *arity != args.len() as i32
|
||||
&& callee_name.lexeme != "io.printf"
|
||||
// TODO: disgusting hack
|
||||
{
|
||||
return error!(
|
||||
&paren.loc,
|
||||
format!("expected {} arguments, got {}", arity, args.len())
|
||||
|
||||
@@ -100,7 +100,7 @@ impl<'a> CodegenX86_64<'a> {
|
||||
format!("section .data\n{}{}", self.data_section, self.output)
|
||||
}
|
||||
|
||||
pub fn emit_prologue(&mut self, emit_start: bool) -> Result<(), ZernError> {
|
||||
pub fn emit_prologue(&mut self, use_gcc: bool) -> Result<(), ZernError> {
|
||||
emit!(
|
||||
&mut self.output,
|
||||
"section .note.GNU-stack
|
||||
@@ -145,7 +145,7 @@ _builtin_syscall:
|
||||
"
|
||||
);
|
||||
|
||||
if emit_start {
|
||||
if !use_gcc {
|
||||
emit!(
|
||||
&mut self.output,
|
||||
"
|
||||
|
||||
22
src/main.rs
22
src/main.rs
@@ -13,6 +13,18 @@ use tokenizer::ZernError;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
macro_rules! compile_std {
|
||||
($codegen:expr, $( $name:literal ),* $(,)? ) => {
|
||||
$(
|
||||
compile_file_to(
|
||||
$codegen,
|
||||
$name,
|
||||
include_str!(concat!("std/", $name)).into()
|
||||
)?;
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
fn compile_file_to(
|
||||
codegen: &mut codegen_x86_64::CodegenX86_64,
|
||||
filename: &str,
|
||||
@@ -63,14 +75,8 @@ fn compile_file(args: Args) -> Result<(), ZernError> {
|
||||
|
||||
let mut analyzer = analyzer::Analyzer::new();
|
||||
let mut codegen = codegen_x86_64::CodegenX86_64::new(&mut analyzer);
|
||||
codegen.emit_prologue(!args.use_gcc)?;
|
||||
compile_file_to(
|
||||
&mut codegen,
|
||||
"syscalls.zr",
|
||||
include_str!("std/syscalls.zr").into(),
|
||||
)?;
|
||||
compile_file_to(&mut codegen, "std.zr", include_str!("std/std.zr").into())?;
|
||||
compile_file_to(&mut codegen, "net.zr", include_str!("std/net.zr").into())?;
|
||||
codegen.emit_prologue(args.use_gcc)?;
|
||||
compile_std!(&mut codegen, "syscalls.zr", "std.zr", "net.zr");
|
||||
compile_file_to(&mut codegen, filename, source)?;
|
||||
|
||||
if !args.output_asm {
|
||||
|
||||
@@ -186,6 +186,36 @@ func mem.write32[x: ptr, d: i64] : void
|
||||
func mem.write64[x: ptr, d: i64] : void
|
||||
_builtin_set64(x, d)
|
||||
|
||||
// TODO: we don't have variadics so we manually skip arity checking in the analyzer
|
||||
func io.printf[s: str, a1: any, a2: any, a3: any, a4: any, a5: any] : void
|
||||
// TODO: we really shouldn't heap allocate to print something
|
||||
let args: Array = [a1, a2, a3, a4, a5]
|
||||
let arg_idx: i64 = 0
|
||||
|
||||
while s[0]
|
||||
if s[0] == '%'
|
||||
s = s + 1
|
||||
|
||||
if s[0] == 'd'
|
||||
io.print_i64(array.nth(args, arg_idx))
|
||||
arg_idx = arg_idx + 1
|
||||
else if s[0] == 'x'
|
||||
io.print_i64_hex(array.nth(args, arg_idx))
|
||||
arg_idx = arg_idx + 1
|
||||
else if s[0] == 's'
|
||||
io.print(array.nth(args, arg_idx))
|
||||
arg_idx = arg_idx + 1
|
||||
else if (s[0] == 'c')
|
||||
io.print_char(array.nth(args, arg_idx))
|
||||
arg_idx = arg_idx + 1
|
||||
else if (s[0] == '%')
|
||||
io.print_char('%')
|
||||
else
|
||||
io.print_char(s[0])
|
||||
s = s + 1
|
||||
|
||||
array.free(args)
|
||||
|
||||
func io.print_sized[x: str, size: i64] : void
|
||||
_builtin_syscall(SYS_write, 1, x, size)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user