From 59dcf5b4be12919c088a3453234e389229de3bf7 Mon Sep 17 00:00:00 2001 From: jonathan Date: Thu, 30 Nov 2023 21:49:13 +0100 Subject: [PATCH] AOC 2022 day 1! --- compiler/src/passes/explicate/display.rs | 8 +++- compiler/src/passes/select/mod.rs | 6 +-- programs/aoc/y2022d01.test | 52 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 programs/aoc/y2022d01.test diff --git a/compiler/src/passes/explicate/display.rs b/compiler/src/passes/explicate/display.rs index 10ba7a9..f6c17ef 100644 --- a/compiler/src/passes/explicate/display.rs +++ b/compiler/src/passes/explicate/display.rs @@ -60,8 +60,12 @@ impl Display for CExpr<'_> { CExpr::AccessField { strct, field } => { write!(f, "{strct}.{field}") } - CExpr::Asm { .. } => { - todo!() + CExpr::Asm { instrs } => { + writeln!(f, "asm {{")?; + for instr in instrs { + writeln!(indented(f), "{instr}")?; + } + write!(f, "}}") } } } diff --git a/compiler/src/passes/select/mod.rs b/compiler/src/passes/select/mod.rs index d7a3a4f..e2e06c2 100644 --- a/compiler/src/passes/select/mod.rs +++ b/compiler/src/passes/select/mod.rs @@ -72,7 +72,7 @@ pub enum Instr { Popq { dst: Arg }, #[display(fmt = "retq")] Retq, - #[display(fmt = "syscall\t// arity: {arity}")] + #[display(fmt = "syscall\t{arity}")] Syscall { arity: usize }, #[display(fmt = "cmpq\t{src}\t{dst}")] Cmpq { src: Arg, dst: Arg }, @@ -92,9 +92,9 @@ pub enum Instr { Setcc { cnd: Cnd }, //TODO allow setting other byteregs #[display(fmt = "loadlbl\t{sym}\t{dst}")] LoadLbl { sym: IdentVars, dst: Arg }, - #[display(fmt = "call_direct\t{lbl}\t// arity: {arity}")] + #[display(fmt = "call_direct\t{lbl}\t{arity}")] CallqDirect { lbl: IdentVars, arity: usize }, - #[display(fmt = "call_indirect\t{src}\t// arity: {arity}")] + #[display(fmt = "call_indirect\t{src}\t{arity}")] CallqIndirect { src: Arg, arity: usize }, } diff --git a/programs/aoc/y2022d01.test b/programs/aoc/y2022d01.test new file mode 100644 index 0000000..06914d1 --- /dev/null +++ b/programs/aoc/y2022d01.test @@ -0,0 +1,52 @@ +fn read_char() -> I64 { + let mut v = 0; + let mut res = 0; + asm { + subq $8 %RSP // allocate stack space for reading char + movq $0 %RAX // read + movq $0 %RDI // stdin + movq %RSP %RSI // put read char at top of stack + movq $1 %RDX // read 1 byte + syscall 4 // arity of 4 + movq %RAX {res} // result of system call + popq {v} // pop read char + }; + if res == 0 { + return res + }; + v +} + +fn main() { + let ASCII_NEWLINE = 10; + let ASCII_ZERO = 48; + let ASCII_NULL = 0; + + let mut next = read_char(); + + let mut best = 0; + let mut sum = 0; + let mut current = 0; + let mut last_was_newline = false; + + while next != ASCII_NULL { + if next == ASCII_NEWLINE { + if last_was_newline { + // Found empty line + if sum > best { + best = sum; + }; + sum = 0; + } else { + last_was_newline = true; + sum = sum + current; + current = 0; + } + } else { + last_was_newline = false; + current = current * 10 + next - ASCII_ZERO; + }; + next = read_char(); + }; + print(best); +}