-
Notifications
You must be signed in to change notification settings - Fork 3
/
cc.hl
157 lines (147 loc) · 5.7 KB
/
cc.hl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#(ctimex (define debug-display-include-paths #t))
#(ctimex (define compiler-optimise-cache nil))
#(usedll CCSmall1)
include "./clike/backend/ll-ast.hl"
litinclude ("./doc/emit") "./backends/c2/emit.hl"
define cmdline_imul = mkref(true)
define arch = mkref('small1')
define subarch = mkref('none')
function prepare(env, mdl) {
hlsenv = hls_devenv(^arch, ^subarch);
visit:ir2(irmodule: mdl) {
deep irtoplevel {
%function -> if (body) {
if (filter cc as match cc with 'hls'() -> true)
prep_hls(hlsenv, env, %generic-filepath("."), node, name)
else if (deref( %llvm-optimise )) {
<g2tp:g2> = ll_genssa_opt(env, node);
// Do it here for now, later the backend may want to see some
// pragmas too.
g3 = ll_clean_pragmas(fun(x) [], g2);
return g3} else node
} else node
| else -> node}}}
function ir2backend_small1(mdl, fnm)
{
try {
try {
small1_enable_mul := ^cmdline_imul;
ir2mdl = map m in mdl do %ir0->ir2(m);
p1 = ir2_bodypass(ir2mdl, il2ssa_fullssa);
px = prepare(ll_module_types(ir2mdl), p1);
p2 = small1_backend_passes(px,fnm);
return p1
} catch (t_MBaseException e) {
println(mbaseerror(e));
println(%->s(e));
exit(-1);
}
} catch (t_Exception e) {
println(%->s(e));
exit(-1)
}
}
function ir2backend_c2(mdl, fnm)
{
try {
try {
ir2mdl = map m in mdl do %ir0->ir2(m);
p1 = ir2_bodypass(ir2mdl, il2ssa_fullssa);
px = prepare(ll_module_types(ir2mdl), p1);
p2 = c2_driver_mdl(px,fnm);
return p1
} catch (t_MBaseException e) {
println(mbaseerror(e));
println(%->s(e));
exit(-1);
}
} catch (t_Exception e) {
println(%->s(e));
exit(-1)
}
}
function clike_compile_files(mdname, fls)
{
nollvm = true;
{match ^arch with
'small1' -> ir2backend_hook := ir2backend_small1
| 'c2' -> ir2backend_hook := ir2backend_c2
| else -> ccerror('UNKNOWN-ARCH'(^arch))};
mdl = llvm_init_module(mdname);
env = clike_global_env();
%read-int-eval('define'('clike-current-env',#`(quote ,env)));
%read-int-eval('define'('clike-current-llvm-env',#`(quote ,mdl)));
iter fls do {
println(%S<<("Compiling: ",fls));
match fls with
c(fn) -> clike_compile_file(mdl,env,fn)
| x(fn) -> clike_bypass_file(mdl,env,fn)
};
llvm_save(mdl, mdname);
println("Done.");
}
function main()
collector(fadd,fget) {
%read-int-eval(#'(n.module clikeccrepl dll));
mname = mkref("test");
%llvm-optimise := true;
do loop(a = %a->l( %*CMDLINE* )) {
match a with
["/out";mdlname;@rest] -> {mname := mdlname;loop(rest)}
| ["/arch";archname;@rest] -> {arch := %Sm<<(archname); loop(rest)}
| ["/subarch";subname;@rest] ->
{subarch := %Sm<<(subname);
if (subarch === 'ice40') { // some defaults
c2_ram_end := 1023;
c2_enable_muops := [];
};
loop(rest)}
| ["/imul";@rest] -> {cmdline_imul := true; loop(rest)}
| ["/noimul";@rest] -> {cmdline_imul := []; loop(rest)}
| ["/c2ram";n;@rest] -> {c2_ram_end := %S->N(n); loop(rest)}
| ["/c2muops";@rest] -> {c2_enable_muops := true; loop(rest)}
| ["/c2muops-";@rest] -> {c2_enable_muops := []; loop(rest)}
| ["/c2barrel";@rest] -> {c2_enable_barrel := true; loop(rest)}
| ["/c2barrel-";@rest] -> {c2_enable_barrel := []; loop(rest)}
| ["/c2cmpops";@rest] -> {c2_enable_cmp := true; loop(rest)}
| ["/c2cmpops-";@rest] -> {c2_enable_cmp := []; loop(rest)}
| ["/d";dllname;@rest] -> {%read-compile-eval('usedll'(%Sm<<(dllname)));
loop(rest);}
| ["/o+";@rest] -> { %llvm-optimise := true; loop(rest)}
| ["/o-";@rest] -> { %llvm-optimise := []; loop(rest)}
| ["/dbg";levl;@rest] -> {clike_debug_level := %S->N(levl); loop(rest)}
| ["/s";hlfile;@rest] -> {%read-compile-eval('hlevl-file'(hlfile)); loop(rest)}
| ["/a";alfile;@rest] -> {%read-compile-eval('include'(alfile)); loop(rest)}
| ["/i";@rest] -> {clike_no_llvm := true;loop(rest)}
| ["/x";fname;@rest] -> {fadd('x'(fname));loop(rest)}
| [srcfile;@rest] -> {fadd('c'(srcfile));loop(rest)}
| [] -> {
fls = fget();
if(not(fls)) {
iter(println,
["Usage: clikecc [option...] file.c...";
"Options:";
" /out <modulename> : save bitcode to a given module";
" /arch small1|c2 : select target CPU";
" /subarch ice40|spartan6|artix7";
" : select target hardware configuration";
" /imul : enable IMUL and the other extended instructions (Small1)";
" /noimul : disable IMUL and the other extended instructions (Small1)";
" /d <dllname> : load a given MBase dll";
" /s <srcfile> : load a given PFront source file";
" /a <srcfile> : load a given MBase source file";
" /x <file> : compile in an intermediate LLVM code";
" /i : save an output in an intermediate format";
" /o+ : enable optimisation";
" /o- : disable optimisation";
" /c2ram <number> : size of C2 SoC RAM";
" /c2muops : enable C2 muops";
" /c2barrel : enable C2 barrel shifter";
" /c2cmpops : enable C2 cmpops";
" /dbg <level> : enable debugging output (1-8)"]);
quit();
}
else clike_compile_files(^mname,fls);
}
}
}