mirror of
https://github.com/janet-lang/janet
synced 2025-06-26 15:12:49 +00:00
Add more drawing examples.
This commit is contained in:
parent
c13ef02ea2
commit
9fa9286fca
@ -51,14 +51,14 @@
|
|||||||
(while (< y h)
|
(while (< y h)
|
||||||
(var x:uint 0)
|
(var x:uint 0)
|
||||||
(while (< x w)
|
(while (< x w)
|
||||||
(write_32 (if (< y 32) blue red))
|
(write_32 (if (> y 64) blue red))
|
||||||
(set x (+ 1 x)))
|
(set x (+ 1 x)))
|
||||||
(set y (+ y 1)))
|
(set y (+ y 1)))
|
||||||
(return))
|
(return))
|
||||||
|
|
||||||
(defsys main:int []
|
(defsys main:int []
|
||||||
(def w:uint 128)
|
(def w:uint 512)
|
||||||
(def h:uint 128)
|
(def h:uint 512)
|
||||||
(write_header w h)
|
(write_header w h)
|
||||||
(draw w h)
|
(draw w h)
|
||||||
(return 0))
|
(return 0))
|
||||||
|
@ -2,6 +2,9 @@
|
|||||||
### Create a .bmp file on linux.
|
### 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)
|
(use ./frontend)
|
||||||
|
|
||||||
(defpointer p32 uint)
|
(defpointer p32 uint)
|
||||||
@ -15,7 +18,7 @@
|
|||||||
(defsys w32:void [c:cursor x:uint]
|
(defsys w32:void [c:cursor x:uint]
|
||||||
(def p:p32 (load c))
|
(def p:p32 (load c))
|
||||||
(store p x)
|
(store p x)
|
||||||
(store c (the p32 (pointer-add p 4)))
|
(store c (the p32 (pointer-add p 1)))
|
||||||
(return))
|
(return))
|
||||||
|
|
||||||
(defsys w16:void [c:cursor x:uint]
|
(defsys w16:void [c:cursor x:uint]
|
||||||
@ -28,14 +31,17 @@
|
|||||||
(defsys makebmp:p32 [w:uint h:uint]
|
(defsys makebmp:p32 [w:uint h:uint]
|
||||||
(def size:uint (+ 56 (* w h 4)))
|
(def size:uint (+ 56 (* w h 4)))
|
||||||
(def mem:p32 (malloc size))
|
(def mem:p32 (malloc size))
|
||||||
(def c:cursor (address mem))
|
(def cursor_data:p32 mem)
|
||||||
(w16 c 0x424D) # ascii "BM"
|
(def c:cursor (address cursor_data))
|
||||||
|
(w16 c 0x4D42) # ascii "BM"
|
||||||
(w32 c size)
|
(w32 c size)
|
||||||
|
(w32 c 0)
|
||||||
(w32 c 56)
|
(w32 c 56)
|
||||||
(w32 c 40)
|
(w32 c 40)
|
||||||
(w32 c w)
|
(w32 c w)
|
||||||
(w32 c h)
|
(w32 c h)
|
||||||
(w32 c 0x20001)
|
(w16 c 1)
|
||||||
|
(w16 c 32)
|
||||||
(w32 c 0)
|
(w32 c 0)
|
||||||
(w32 c 0)
|
(w32 c 0)
|
||||||
(w32 c 4096)
|
(w32 c 4096)
|
||||||
@ -46,12 +52,10 @@
|
|||||||
# Draw
|
# Draw
|
||||||
(def red:uint 0xFFFF0000)
|
(def red:uint 0xFFFF0000)
|
||||||
(def blue:uint 0xFF0000FF)
|
(def blue:uint 0xFF0000FF)
|
||||||
(def size:uint (* w h 4))
|
|
||||||
(var y:uint 0)
|
(var y:uint 0)
|
||||||
(while (< y h)
|
(while (< y h)
|
||||||
(var x:uint 0)
|
(var x:uint 0)
|
||||||
(while (< x w)
|
(while (< x w)
|
||||||
#(write_32 (if (< y 32) blue red))
|
|
||||||
(if (> y 64)
|
(if (> y 64)
|
||||||
(w32 c blue)
|
(w32 c blue)
|
||||||
(w32 c red))
|
(w32 c red))
|
||||||
@ -60,31 +64,10 @@
|
|||||||
(write 1 mem size)
|
(write 1 mem size)
|
||||||
(return mem))
|
(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 []
|
(defsys main:int []
|
||||||
(def w:uint 128)
|
(def w:uint 128)
|
||||||
(def h:uint 128)
|
(def h:uint 512)
|
||||||
#(makebmp w h)
|
(makebmp w h)
|
||||||
(makebmp2 w h)
|
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|
||||||
####
|
####
|
||||||
|
@ -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];
|
uint32_t operand_type = ir->types[instruction.three.dest];
|
||||||
tempbuf->count = 0;
|
tempbuf->count = 0;
|
||||||
uint32_t index_index = 0;
|
uint32_t index_index = 0;
|
||||||
@ -1408,7 +1408,7 @@ static void emit_binop(JanetSysIR *ir, JanetBuffer *buffer, JanetBuffer *tempbuf
|
|||||||
JanetSysIRLinkage *linkage = ir->linkage;
|
JanetSysIRLinkage *linkage = ir->linkage;
|
||||||
|
|
||||||
/* Top-level pointer semantics */
|
/* 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;
|
operand_type = linkage->type_defs[operand_type].pointer.type;
|
||||||
is_pointer = 1;
|
is_pointer = 1;
|
||||||
}
|
}
|
||||||
@ -1454,7 +1454,8 @@ void janet_sys_ir_lower_to_c(JanetSysIRLinkage *linkage, JanetBuffer *buffer) {
|
|||||||
|
|
||||||
JanetBuffer *tempbuf = janet_buffer(0);
|
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 */
|
/* Prelude */
|
||||||
janet_formatb(buffer, "#include <stddef.h>\n#include <stdlib.h>\n#include <stdint.h>\n#include <stdbool.h>\n#include <stdio.h>\n#include <sys/syscall.h>\n#define _t0 void\n\n");
|
janet_formatb(buffer, "#include <stddef.h>\n#include <stdlib.h>\n#include <stdint.h>\n#include <stdbool.h>\n#include <stdio.h>\n#include <sys/syscall.h>\n#define _t0 void\n\n");
|
||||||
@ -1567,13 +1568,17 @@ void janet_sys_ir_lower_to_c(JanetSysIRLinkage *linkage, JanetBuffer *buffer) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case JANET_SYSOP_ADD:
|
case JANET_SYSOP_ADD:
|
||||||
case JANET_SYSOP_POINTER_ADD:
|
|
||||||
EMITBINOP("+");
|
EMITBINOP("+");
|
||||||
break;
|
break;
|
||||||
|
case JANET_SYSOP_POINTER_ADD:
|
||||||
|
EMITBINOP_NOSUGAR("+");
|
||||||
|
break;
|
||||||
case JANET_SYSOP_SUBTRACT:
|
case JANET_SYSOP_SUBTRACT:
|
||||||
case JANET_SYSOP_POINTER_SUBTRACT:
|
|
||||||
EMITBINOP("-");
|
EMITBINOP("-");
|
||||||
break;
|
break;
|
||||||
|
case JANET_SYSOP_POINTER_SUBTRACT:
|
||||||
|
EMITBINOP_NOSUGAR("-");
|
||||||
|
break;
|
||||||
case JANET_SYSOP_MULTIPLY:
|
case JANET_SYSOP_MULTIPLY:
|
||||||
EMITBINOP("*");
|
EMITBINOP("*");
|
||||||
break;
|
break;
|
||||||
@ -1683,6 +1688,7 @@ void janet_sys_ir_lower_to_c(JanetSysIRLinkage *linkage, JanetBuffer *buffer) {
|
|||||||
|
|
||||||
janet_buffer_push_cstring(buffer, "}\n");
|
janet_buffer_push_cstring(buffer, "}\n");
|
||||||
#undef EMITBINOP
|
#undef EMITBINOP
|
||||||
|
#undef EMITBINOP_NOSUGAR
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user