From 9fa9286fcaf16dc97021f4324bec3658d7a8e6bb Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Mon, 25 Nov 2024 09:39:55 -0600 Subject: [PATCH] Add more drawing examples. --- examples/sysir/drawing.janet | 6 ++--- examples/sysir/drawing2.janet | 41 ++++++++++------------------------- src/core/sysir.c | 16 +++++++++----- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/examples/sysir/drawing.janet b/examples/sysir/drawing.janet index 94d996b1..db6dd197 100644 --- a/examples/sysir/drawing.janet +++ b/examples/sysir/drawing.janet @@ -51,14 +51,14 @@ (while (< y h) (var x:uint 0) (while (< x w) - (write_32 (if (< y 32) blue red)) + (write_32 (if (> y 64) blue red)) (set x (+ 1 x))) (set y (+ y 1))) (return)) (defsys main:int [] - (def w:uint 128) - (def h:uint 128) + (def w:uint 512) + (def h:uint 512) (write_header w h) (draw w h) (return 0)) diff --git a/examples/sysir/drawing2.janet b/examples/sysir/drawing2.janet index 973ec1c1..9a7fd03e 100644 --- a/examples/sysir/drawing2.janet +++ b/examples/sysir/drawing2.janet @@ -2,6 +2,9 @@ ### Create a .bmp file on linux. ### +# Quick run and view on Linux: +# build/janet examples/sysir/drawing2.janet > temp.c && cc temp.c && ./a.out > temp.bmp && feh temp.bmp + (use ./frontend) (defpointer p32 uint) @@ -15,7 +18,7 @@ (defsys w32:void [c:cursor x:uint] (def p:p32 (load c)) (store p x) - (store c (the p32 (pointer-add p 4))) + (store c (the p32 (pointer-add p 1))) (return)) (defsys w16:void [c:cursor x:uint] @@ -28,14 +31,17 @@ (defsys makebmp:p32 [w:uint h:uint] (def size:uint (+ 56 (* w h 4))) (def mem:p32 (malloc size)) - (def c:cursor (address mem)) - (w16 c 0x424D) # ascii "BM" + (def cursor_data:p32 mem) + (def c:cursor (address cursor_data)) + (w16 c 0x4D42) # ascii "BM" (w32 c size) + (w32 c 0) (w32 c 56) (w32 c 40) (w32 c w) (w32 c h) - (w32 c 0x20001) + (w16 c 1) + (w16 c 32) (w32 c 0) (w32 c 0) (w32 c 4096) @@ -46,12 +52,10 @@ # Draw (def red:uint 0xFFFF0000) (def blue:uint 0xFF0000FF) - (def size:uint (* w h 4)) (var y:uint 0) (while (< y h) (var x:uint 0) (while (< x w) - #(write_32 (if (< y 32) blue red)) (if (> y 64) (w32 c blue) (w32 c red)) @@ -60,31 +64,10 @@ (write 1 mem size) (return mem)) -(defsys makebmp2:p32 [w:uint h:uint] - (def size:uint 56) - (def mem:p32 (malloc size)) - (def c:cursor (address mem)) - (w32 c 0x12345678) - (w32 c size) - (w32 c 56) - (w32 c 40) - (w32 c w) - (w32 c h) - (w32 c 0x20001) - (w32 c 0) - (w32 c 0) - (w32 c 4096) - (w32 c 4096) - (w32 c 0) - (w32 c 0) - (write 1 mem size) - (return mem)) - (defsys main:int [] (def w:uint 128) - (def h:uint 128) - #(makebmp w h) - (makebmp2 w h) + (def h:uint 512) + (makebmp w h) (return 0)) #### diff --git a/src/core/sysir.c b/src/core/sysir.c index 793b1a58..54c1e0ff 100644 --- a/src/core/sysir.c +++ b/src/core/sysir.c @@ -1400,7 +1400,7 @@ static void op_or_const(JanetSysIR *ir, JanetBuffer *buf, uint32_t reg) { } } -static void emit_binop(JanetSysIR *ir, JanetBuffer *buffer, JanetBuffer *tempbuf, JanetSysInstruction instruction, const char *op) { +static void emit_binop(JanetSysIR *ir, JanetBuffer *buffer, JanetBuffer *tempbuf, JanetSysInstruction instruction, const char *op, int pointer_sugar) { uint32_t operand_type = ir->types[instruction.three.dest]; tempbuf->count = 0; uint32_t index_index = 0; @@ -1408,7 +1408,7 @@ static void emit_binop(JanetSysIR *ir, JanetBuffer *buffer, JanetBuffer *tempbuf JanetSysIRLinkage *linkage = ir->linkage; /* Top-level pointer semantics */ - if (janet_sys_optype(ir, instruction.three.dest) == JANET_PRIM_POINTER) { + if (pointer_sugar && janet_sys_optype(ir, instruction.three.dest) == JANET_PRIM_POINTER) { operand_type = linkage->type_defs[operand_type].pointer.type; is_pointer = 1; } @@ -1454,7 +1454,8 @@ void janet_sys_ir_lower_to_c(JanetSysIRLinkage *linkage, JanetBuffer *buffer) { JanetBuffer *tempbuf = janet_buffer(0); -#define EMITBINOP(OP) emit_binop(ir, buffer, tempbuf, instruction, OP) +#define EMITBINOP(OP) emit_binop(ir, buffer, tempbuf, instruction, OP, 1) +#define EMITBINOP_NOSUGAR(OP) emit_binop(ir, buffer, tempbuf, instruction, OP, 0) /* Prelude */ janet_formatb(buffer, "#include \n#include \n#include \n#include \n#include \n#include \n#define _t0 void\n\n"); @@ -1567,13 +1568,17 @@ void janet_sys_ir_lower_to_c(JanetSysIRLinkage *linkage, JanetBuffer *buffer) { } break; case JANET_SYSOP_ADD: - case JANET_SYSOP_POINTER_ADD: EMITBINOP("+"); break; + case JANET_SYSOP_POINTER_ADD: + EMITBINOP_NOSUGAR("+"); + break; case JANET_SYSOP_SUBTRACT: - case JANET_SYSOP_POINTER_SUBTRACT: EMITBINOP("-"); break; + case JANET_SYSOP_POINTER_SUBTRACT: + EMITBINOP_NOSUGAR("-"); + break; case JANET_SYSOP_MULTIPLY: EMITBINOP("*"); break; @@ -1683,6 +1688,7 @@ void janet_sys_ir_lower_to_c(JanetSysIRLinkage *linkage, JanetBuffer *buffer) { janet_buffer_push_cstring(buffer, "}\n"); #undef EMITBINOP +#undef EMITBINOP_NOSUGAR } }