1 #ifndef COMPILE_H
2 #define COMPILE_H
3 #include <stdint.h>
4 #include "jv.h"
5 #include "bytecode.h"
6 #include "locfile.h"
7 
8 struct inst;
9 typedef struct inst inst;
10 
11 
12 typedef struct block {
13   inst* first;
14   inst* last;
15 } block;
16 
17 block gen_location(location, struct locfile*, block);
18 
19 block gen_noop();
20 int block_is_noop(block b);
21 block gen_op_simple(opcode op);
22 block gen_const(jv constant);
23 block gen_const_global(jv constant, const char *name);
24 int block_is_const(block b);
25 int block_is_const_inf(block b);
26 jv_kind block_const_kind(block b);
27 jv block_const(block b);
28 block gen_op_target(opcode op, block target);
29 block gen_op_unbound(opcode op, const char* name);
30 block gen_op_bound(opcode op, block binder);
31 block gen_op_var_fresh(opcode op, const char* name);
32 block gen_op_pushk_under(jv constant);
33 
34 block gen_module(block metadata);
35 jv block_module_meta(block b);
36 block gen_import(const char* name, const char *as, int is_data);
37 block gen_import_meta(block import, block metadata);
38 block gen_function(const char* name, block formals, block body);
39 block gen_param_regular(const char* name);
40 block gen_param(const char* name);
41 block gen_lambda(block body);
42 block gen_call(const char* name, block body);
43 block gen_subexp(block a);
44 block gen_both(block a, block b);
45 block gen_const_object(block expr);
46 block gen_collect(block expr);
47 block gen_reduce(block source, block matcher, block init, block body);
48 block gen_foreach(block source, block matcher, block init, block update, block extract);
49 block gen_definedor(block a, block b);
50 block gen_condbranch(block iftrue, block iffalse);
51 block gen_and(block a, block b);
52 block gen_or(block a, block b);
53 block gen_dictpair(block k, block v);
54 
55 block gen_var_binding(block var, const char* name, block body);
56 block gen_array_matcher(block left, block curr);
57 block gen_object_matcher(block name, block curr);
58 block gen_destructure(block var, block matcher, block body);
59 block gen_destructure_alt(block matcher);
60 
61 block gen_cond(block cond, block iftrue, block iffalse);
62 block gen_try_handler(block handler);
63 block gen_try(block exp, block handler);
64 block gen_label(const char *label, block exp);
65 
66 block gen_cbinding(const struct cfunction* functions, int nfunctions, block b);
67 
68 void block_append(block* b, block b2);
69 block block_join(block a, block b);
70 int block_has_only_binders_and_imports(block, int bindflags);
71 int block_has_only_binders(block, int bindflags);
72 int block_has_main(block);
73 int block_is_funcdef(block b);
74 int block_is_single(block b);
75 block block_bind(block binder, block body, int bindflags);
76 block block_bind_library(block binder, block body, int bindflags, const char* libname);
77 block block_bind_referenced(block binder, block body, int bindflags);
78 block block_drop_unreferenced(block body);
79 
80 jv block_take_imports(block* body);
81 jv block_list_funcs(block body, int omit_underscores);
82 
83 int block_compile(block, struct bytecode**, struct locfile*, jv);
84 
85 void block_free(block);
86 
87 
88 
89 // Here's some horrible preprocessor gunk so that code
90 // sequences can be contructed as BLOCK(block1, block2, block3)
91 
92 #define BLOCK_1(b1) (b1)
93 #define BLOCK_2(b1,b2) (block_join((b1),(b2)))
94 #define BLOCK_3(b1,b2,b3) (block_join(BLOCK_2(b1,b2),(b3)))
95 #define BLOCK_4(b1,b2,b3,b4) (block_join(BLOCK_3(b1,b2,b3),(b4)))
96 #define BLOCK_5(b1,b2,b3,b4,b5) (block_join(BLOCK_4(b1,b2,b3,b4),(b5)))
97 #define BLOCK_6(b1,b2,b3,b4,b5,b6) (block_join(BLOCK_5(b1,b2,b3,b4,b5),(b6)))
98 #define BLOCK_7(b1,b2,b3,b4,b5,b6,b7) (block_join(BLOCK_6(b1,b2,b3,b4,b5,b6),(b7)))
99 #define BLOCK_8(b1,b2,b3,b4,b5,b6,b7,b8) (block_join(BLOCK_7(b1,b2,b3,b4,b5,b6,b7),(b8)))
100 
101 #define BLOCK_IDX(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
102 #define BLOCK(...) \
103   BLOCK_IDX(__VA_ARGS__, BLOCK_8, BLOCK_7, BLOCK_6, BLOCK_5, BLOCK_4, BLOCK_3, BLOCK_2, BLOCK_1)(__VA_ARGS__)
104 
105 
106 #endif
107