-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.jai
379 lines (314 loc) · 13.1 KB
/
build.jai
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#import "Basic";
#import "Compiler";
#import "System";
#import "String";
#import "Math";
#import "Hash_Table";
Reflector :: #import "reflector";
// #load "src/basic.jai";
// #load "src/reflect.jai"; // For StructNodeIdentFor
build :: ()
{
set_build_options_dc( .{do_output=false} ); // No executable for this workspace.
// The compiler will set the CWD to the directory containing the current file
//path := get_working_directory();
//print( "Working directory for 'build' metaprogram: '%'\n", path );
global_options := get_build_options();
global_options.output_path = "bin";
global_options.intermediate_path = "bin";
build_release := false;
args := global_options.compile_time_command_line;
// NOTE These arguments have to be specified last, after any arguments for the compiler itself, separated with a hyphen, e.g:
// jai build.jai - release
for arg: args
{
if arg ==
{
case "--release"; #through;
case "-release"; #through;
case "release";
build_release = true;
print( "Building release version.\n" );
}
}
// Test executable
{
w := compiler_create_workspace();
options := global_options;
options.output_type = .EXECUTABLE;
options.output_executable_name = "test";
if build_release
{
set_optimization( *options, .VERY_OPTIMIZED, true );
options.backend =.LLVM;
}
else
{
set_optimization( *options, .DEBUG, true );
options.backend =.X64;
}
set_build_options( options, w );
// I assume this must happen before we add files to the compilation?
compiler_begin_intercept( w );
Reflector.AddSourceStubs( w );
add_build_file( "src/test.jai", w );
MessageLoop( w );
compiler_end_intercept(w);
}
// Benchmark executable
{
w := compiler_create_workspace();
options := global_options;
options.output_type = .EXECUTABLE;
options.output_executable_name = "bench";
if build_release
{
set_optimization( *options, .VERY_OPTIMIZED, true );
options.backend =.LLVM;
}
else
{
set_optimization( *options, .DEBUG, true );
options.backend =.X64;
}
set_build_options( options, w );
// I assume this must happen before we add files to the compilation?
// compiler_begin_intercept( w );
Reflector.AddSourceStubs( w );
add_build_file( "src/bench.jai", w );
// MessageLoop( w );
// compiler_end_intercept(w);
}
}
#run build();
#if 1
{
#scope_file
// TODO TODO If we can live with just reporting the location of the enclosing struct declaration for errors (like we're currently doing)
// I dont think most of this crap is needed
// Test everything out with this removed..
EmitStructCodeNodes :: false;
PrintDebugLogs :: false;
#if 0
{
TryEmitNodeFor :: ( st: *Type_Info_Struct, w: Workspace ) -> bool
{
// Do we have it yet
node, nodeFound := table_find( *globalStructNodes, st );
if nodeFound
{
// TODO Seems like we dont even need the placeholder!
// Add a placeholder with a unique name for this Type_Info pointer
// The relevant Reflect() function will know to look for it
//str := tprint( "#placeholder _struct_node_%;\n", st );
//add_build_string( str, w ); //, code = REFLECT_MODULE_SCOPE );
str := tprint( "% :: cast(*Code_Struct) 0x%;\n", StructNodeIdentFor( st, false ), node );
add_build_string( str, w ); //, code = REFLECT_MODULE_SCOPE );
// Also add it to the list to emit them all at the end as an array
array_add( *globalNodesTable, node );
#if PrintDebugLogs
{
print( "##### Emitted node for %\n", st.name );
}
return true;
}
return false;
}
}
MessageLoop :: ( w: Workspace )
{
while true
{
message := compiler_wait_for_message();
if message.kind ==
{
case .FILE;
file := cast(*Message_File) message;
// Pretty janky
// Why oh why can't you refer to specific placeholders by name when calling add_build_string!?
if file.fully_pathed_filename == tprint( "%src/test.jai", #filepath )
globalMainTestFile = file;
case .TYPECHECKED;
typechecked := cast(*Message_Typechecked) message;
#if PrintDebugLogs
{
print( "# Got msg TYPECHECKED (% things: % headers, % bodies, % structs)\n", typechecked.all.count,
typechecked.procedure_headers.count, typechecked.procedure_bodies.count, typechecked.structs.count );
}
for tc: typechecked.procedure_headers
{
proc := tc.expression;
// Skip procs that are imported from somewhere
if proc.enclosing_load {
if proc.enclosing_load.enclosing_import.module_type != .MAIN_PROGRAM
continue;
}
// If it is a procedure we want to test, make note of it for later.
if HasNote( proc, "test" )
{
array_add( *globalTests, proc );
}
}
#if EmitStructCodeNodes
{
// Add the code node of each struct to a table
for tc: typechecked.structs
{
stNode := cast(*Code_Struct) tc.expression;
if stNode.defined_type.name
table_set( *globalStructNodes, stNode.defined_type, stNode );
}
for tc: typechecked.procedure_headers
{
header := tc.expression;
// If it is a procedure we want to run, make note of it for later.
args: string;
if HasNote( header, "runAfterTypechecking", *args )
{
array_add( *globalRunners, header );
array_add( *globalRunnerArgs, args );
}
// Examine all Reflect() calls and make a note of the type of the first argument
// If it's a pointer to some struct, emit its Code_Struct node pointer as a program constant
if header.name == "Reflect"
&& header.arguments.count == 2 && header.arguments[0].type_inst.pointer_to != null
{
inst := header.arguments[0].type_inst.pointer_to;
if inst.result.type == .STRUCT
{
st := cast(*Type_Info_Struct) inst.result;
emitted, newlyAdded := find_or_add( *globalReflectedTypes, st );
if newlyAdded && !<<emitted
{
#if PrintDebugLogs
{
print( "##### Found Reflect for type %\n", st.name );
}
// If we have not yet emitted a node for this type, try to do so right away (if we have it)
if TryEmitNodeFor( st, w )
{
<<emitted = true;
}
}
}
}
}
}
case .PHASE;
phase := cast(*Message_Phase) message;
if phase.phase ==
{
case .ALL_SOURCE_CODE_PARSED;
{
#if PrintDebugLogs
{
print( "# Got msg ALL_SOURCE_CODE_PARSED\n" );
}
}
case .TYPECHECKED_ALL_WE_CAN;
{
#if PrintDebugLogs
{
print( "# Got msg TYPECHECKED_ALL_WE_CAN (% waiting)\n", phase.num_items_waiting_to_typecheck );
}
// Emit all tests we've found until now
if !globalEmittedTestsTable
{
// Emit all struct nodes as a constant array
print_to_builder( *globalBuilder, "AllTests: [%] TestCase = .[\n", globalTests.count );
for globalTests
print_to_builder( *globalBuilder, " .{ name = \"%\", proc = % },\n", it.name, it.name );
append( *globalBuilder, "];\n" );
add_build_string( builder_to_string( *globalBuilder ), w, message = globalMainTestFile );
free_buffers( *globalBuilder );
array_reset( *globalTests );
globalEmittedTestsTable = true;
}
#if EmitStructCodeNodes
{
// Continue trying to emit any nodes we haven't yet emitted
for globalReflectedTypes
{
// Key, value
st, emitted := it_index, it;
if !emitted
{
if TryEmitNodeFor( st, w )
table_set( *globalReflectedTypes, st, true );
else if phase.num_items_waiting_to_typecheck == 0
// If we got to typecheck everything, and we still couldn't emit a node for some type, we failed
assert( false, "Didn't typecheck node for type '%' in time to emit it", st.name );
}
}
// NOTE When there's nothing else to typecheck, assume we have all the nodes (and no others will be added)
// and emit a table containing all of them, plus run any runners
if phase.num_items_waiting_to_typecheck == 0 && !globalEmittedNodesTable
{
// Emit all struct nodes as a constant array
print_to_builder( *globalBuilder, "globalReflectedStructNodes: [%] *Code_Struct: .[\n", globalNodesTable.count );
for globalNodesTable
{
print_to_builder( *globalBuilder, " cast(*Code_Struct) 0x%,\n", it );
}
append( *globalBuilder, "];\n" );
// Then emit a run directive for each of our runners
for globalRunners
{
// Pass any arguments to the note verbatim
print_to_builder( *globalBuilder, "#run %(%);", it.name, globalRunnerArgs[it_index] );
}
add_build_string( builder_to_string( *globalBuilder ), w ); //, code = REFLECT_MODULE_SCOPE );
free_buffers( *globalBuilder );
globalEmittedNodesTable = true;
}
}
}
case .PRE_WRITE_EXECUTABLE;
{
// TODO Any diagnostic / info msgs should go here so as to not interfere with the compiler's own msgs
#if PrintDebugLogs
{
print( "# Got msg PRE_WRITE_EXECUTABLE\n" );
}
}
}
case .COMPLETE;
break;
}
}
}
HasNote :: (header: *Code_Struct, note: string) -> bool {
for header.notes if it.text == note return true;
return false;
}
HasNote :: ( header: *Code_Procedure_Header, note: string, args: *string = null ) -> bool
{
for header.notes
{
if starts_with( it.text, note )
{
if args
{
// Anything in parenthesis is considered arguments to the note
s := slice( it.text, note.count, it.text.count - note.count );
if s && s[0] == #char "(" && s[s.count - 1] == #char ")"
{
<<args = slice( s, 1, s.count - 2 );
}
}
return true;
}
}
return false;
}
globalBuilder: String_Builder;
globalStructNodes: Table(*Type_Info_Struct, *Code_Struct);
globalReflectedTypes: Table(*Type_Info_Struct, bool);
globalNodesTable: [..] *Code_Struct;
globalEmittedNodesTable: bool = false;
globalRunners: [..] *Code_Procedure_Header;
globalRunnerArgs: [..] string;
}
globalMainTestFile: *Message;
globalTests: [..] *Code_Procedure_Header;
globalEmittedTestsTable: bool = false;