]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | * Copyright 2011 Steven Watanabe | |
3 | * Copyright 2016 Rene Rivera | |
4 | * Distributed under the Boost Software License, Version 1.0. | |
5 | * (See accompanying file LICENSE_1_0.txt or copy at | |
6 | * http://www.boost.org/LICENSE_1_0.txt) | |
7 | */ | |
8 | ||
9 | #include "jam.h" | |
10 | #include "function.h" | |
11 | ||
12 | #include "class.h" | |
13 | #include "compile.h" | |
14 | #include "constants.h" | |
b32b8144 | 15 | #include "debugger.h" |
7c673cae FG |
16 | #include "filesys.h" |
17 | #include "frames.h" | |
18 | #include "lists.h" | |
19 | #include "mem.h" | |
20 | #include "pathsys.h" | |
21 | #include "rules.h" | |
22 | #include "search.h" | |
23 | #include "variable.h" | |
24 | #include "output.h" | |
25 | ||
26 | #include <assert.h> | |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | ||
b32b8144 | 31 | /* |
7c673cae | 32 | #define FUNCTION_DEBUG_PROFILE |
b32b8144 | 33 | */ |
7c673cae FG |
34 | |
35 | #ifndef FUNCTION_DEBUG_PROFILE | |
b32b8144 FG |
36 | #undef PROFILE_ENTER_LOCAL |
37 | #define PROFILE_ENTER_LOCAL(x) static int unused_LOCAL_##x = 0 | |
38 | #undef PROFILE_EXIT_LOCAL | |
7c673cae FG |
39 | #define PROFILE_EXIT_LOCAL(x) |
40 | #endif | |
41 | ||
42 | int glob( char const * s, char const * c ); | |
43 | void backtrace( FRAME * ); | |
44 | void backtrace_line( FRAME * ); | |
45 | ||
46 | #define INSTR_PUSH_EMPTY 0 | |
47 | #define INSTR_PUSH_CONSTANT 1 | |
48 | #define INSTR_PUSH_ARG 2 | |
49 | #define INSTR_PUSH_VAR 3 | |
50 | #define INSTR_PUSH_VAR_FIXED 57 | |
51 | #define INSTR_PUSH_GROUP 4 | |
52 | #define INSTR_PUSH_RESULT 5 | |
53 | #define INSTR_PUSH_APPEND 6 | |
54 | #define INSTR_SWAP 7 | |
55 | ||
56 | #define INSTR_JUMP_EMPTY 8 | |
57 | #define INSTR_JUMP_NOT_EMPTY 9 | |
58 | ||
59 | #define INSTR_JUMP 10 | |
60 | #define INSTR_JUMP_LT 11 | |
61 | #define INSTR_JUMP_LE 12 | |
62 | #define INSTR_JUMP_GT 13 | |
63 | #define INSTR_JUMP_GE 14 | |
64 | #define INSTR_JUMP_EQ 15 | |
65 | #define INSTR_JUMP_NE 16 | |
66 | #define INSTR_JUMP_IN 17 | |
67 | #define INSTR_JUMP_NOT_IN 18 | |
68 | ||
69 | #define INSTR_JUMP_NOT_GLOB 19 | |
70 | ||
71 | #define INSTR_FOR_INIT 56 | |
72 | #define INSTR_FOR_LOOP 20 | |
73 | ||
74 | #define INSTR_SET_RESULT 21 | |
75 | #define INSTR_RETURN 22 | |
76 | #define INSTR_POP 23 | |
77 | ||
78 | #define INSTR_PUSH_LOCAL 24 | |
79 | #define INSTR_POP_LOCAL 25 | |
80 | #define INSTR_SET 26 | |
81 | #define INSTR_APPEND 27 | |
82 | #define INSTR_DEFAULT 28 | |
83 | ||
84 | #define INSTR_PUSH_LOCAL_FIXED 58 | |
85 | #define INSTR_POP_LOCAL_FIXED 59 | |
86 | #define INSTR_SET_FIXED 60 | |
87 | #define INSTR_APPEND_FIXED 61 | |
88 | #define INSTR_DEFAULT_FIXED 62 | |
89 | ||
90 | #define INSTR_PUSH_LOCAL_GROUP 29 | |
91 | #define INSTR_POP_LOCAL_GROUP 30 | |
92 | #define INSTR_SET_GROUP 31 | |
93 | #define INSTR_APPEND_GROUP 32 | |
94 | #define INSTR_DEFAULT_GROUP 33 | |
95 | ||
96 | #define INSTR_PUSH_ON 34 | |
97 | #define INSTR_POP_ON 35 | |
98 | #define INSTR_SET_ON 36 | |
99 | #define INSTR_APPEND_ON 37 | |
100 | #define INSTR_DEFAULT_ON 38 | |
101 | #define INSTR_GET_ON 65 | |
102 | ||
103 | #define INSTR_CALL_RULE 39 | |
104 | #define INSTR_CALL_MEMBER_RULE 66 | |
105 | ||
106 | #define INSTR_APPLY_MODIFIERS 40 | |
107 | #define INSTR_APPLY_INDEX 41 | |
108 | #define INSTR_APPLY_INDEX_MODIFIERS 42 | |
109 | #define INSTR_APPLY_MODIFIERS_GROUP 43 | |
110 | #define INSTR_APPLY_INDEX_GROUP 44 | |
111 | #define INSTR_APPLY_INDEX_MODIFIERS_GROUP 45 | |
112 | #define INSTR_COMBINE_STRINGS 46 | |
113 | #define INSTR_GET_GRIST 64 | |
114 | ||
115 | #define INSTR_INCLUDE 47 | |
116 | #define INSTR_RULE 48 | |
117 | #define INSTR_ACTIONS 49 | |
118 | #define INSTR_PUSH_MODULE 50 | |
119 | #define INSTR_POP_MODULE 51 | |
120 | #define INSTR_CLASS 52 | |
121 | #define INSTR_BIND_MODULE_VARIABLES 63 | |
122 | ||
123 | #define INSTR_APPEND_STRINGS 53 | |
124 | #define INSTR_WRITE_FILE 54 | |
125 | #define INSTR_OUTPUT_STRINGS 55 | |
126 | ||
b32b8144 | 127 | #define INSTR_DEBUG_LINE 67 |
7c673cae FG |
128 | #define INSTR_FOR_POP 70 |
129 | ||
130 | typedef struct instruction | |
131 | { | |
132 | unsigned int op_code; | |
133 | int arg; | |
134 | } instruction; | |
135 | ||
136 | typedef struct _subfunction | |
137 | { | |
138 | OBJECT * name; | |
139 | FUNCTION * code; | |
140 | int local; | |
141 | } SUBFUNCTION; | |
142 | ||
143 | typedef struct _subaction | |
144 | { | |
145 | OBJECT * name; | |
146 | FUNCTION * command; | |
147 | int flags; | |
148 | } SUBACTION; | |
149 | ||
150 | #define FUNCTION_BUILTIN 0 | |
151 | #define FUNCTION_JAM 1 | |
152 | ||
153 | struct argument | |
154 | { | |
155 | int flags; | |
156 | #define ARG_ONE 0 | |
157 | #define ARG_OPTIONAL 1 | |
158 | #define ARG_PLUS 2 | |
159 | #define ARG_STAR 3 | |
160 | #define ARG_VARIADIC 4 | |
161 | OBJECT * type_name; | |
162 | OBJECT * arg_name; | |
163 | int index; | |
164 | }; | |
165 | ||
166 | struct arg_list | |
167 | { | |
168 | int size; | |
169 | struct argument * args; | |
170 | }; | |
171 | ||
172 | struct _function | |
173 | { | |
174 | int type; | |
175 | int reference_count; | |
176 | OBJECT * rulename; | |
177 | struct arg_list * formal_arguments; | |
178 | int num_formal_arguments; | |
179 | }; | |
180 | ||
181 | typedef struct _builtin_function | |
182 | { | |
183 | FUNCTION base; | |
184 | LIST * ( * func )( FRAME *, int flags ); | |
185 | int flags; | |
186 | } BUILTIN_FUNCTION; | |
187 | ||
188 | typedef struct _jam_function | |
189 | { | |
190 | FUNCTION base; | |
191 | int code_size; | |
192 | instruction * code; | |
193 | int num_constants; | |
194 | OBJECT * * constants; | |
195 | int num_subfunctions; | |
196 | SUBFUNCTION * functions; | |
197 | int num_subactions; | |
198 | SUBACTION * actions; | |
199 | FUNCTION * generic; | |
200 | OBJECT * file; | |
201 | int line; | |
202 | } JAM_FUNCTION; | |
203 | ||
204 | ||
205 | #ifdef HAVE_PYTHON | |
206 | ||
207 | #define FUNCTION_PYTHON 2 | |
208 | ||
209 | typedef struct _python_function | |
210 | { | |
211 | FUNCTION base; | |
212 | PyObject * python_function; | |
213 | } PYTHON_FUNCTION; | |
214 | ||
215 | static LIST * call_python_function( PYTHON_FUNCTION *, FRAME * ); | |
216 | ||
217 | #endif | |
218 | ||
219 | ||
220 | struct _stack | |
221 | { | |
222 | void * data; | |
223 | }; | |
224 | ||
225 | static void * stack; | |
226 | ||
227 | STACK * stack_global() | |
228 | { | |
229 | static STACK result; | |
230 | if ( !stack ) | |
231 | { | |
232 | int const size = 1 << 21; | |
233 | stack = BJAM_MALLOC( size ); | |
234 | result.data = (char *)stack + size; | |
235 | } | |
236 | return &result; | |
237 | } | |
238 | ||
239 | struct list_alignment_helper | |
240 | { | |
241 | char ch; | |
242 | LIST * l; | |
243 | }; | |
244 | ||
245 | #define LISTPTR_ALIGN_BASE ( sizeof( struct list_alignment_helper ) - sizeof( LIST * ) ) | |
246 | #define LISTPTR_ALIGN ( ( LISTPTR_ALIGN_BASE > sizeof( LIST * ) ) ? sizeof( LIST * ) : LISTPTR_ALIGN_BASE ) | |
247 | ||
248 | static void check_alignment( STACK * s ) | |
249 | { | |
250 | assert( (size_t)s->data % LISTPTR_ALIGN == 0 ); | |
251 | } | |
252 | ||
253 | void * stack_allocate( STACK * s, int size ) | |
254 | { | |
255 | check_alignment( s ); | |
256 | s->data = (char *)s->data - size; | |
257 | check_alignment( s ); | |
258 | return s->data; | |
259 | } | |
260 | ||
261 | void stack_deallocate( STACK * s, int size ) | |
262 | { | |
263 | check_alignment( s ); | |
264 | s->data = (char *)s->data + size; | |
265 | check_alignment( s ); | |
266 | } | |
267 | ||
268 | void stack_push( STACK * s, LIST * l ) | |
269 | { | |
270 | *(LIST * *)stack_allocate( s, sizeof( LIST * ) ) = l; | |
271 | } | |
272 | ||
273 | LIST * stack_pop( STACK * s ) | |
274 | { | |
275 | LIST * const result = *(LIST * *)s->data; | |
276 | stack_deallocate( s, sizeof( LIST * ) ); | |
277 | return result; | |
278 | } | |
279 | ||
280 | LIST * stack_top( STACK * s ) | |
281 | { | |
282 | check_alignment( s ); | |
283 | return *(LIST * *)s->data; | |
284 | } | |
285 | ||
286 | LIST * stack_at( STACK * s, int n ) | |
287 | { | |
288 | check_alignment( s ); | |
289 | return *( (LIST * *)s->data + n ); | |
290 | } | |
291 | ||
292 | void stack_set( STACK * s, int n, LIST * value ) | |
293 | { | |
294 | check_alignment( s ); | |
295 | *((LIST * *)s->data + n) = value; | |
296 | } | |
297 | ||
298 | void * stack_get( STACK * s ) | |
299 | { | |
300 | check_alignment( s ); | |
301 | return s->data; | |
302 | } | |
303 | ||
304 | LIST * frame_get_local( FRAME * frame, int idx ) | |
305 | { | |
306 | /* The only local variables are the arguments. */ | |
307 | return list_copy( lol_get( frame->args, idx ) ); | |
308 | } | |
309 | ||
310 | static OBJECT * function_get_constant( JAM_FUNCTION * function, int idx ) | |
311 | { | |
312 | return function->constants[ idx ]; | |
313 | } | |
314 | ||
315 | static LIST * function_get_variable( JAM_FUNCTION * function, FRAME * frame, | |
316 | int idx ) | |
317 | { | |
318 | return list_copy( var_get( frame->module, function->constants[ idx ] ) ); | |
319 | } | |
320 | ||
321 | static void function_set_variable( JAM_FUNCTION * function, FRAME * frame, | |
322 | int idx, LIST * value ) | |
323 | { | |
324 | var_set( frame->module, function->constants[ idx ], value, VAR_SET ); | |
325 | } | |
326 | ||
327 | static LIST * function_swap_variable( JAM_FUNCTION * function, FRAME * frame, | |
328 | int idx, LIST * value ) | |
329 | { | |
330 | return var_swap( frame->module, function->constants[ idx ], value ); | |
331 | } | |
332 | ||
333 | static void function_append_variable( JAM_FUNCTION * function, FRAME * frame, | |
334 | int idx, LIST * value ) | |
335 | { | |
336 | var_set( frame->module, function->constants[ idx ], value, VAR_APPEND ); | |
337 | } | |
338 | ||
339 | static void function_default_variable( JAM_FUNCTION * function, FRAME * frame, | |
340 | int idx, LIST * value ) | |
341 | { | |
342 | var_set( frame->module, function->constants[ idx ], value, VAR_DEFAULT ); | |
343 | } | |
344 | ||
345 | static void function_set_rule( JAM_FUNCTION * function, FRAME * frame, | |
346 | STACK * s, int idx ) | |
347 | { | |
348 | SUBFUNCTION * sub = function->functions + idx; | |
349 | new_rule_body( frame->module, sub->name, sub->code, !sub->local ); | |
350 | } | |
351 | ||
352 | static void function_set_actions( JAM_FUNCTION * function, FRAME * frame, | |
353 | STACK * s, int idx ) | |
354 | { | |
355 | SUBACTION * sub = function->actions + idx; | |
356 | LIST * bindlist = stack_pop( s ); | |
357 | new_rule_actions( frame->module, sub->name, sub->command, bindlist, | |
358 | sub->flags ); | |
359 | } | |
360 | ||
361 | ||
362 | /* | |
363 | * Returns the index if name is "<", ">", "1", "2", ... or "19" otherwise | |
364 | * returns -1. | |
365 | */ | |
366 | ||
367 | static int get_argument_index( char const * s ) | |
368 | { | |
369 | if ( s[ 0 ] != '\0') | |
370 | { | |
371 | if ( s[ 1 ] == '\0' ) | |
372 | { | |
373 | switch ( s[ 0 ] ) | |
374 | { | |
375 | case '<': return 0; | |
376 | case '>': return 1; | |
377 | ||
378 | case '1': | |
379 | case '2': | |
380 | case '3': | |
381 | case '4': | |
382 | case '5': | |
383 | case '6': | |
384 | case '7': | |
385 | case '8': | |
386 | case '9': | |
387 | return s[ 0 ] - '1'; | |
388 | } | |
389 | } | |
390 | else if ( s[ 0 ] == '1' && s[ 2 ] == '\0' ) | |
391 | { | |
392 | switch( s[ 1 ] ) | |
393 | { | |
394 | case '0': | |
395 | case '1': | |
396 | case '2': | |
397 | case '3': | |
398 | case '4': | |
399 | case '5': | |
400 | case '6': | |
401 | case '7': | |
402 | case '8': | |
403 | case '9': | |
404 | return s[ 1 ] - '0' + 10 - 1; | |
405 | } | |
406 | } | |
407 | } | |
408 | return -1; | |
409 | } | |
410 | ||
411 | static LIST * function_get_named_variable( JAM_FUNCTION * function, | |
412 | FRAME * frame, OBJECT * name ) | |
413 | { | |
414 | int const idx = get_argument_index( object_str( name ) ); | |
415 | return idx == -1 | |
416 | ? list_copy( var_get( frame->module, name ) ) | |
417 | : list_copy( lol_get( frame->args, idx ) ); | |
418 | } | |
419 | ||
420 | static void function_set_named_variable( JAM_FUNCTION * function, FRAME * frame, | |
421 | OBJECT * name, LIST * value) | |
422 | { | |
423 | var_set( frame->module, name, value, VAR_SET ); | |
424 | } | |
425 | ||
426 | static LIST * function_swap_named_variable( JAM_FUNCTION * function, | |
427 | FRAME * frame, OBJECT * name, LIST * value ) | |
428 | { | |
429 | return var_swap( frame->module, name, value ); | |
430 | } | |
431 | ||
432 | static void function_append_named_variable( JAM_FUNCTION * function, | |
433 | FRAME * frame, OBJECT * name, LIST * value) | |
434 | { | |
435 | var_set( frame->module, name, value, VAR_APPEND ); | |
436 | } | |
437 | ||
438 | static void function_default_named_variable( JAM_FUNCTION * function, | |
439 | FRAME * frame, OBJECT * name, LIST * value ) | |
440 | { | |
441 | var_set( frame->module, name, value, VAR_DEFAULT ); | |
442 | } | |
443 | ||
444 | static LIST * function_call_rule( JAM_FUNCTION * function, FRAME * frame, | |
445 | STACK * s, int n_args, char const * unexpanded, OBJECT * file, int line ) | |
446 | { | |
447 | FRAME inner[ 1 ]; | |
448 | int i; | |
449 | LIST * first = stack_pop( s ); | |
450 | LIST * result = L0; | |
451 | OBJECT * rulename; | |
452 | LIST * trailing; | |
453 | ||
454 | frame->file = file; | |
455 | frame->line = line; | |
456 | ||
457 | if ( list_empty( first ) ) | |
458 | { | |
459 | backtrace_line( frame ); | |
460 | out_printf( "warning: rulename %s expands to empty string\n", unexpanded ); | |
461 | backtrace( frame ); | |
462 | list_free( first ); | |
463 | for ( i = 0; i < n_args; ++i ) | |
464 | list_free( stack_pop( s ) ); | |
465 | return result; | |
466 | } | |
467 | ||
468 | rulename = object_copy( list_front( first ) ); | |
469 | ||
470 | frame_init( inner ); | |
471 | inner->prev = frame; | |
472 | inner->prev_user = frame->module->user_module ? frame : frame->prev_user; | |
473 | inner->module = frame->module; /* This gets fixed up in evaluate_rule(). */ | |
474 | ||
475 | for ( i = 0; i < n_args; ++i ) | |
476 | lol_add( inner->args, stack_at( s, n_args - i - 1 ) ); | |
477 | ||
478 | for ( i = 0; i < n_args; ++i ) | |
479 | stack_pop( s ); | |
480 | ||
481 | trailing = list_pop_front( first ); | |
482 | if ( trailing ) | |
483 | { | |
484 | if ( inner->args->count == 0 ) | |
485 | lol_add( inner->args, trailing ); | |
486 | else | |
487 | { | |
488 | LIST * * const l = &inner->args->list[ 0 ]; | |
489 | *l = list_append( trailing, *l ); | |
490 | } | |
491 | } | |
492 | ||
493 | result = evaluate_rule( bindrule( rulename, inner->module ), rulename, inner ); | |
494 | frame_free( inner ); | |
495 | object_free( rulename ); | |
496 | return result; | |
497 | } | |
498 | ||
499 | static LIST * function_call_member_rule( JAM_FUNCTION * function, FRAME * frame, STACK * s, int n_args, OBJECT * rulename, OBJECT * file, int line ) | |
500 | { | |
501 | FRAME inner[ 1 ]; | |
502 | int i; | |
503 | LIST * first = stack_pop( s ); | |
504 | LIST * result = L0; | |
505 | LIST * trailing; | |
506 | RULE * rule; | |
507 | module_t * module; | |
508 | OBJECT * real_rulename = 0; | |
509 | ||
510 | frame->file = file; | |
511 | frame->line = line; | |
512 | ||
513 | if ( list_empty( first ) ) | |
514 | { | |
515 | backtrace_line( frame ); | |
516 | out_printf( "warning: object is empty\n" ); | |
517 | backtrace( frame ); | |
518 | ||
519 | list_free( first ); | |
520 | ||
521 | for( i = 0; i < n_args; ++i ) | |
522 | { | |
523 | list_free( stack_pop( s ) ); | |
524 | } | |
525 | ||
526 | return result; | |
527 | } | |
528 | ||
529 | /* FIXME: handle generic case */ | |
530 | assert( list_length( first ) == 1 ); | |
531 | ||
532 | module = bindmodule( list_front( first ) ); | |
533 | if ( module->class_module ) | |
534 | { | |
535 | rule = bindrule( rulename, module ); | |
536 | real_rulename = object_copy( function_rulename( rule->procedure ) ); | |
537 | } | |
538 | else | |
539 | { | |
540 | string buf[ 1 ]; | |
541 | string_new( buf ); | |
542 | string_append( buf, object_str( list_front( first ) ) ); | |
543 | string_push_back( buf, '.' ); | |
544 | string_append( buf, object_str( rulename ) ); | |
545 | real_rulename = object_new( buf->value ); | |
546 | string_free( buf ); | |
547 | rule = bindrule( real_rulename, frame->module ); | |
548 | } | |
549 | ||
550 | frame_init( inner ); | |
551 | ||
552 | inner->prev = frame; | |
553 | inner->prev_user = frame->module->user_module ? frame : frame->prev_user; | |
554 | inner->module = frame->module; /* This gets fixed up in evaluate_rule(), below. */ | |
555 | ||
556 | for( i = 0; i < n_args; ++i ) | |
557 | { | |
558 | lol_add( inner->args, stack_at( s, n_args - i - 1 ) ); | |
559 | } | |
560 | ||
561 | for( i = 0; i < n_args; ++i ) | |
562 | { | |
563 | stack_pop( s ); | |
564 | } | |
565 | ||
566 | if ( list_length( first ) > 1 ) | |
567 | { | |
568 | string buf[ 1 ]; | |
569 | LIST * trailing = L0; | |
570 | LISTITER iter = list_begin( first ), end = list_end( first ); | |
571 | iter = list_next( iter ); | |
572 | string_new( buf ); | |
573 | for ( ; iter != end; iter = list_next( iter ) ) | |
574 | { | |
575 | string_append( buf, object_str( list_item( iter ) ) ); | |
576 | string_push_back( buf, '.' ); | |
577 | string_append( buf, object_str( rulename ) ); | |
578 | trailing = list_push_back( trailing, object_new( buf->value ) ); | |
579 | string_truncate( buf, 0 ); | |
580 | } | |
581 | string_free( buf ); | |
582 | if ( inner->args->count == 0 ) | |
583 | lol_add( inner->args, trailing ); | |
584 | else | |
585 | { | |
586 | LIST * * const l = &inner->args->list[ 0 ]; | |
587 | *l = list_append( trailing, *l ); | |
588 | } | |
589 | } | |
590 | ||
591 | list_free( first ); | |
592 | result = evaluate_rule( rule, real_rulename, inner ); | |
593 | frame_free( inner ); | |
594 | object_free( real_rulename ); | |
595 | return result; | |
596 | } | |
597 | ||
598 | ||
599 | /* Variable expansion */ | |
600 | ||
601 | typedef struct | |
602 | { | |
603 | int sub1; | |
604 | int sub2; | |
605 | } subscript_t; | |
606 | ||
607 | typedef struct | |
608 | { | |
609 | PATHNAME f; /* :GDBSMR -- pieces */ | |
610 | char parent; /* :P -- go to parent directory */ | |
611 | char filemods; /* one of the above applied */ | |
612 | char downshift; /* :L -- downshift result */ | |
613 | char upshift; /* :U -- upshift result */ | |
614 | char to_slashes; /* :T -- convert "\" to "/" */ | |
615 | char to_windows; /* :W -- convert cygwin to native paths */ | |
616 | PATHPART empty; /* :E -- default for empties */ | |
617 | PATHPART join; /* :J -- join list with char */ | |
618 | } VAR_EDITS; | |
619 | ||
620 | static LIST * apply_modifiers_impl( LIST * result, string * buf, | |
621 | VAR_EDITS * edits, int n, LISTITER iter, LISTITER end ); | |
622 | static void get_iters( subscript_t const subscript, LISTITER * const first, | |
623 | LISTITER * const last, int const length ); | |
624 | ||
625 | ||
626 | /* | |
627 | * var_edit_parse() - parse : modifiers into PATHNAME structure | |
628 | * | |
629 | * The : modifiers in a $(varname:modifier) currently support replacing or | |
630 | * omitting elements of a filename, and so they are parsed into a PATHNAME | |
631 | * structure (which contains pointers into the original string). | |
632 | * | |
633 | * Modifiers of the form "X=value" replace the component X with the given value. | |
634 | * Modifiers without the "=value" cause everything but the component X to be | |
635 | * omitted. X is one of: | |
636 | * | |
637 | * G <grist> | |
638 | * D directory name | |
639 | * B base name | |
640 | * S .suffix | |
641 | * M (member) | |
642 | * R root directory - prepended to whole path | |
643 | * | |
644 | * This routine sets: | |
645 | * | |
646 | * f->f_xxx.ptr = 0 | |
647 | * f->f_xxx.len = 0 | |
648 | * -> leave the original component xxx | |
649 | * | |
650 | * f->f_xxx.ptr = string | |
651 | * f->f_xxx.len = strlen( string ) | |
652 | * -> replace component xxx with string | |
653 | * | |
654 | * f->f_xxx.ptr = "" | |
655 | * f->f_xxx.len = 0 | |
656 | * -> omit component xxx | |
657 | * | |
658 | * var_edit_file() below and path_build() obligingly follow this convention. | |
659 | */ | |
660 | ||
661 | static int var_edit_parse( char const * mods, VAR_EDITS * edits, int havezeroed | |
662 | ) | |
663 | { | |
664 | while ( *mods ) | |
665 | { | |
666 | PATHPART * fp; | |
667 | ||
668 | switch ( *mods++ ) | |
669 | { | |
670 | case 'L': edits->downshift = 1; continue; | |
671 | case 'U': edits->upshift = 1; continue; | |
672 | case 'P': edits->parent = edits->filemods = 1; continue; | |
673 | case 'E': fp = &edits->empty; goto strval; | |
674 | case 'J': fp = &edits->join; goto strval; | |
675 | case 'G': fp = &edits->f.f_grist; goto fileval; | |
676 | case 'R': fp = &edits->f.f_root; goto fileval; | |
677 | case 'D': fp = &edits->f.f_dir; goto fileval; | |
678 | case 'B': fp = &edits->f.f_base; goto fileval; | |
679 | case 'S': fp = &edits->f.f_suffix; goto fileval; | |
680 | case 'M': fp = &edits->f.f_member; goto fileval; | |
681 | case 'T': edits->to_slashes = 1; continue; | |
682 | case 'W': edits->to_windows = 1; continue; | |
683 | default: | |
684 | continue; /* Should complain, but so what... */ | |
685 | } | |
686 | ||
687 | fileval: | |
688 | /* Handle :CHARS, where each char (without a following =) selects a | |
689 | * particular file path element. On the first such char, we deselect all | |
690 | * others (by setting ptr = "", len = 0) and for each char we select | |
691 | * that element (by setting ptr = 0). | |
692 | */ | |
693 | edits->filemods = 1; | |
694 | ||
695 | if ( *mods != '=' ) | |
696 | { | |
697 | if ( !havezeroed++ ) | |
698 | { | |
699 | int i; | |
700 | for ( i = 0; i < 6; ++i ) | |
701 | { | |
702 | edits->f.part[ i ].len = 0; | |
703 | edits->f.part[ i ].ptr = ""; | |
704 | } | |
705 | } | |
706 | ||
707 | fp->ptr = 0; | |
708 | continue; | |
709 | } | |
710 | ||
711 | strval: | |
712 | /* Handle :X=value, or :X */ | |
713 | if ( *mods != '=' ) | |
714 | { | |
715 | fp->ptr = ""; | |
716 | fp->len = 0; | |
717 | } | |
718 | else | |
719 | { | |
720 | fp->ptr = ++mods; | |
721 | fp->len = strlen( mods ); | |
722 | mods += fp->len; | |
723 | } | |
724 | } | |
725 | ||
726 | return havezeroed; | |
727 | } | |
728 | ||
729 | ||
730 | /* | |
731 | * var_edit_file() - copy input target name to output, modifying filename. | |
732 | */ | |
733 | ||
734 | static void var_edit_file( char const * in, string * out, VAR_EDITS * edits ) | |
735 | { | |
736 | if ( edits->filemods ) | |
737 | { | |
738 | PATHNAME pathname; | |
739 | ||
740 | /* Parse apart original filename, putting parts into "pathname". */ | |
741 | path_parse( in, &pathname ); | |
742 | ||
743 | /* Replace any pathname with edits->f */ | |
744 | if ( edits->f.f_grist .ptr ) pathname.f_grist = edits->f.f_grist; | |
745 | if ( edits->f.f_root .ptr ) pathname.f_root = edits->f.f_root; | |
746 | if ( edits->f.f_dir .ptr ) pathname.f_dir = edits->f.f_dir; | |
747 | if ( edits->f.f_base .ptr ) pathname.f_base = edits->f.f_base; | |
748 | if ( edits->f.f_suffix.ptr ) pathname.f_suffix = edits->f.f_suffix; | |
749 | if ( edits->f.f_member.ptr ) pathname.f_member = edits->f.f_member; | |
750 | ||
751 | /* If requested, modify pathname to point to parent. */ | |
752 | if ( edits->parent ) | |
753 | path_parent( &pathname ); | |
754 | ||
755 | /* Put filename back together. */ | |
756 | path_build( &pathname, out ); | |
757 | } | |
758 | else | |
759 | string_append( out, in ); | |
760 | } | |
761 | ||
762 | ||
763 | /* | |
764 | * var_edit_translate_path() - translate path to os native format. | |
765 | */ | |
766 | ||
767 | static void var_edit_translate_path( string * out, size_t pos, VAR_EDITS * edits ) | |
768 | { | |
769 | if ( edits->to_windows ) | |
770 | { | |
771 | string result[ 1 ]; | |
772 | int translated; | |
773 | ||
774 | /* Translate path to os native format. */ | |
775 | translated = path_translate_to_os( out->value + pos, result ); | |
776 | if ( translated ) | |
777 | { | |
778 | string_truncate( out, pos ); | |
779 | string_append( out, result->value ); | |
780 | edits->to_slashes = 0; | |
781 | } | |
782 | ||
783 | string_free( result ); | |
784 | } | |
785 | } | |
786 | ||
787 | ||
788 | /* | |
789 | * var_edit_shift() - do upshift/downshift & other mods. | |
790 | */ | |
791 | ||
792 | static void var_edit_shift( string * out, size_t pos, VAR_EDITS * edits ) | |
793 | { | |
794 | #if defined( OS_CYGWIN ) || defined( OS_VMS ) | |
795 | var_edit_translate_path( out, pos, edits ); | |
796 | #endif | |
797 | ||
798 | if ( edits->upshift || edits->downshift || edits->to_slashes ) | |
799 | { | |
800 | /* Handle upshifting, downshifting and slash translation now. */ | |
801 | char * p; | |
802 | for ( p = out->value + pos; *p; ++p ) | |
803 | { | |
804 | if ( edits->upshift ) | |
805 | *p = toupper( *p ); | |
806 | else if ( edits->downshift ) | |
807 | *p = tolower( *p ); | |
808 | if ( edits->to_slashes && ( *p == '\\' ) ) | |
809 | *p = '/'; | |
810 | } | |
811 | } | |
812 | } | |
813 | ||
814 | ||
815 | /* | |
816 | * Reads n LISTs from the top of the STACK and combines them to form VAR_EDITS. | |
817 | * Returns the number of VAR_EDITS pushed onto the STACK. | |
818 | */ | |
819 | ||
820 | static int expand_modifiers( STACK * s, int n ) | |
821 | { | |
822 | int i; | |
823 | int total = 1; | |
824 | LIST * * args = stack_get( s ); | |
825 | for ( i = 0; i < n; ++i ) | |
826 | total *= list_length( args[ i ] ); | |
827 | ||
828 | if ( total != 0 ) | |
829 | { | |
830 | VAR_EDITS * out = stack_allocate( s, total * sizeof( VAR_EDITS ) ); | |
831 | LISTITER * iter = stack_allocate( s, n * sizeof( LIST * ) ); | |
832 | for ( i = 0; i < n; ++i ) | |
833 | iter[ i ] = list_begin( args[ i ] ); | |
834 | i = 0; | |
835 | { | |
836 | int havezeroed; | |
837 | loop: | |
838 | memset( out, 0, sizeof( *out ) ); | |
839 | havezeroed = 0; | |
840 | for ( i = 0; i < n; ++i ) | |
841 | havezeroed = var_edit_parse( object_str( list_item( iter[ i ] ) | |
842 | ), out, havezeroed ); | |
843 | ++out; | |
844 | while ( --i >= 0 ) | |
845 | { | |
846 | if ( list_next( iter[ i ] ) != list_end( args[ i ] ) ) | |
847 | { | |
848 | iter[ i ] = list_next( iter[ i ] ); | |
849 | goto loop; | |
850 | } | |
851 | iter[ i ] = list_begin( args[ i ] ); | |
852 | } | |
853 | } | |
854 | stack_deallocate( s, n * sizeof( LIST * ) ); | |
855 | } | |
856 | return total; | |
857 | } | |
858 | ||
859 | static LIST * apply_modifiers( STACK * s, int n ) | |
860 | { | |
861 | LIST * value = stack_top( s ); | |
862 | LIST * result = L0; | |
863 | VAR_EDITS * const edits = (VAR_EDITS *)( (LIST * *)stack_get( s ) + 1 ); | |
864 | string buf[ 1 ]; | |
865 | string_new( buf ); | |
866 | result = apply_modifiers_impl( result, buf, edits, n, list_begin( value ), | |
867 | list_end( value ) ); | |
868 | string_free( buf ); | |
869 | return result; | |
870 | } | |
871 | ||
872 | ||
873 | /* | |
874 | * Parse a string of the form "1-2", "-2--1", "2-" and return the two | |
875 | * subscripts. | |
876 | */ | |
877 | ||
878 | subscript_t parse_subscript( char const * s ) | |
879 | { | |
880 | subscript_t result; | |
881 | result.sub1 = 0; | |
882 | result.sub2 = 0; | |
883 | do /* so we can use "break" */ | |
884 | { | |
885 | /* Allow negative subscripts. */ | |
886 | if ( !isdigit( *s ) && ( *s != '-' ) ) | |
887 | { | |
888 | result.sub2 = 0; | |
889 | break; | |
890 | } | |
891 | result.sub1 = atoi( s ); | |
892 | ||
893 | /* Skip over the first symbol, which is either a digit or dash. */ | |
894 | ++s; | |
895 | while ( isdigit( *s ) ) ++s; | |
896 | ||
897 | if ( *s == '\0' ) | |
898 | { | |
899 | result.sub2 = result.sub1; | |
900 | break; | |
901 | } | |
902 | ||
903 | if ( *s != '-' ) | |
904 | { | |
905 | result.sub2 = 0; | |
906 | break; | |
907 | } | |
908 | ||
909 | ++s; | |
910 | ||
911 | if ( *s == '\0' ) | |
912 | { | |
913 | result.sub2 = -1; | |
914 | break; | |
915 | } | |
916 | ||
917 | if ( !isdigit( *s ) && ( *s != '-' ) ) | |
918 | { | |
919 | result.sub2 = 0; | |
920 | break; | |
921 | } | |
922 | ||
923 | /* First, compute the index of the last element. */ | |
924 | result.sub2 = atoi( s ); | |
925 | while ( isdigit( *++s ) ); | |
926 | ||
927 | if ( *s != '\0' ) | |
928 | result.sub2 = 0; | |
929 | ||
930 | } while ( 0 ); | |
931 | return result; | |
932 | } | |
933 | ||
934 | static LIST * apply_subscript( STACK * s ) | |
935 | { | |
936 | LIST * value = stack_top( s ); | |
937 | LIST * indices = stack_at( s, 1 ); | |
938 | LIST * result = L0; | |
939 | int length = list_length( value ); | |
940 | string buf[ 1 ]; | |
941 | LISTITER indices_iter = list_begin( indices ); | |
942 | LISTITER const indices_end = list_end( indices ); | |
943 | string_new( buf ); | |
944 | for ( ; indices_iter != indices_end; indices_iter = list_next( indices_iter | |
945 | ) ) | |
946 | { | |
947 | LISTITER iter = list_begin( value ); | |
948 | LISTITER end = list_end( value ); | |
949 | subscript_t const subscript = parse_subscript( object_str( list_item( | |
950 | indices_iter ) ) ); | |
951 | get_iters( subscript, &iter, &end, length ); | |
952 | for ( ; iter != end; iter = list_next( iter ) ) | |
953 | result = list_push_back( result, object_copy( list_item( iter ) ) ); | |
954 | } | |
955 | string_free( buf ); | |
956 | return result; | |
957 | } | |
958 | ||
959 | ||
960 | /* | |
961 | * Reads the LIST from first and applies subscript to it. The results are | |
962 | * written to *first and *last. | |
963 | */ | |
964 | ||
965 | static void get_iters( subscript_t const subscript, LISTITER * const first, | |
966 | LISTITER * const last, int const length ) | |
967 | { | |
968 | int start; | |
969 | int size; | |
970 | LISTITER iter; | |
971 | LISTITER end; | |
972 | { | |
973 | ||
974 | if ( subscript.sub1 < 0 ) | |
975 | start = length + subscript.sub1; | |
976 | else if ( subscript.sub1 > length ) | |
977 | start = length; | |
978 | else | |
979 | start = subscript.sub1 - 1; | |
980 | ||
981 | size = subscript.sub2 < 0 | |
982 | ? length + 1 + subscript.sub2 - start | |
983 | : subscript.sub2 - start; | |
984 | ||
985 | /* | |
986 | * HACK: When the first subscript is before the start of the list, it | |
987 | * magically becomes the beginning of the list. This is inconsistent, | |
988 | * but needed for backwards compatibility. | |
989 | */ | |
990 | if ( start < 0 ) | |
991 | start = 0; | |
992 | ||
993 | /* The "sub2 < 0" test handles the semantic error of sub2 < sub1. */ | |
994 | if ( size < 0 ) | |
995 | size = 0; | |
996 | ||
997 | if ( start + size > length ) | |
998 | size = length - start; | |
999 | } | |
1000 | ||
1001 | iter = *first; | |
1002 | while ( start-- > 0 ) | |
1003 | iter = list_next( iter ); | |
1004 | ||
1005 | end = iter; | |
1006 | while ( size-- > 0 ) | |
1007 | end = list_next( end ); | |
1008 | ||
1009 | *first = iter; | |
1010 | *last = end; | |
1011 | } | |
1012 | ||
1013 | static LIST * apply_modifiers_empty( LIST * result, string * buf, | |
1014 | VAR_EDITS * edits, int n ) | |
1015 | { | |
1016 | int i; | |
1017 | for ( i = 0; i < n; ++i ) | |
1018 | { | |
1019 | if ( edits[ i ].empty.ptr ) | |
1020 | { | |
1021 | /** FIXME: is empty.ptr always null-terminated? */ | |
1022 | var_edit_file( edits[ i ].empty.ptr, buf, edits + i ); | |
1023 | var_edit_shift( buf, 0, edits + i ); | |
1024 | result = list_push_back( result, object_new( buf->value ) ); | |
1025 | string_truncate( buf, 0 ); | |
1026 | } | |
1027 | } | |
1028 | return result; | |
1029 | } | |
1030 | ||
1031 | static LIST * apply_modifiers_non_empty( LIST * result, string * buf, | |
1032 | VAR_EDITS * edits, int n, LISTITER begin, LISTITER end ) | |
1033 | { | |
1034 | int i; | |
1035 | LISTITER iter; | |
1036 | for ( i = 0; i < n; ++i ) | |
1037 | { | |
1038 | if ( edits[ i ].join.ptr ) | |
1039 | { | |
1040 | var_edit_file( object_str( list_item( begin ) ), buf, edits + i ); | |
1041 | var_edit_shift( buf, 0, edits + i ); | |
1042 | for ( iter = list_next( begin ); iter != end; iter = list_next( iter | |
1043 | ) ) | |
1044 | { | |
1045 | size_t size; | |
1046 | string_append( buf, edits[ i ].join.ptr ); | |
1047 | size = buf->size; | |
1048 | var_edit_file( object_str( list_item( iter ) ), buf, edits + i | |
1049 | ); | |
1050 | var_edit_shift( buf, size, edits + i ); | |
1051 | } | |
1052 | result = list_push_back( result, object_new( buf->value ) ); | |
1053 | string_truncate( buf, 0 ); | |
1054 | } | |
1055 | else | |
1056 | { | |
1057 | for ( iter = begin; iter != end; iter = list_next( iter ) ) | |
1058 | { | |
1059 | var_edit_file( object_str( list_item( iter ) ), buf, edits + i ); | |
1060 | var_edit_shift( buf, 0, edits + i ); | |
1061 | result = list_push_back( result, object_new( buf->value ) ); | |
1062 | string_truncate( buf, 0 ); | |
1063 | } | |
1064 | } | |
1065 | } | |
1066 | return result; | |
1067 | } | |
1068 | ||
1069 | static LIST * apply_modifiers_impl( LIST * result, string * buf, | |
1070 | VAR_EDITS * edits, int n, LISTITER iter, LISTITER end ) | |
1071 | { | |
1072 | return iter == end | |
1073 | ? apply_modifiers_empty( result, buf, edits, n ) | |
1074 | : apply_modifiers_non_empty( result, buf, edits, n, iter, end ); | |
1075 | } | |
1076 | ||
1077 | static LIST * apply_subscript_and_modifiers( STACK * s, int n ) | |
1078 | { | |
1079 | LIST * const value = stack_top( s ); | |
1080 | LIST * const indices = stack_at( s, 1 ); | |
1081 | LIST * result = L0; | |
1082 | VAR_EDITS * const edits = (VAR_EDITS *)((LIST * *)stack_get( s ) + 2); | |
1083 | int const length = list_length( value ); | |
1084 | string buf[ 1 ]; | |
1085 | LISTITER indices_iter = list_begin( indices ); | |
1086 | LISTITER const indices_end = list_end( indices ); | |
1087 | string_new( buf ); | |
1088 | for ( ; indices_iter != indices_end; indices_iter = list_next( indices_iter | |
1089 | ) ) | |
1090 | { | |
1091 | LISTITER iter = list_begin( value ); | |
1092 | LISTITER end = list_end( value ); | |
1093 | subscript_t const sub = parse_subscript( object_str( list_item( | |
1094 | indices_iter ) ) ); | |
1095 | get_iters( sub, &iter, &end, length ); | |
1096 | result = apply_modifiers_impl( result, buf, edits, n, iter, end ); | |
1097 | } | |
1098 | string_free( buf ); | |
1099 | return result; | |
1100 | } | |
1101 | ||
1102 | ||
1103 | /* | |
1104 | * expand() - expands a list of concatenated strings and variable refereces | |
1105 | * | |
1106 | * Takes a list of expansion items - each representing one element to be | |
1107 | * concatenated and each containing a list of its values. Returns a list of all | |
1108 | * possible values constructed by selecting a single value from each of the | |
1109 | * elements and concatenating them together. | |
1110 | * | |
1111 | * For example, in the following code: | |
1112 | * | |
1113 | * local a = one two three four ; | |
1114 | * local b = foo bar ; | |
1115 | * ECHO /$(a)/$(b)/$(a)/ ; | |
1116 | * | |
1117 | * When constructing the result of /$(a)/$(b)/ this function would get called | |
1118 | * with the following 7 expansion items: | |
1119 | * 1. / | |
1120 | * 2. one two three four | |
1121 | * 3. / | |
1122 | * 4. foo bar | |
1123 | * 5. / | |
1124 | * 6. one two three four | |
1125 | * 7. / | |
1126 | * | |
1127 | * And would result in a list containing 32 values: | |
1128 | * 1. /one/foo/one/ | |
1129 | * 2. /one/foo/two/ | |
1130 | * 3. /one/foo/three/ | |
1131 | * 4. /one/foo/four/ | |
1132 | * 5. /one/bar/one/ | |
1133 | * ... | |
1134 | * | |
1135 | */ | |
1136 | ||
1137 | typedef struct expansion_item | |
1138 | { | |
1139 | /* Item's value list initialized prior to calling expand(). */ | |
1140 | LIST * values; | |
1141 | ||
1142 | /* Internal data initialized and used inside expand(). */ | |
1143 | LISTITER current; /* Currently used value. */ | |
1144 | int size; /* Concatenated string length prior to concatenating the | |
1145 | * item's current value. | |
1146 | */ | |
1147 | } expansion_item; | |
1148 | ||
1149 | static LIST * expand( expansion_item * items, int const length ) | |
1150 | { | |
1151 | LIST * result = L0; | |
1152 | string buf[ 1 ]; | |
1153 | int size = 0; | |
1154 | int i; | |
1155 | ||
1156 | assert( length > 0 ); | |
1157 | for ( i = 0; i < length; ++i ) | |
1158 | { | |
1159 | LISTITER iter = list_begin( items[ i ].values ); | |
1160 | LISTITER const end = list_end( items[ i ].values ); | |
1161 | ||
1162 | /* If any of the items has no values - the result is an empty list. */ | |
1163 | if ( iter == end ) return L0; | |
1164 | ||
1165 | /* Set each item's 'current' to its first listed value. This indicates | |
1166 | * each item's next value to be used when constructing the list of all | |
1167 | * possible concatenated values. | |
1168 | */ | |
1169 | items[ i ].current = iter; | |
1170 | ||
1171 | /* Calculate the longest concatenated string length - to know how much | |
1172 | * memory we need to allocate as a buffer for holding the concatenated | |
1173 | * strings. | |
1174 | */ | |
1175 | { | |
1176 | int max = 0; | |
1177 | for ( ; iter != end; iter = list_next( iter ) ) | |
1178 | { | |
1179 | int const len = strlen( object_str( list_item( iter ) ) ); | |
1180 | if ( len > max ) max = len; | |
1181 | } | |
1182 | size += max; | |
1183 | } | |
1184 | } | |
1185 | ||
1186 | string_new( buf ); | |
1187 | string_reserve( buf, size ); | |
1188 | ||
1189 | i = 0; | |
1190 | while ( i >= 0 ) | |
1191 | { | |
1192 | for ( ; i < length; ++i ) | |
1193 | { | |
1194 | items[ i ].size = buf->size; | |
1195 | string_append( buf, object_str( list_item( items[ i ].current ) ) ); | |
1196 | } | |
1197 | result = list_push_back( result, object_new( buf->value ) ); | |
1198 | while ( --i >= 0 ) | |
1199 | { | |
1200 | if ( list_next( items[ i ].current ) != list_end( items[ i ].values | |
1201 | ) ) | |
1202 | { | |
1203 | items[ i ].current = list_next( items[ i ].current ); | |
1204 | string_truncate( buf, items[ i ].size ); | |
1205 | break; | |
1206 | } | |
1207 | else | |
1208 | items[ i ].current = list_begin( items[ i ].values ); | |
1209 | } | |
1210 | } | |
1211 | ||
1212 | string_free( buf ); | |
1213 | return result; | |
1214 | } | |
1215 | ||
1216 | static void combine_strings( STACK * s, int n, string * out ) | |
1217 | { | |
1218 | int i; | |
1219 | for ( i = 0; i < n; ++i ) | |
1220 | { | |
1221 | LIST * const values = stack_pop( s ); | |
1222 | LISTITER iter = list_begin( values ); | |
1223 | LISTITER const end = list_end( values ); | |
1224 | if ( iter != end ) | |
1225 | { | |
1226 | string_append( out, object_str( list_item( iter ) ) ); | |
1227 | for ( iter = list_next( iter ); iter != end; iter = list_next( iter | |
1228 | ) ) | |
1229 | { | |
1230 | string_push_back( out, ' ' ); | |
1231 | string_append( out, object_str( list_item( iter ) ) ); | |
1232 | } | |
1233 | list_free( values ); | |
1234 | } | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | struct dynamic_array | |
1239 | { | |
1240 | int size; | |
1241 | int capacity; | |
1242 | void * data; | |
1243 | }; | |
1244 | ||
1245 | static void dynamic_array_init( struct dynamic_array * array ) | |
1246 | { | |
1247 | array->size = 0; | |
1248 | array->capacity = 0; | |
1249 | array->data = 0; | |
1250 | } | |
1251 | ||
1252 | static void dynamic_array_free( struct dynamic_array * array ) | |
1253 | { | |
1254 | BJAM_FREE( array->data ); | |
1255 | } | |
1256 | ||
1257 | static void dynamic_array_push_impl( struct dynamic_array * const array, | |
1258 | void const * const value, int const unit_size ) | |
1259 | { | |
1260 | if ( array->capacity == 0 ) | |
1261 | { | |
1262 | array->capacity = 2; | |
1263 | array->data = BJAM_MALLOC( array->capacity * unit_size ); | |
1264 | } | |
1265 | else if ( array->capacity == array->size ) | |
1266 | { | |
1267 | void * new_data; | |
1268 | array->capacity *= 2; | |
1269 | new_data = BJAM_MALLOC( array->capacity * unit_size ); | |
1270 | memcpy( new_data, array->data, array->size * unit_size ); | |
1271 | BJAM_FREE( array->data ); | |
1272 | array->data = new_data; | |
1273 | } | |
1274 | memcpy( (char *)array->data + array->size * unit_size, value, unit_size ); | |
1275 | ++array->size; | |
1276 | } | |
1277 | ||
1278 | #define dynamic_array_push( array, value ) (dynamic_array_push_impl(array, &value, sizeof(value))) | |
1279 | #define dynamic_array_at( type, array, idx ) (((type *)(array)->data)[idx]) | |
1280 | #define dynamic_array_pop( array ) (--(array)->size) | |
1281 | ||
1282 | /* | |
1283 | * struct compiler | |
1284 | */ | |
1285 | ||
1286 | struct label_info | |
1287 | { | |
1288 | int absolute_position; | |
1289 | struct dynamic_array uses[ 1 ]; | |
1290 | }; | |
1291 | ||
1292 | #define LOOP_INFO_BREAK 0 | |
1293 | #define LOOP_INFO_CONTINUE 1 | |
1294 | ||
1295 | struct loop_info | |
1296 | { | |
1297 | int type; | |
1298 | int label; | |
1299 | int cleanup_depth; | |
1300 | }; | |
1301 | ||
1302 | struct stored_rule | |
1303 | { | |
1304 | OBJECT * name; | |
1305 | PARSE * parse; | |
1306 | int num_arguments; | |
1307 | struct arg_list * arguments; | |
1308 | int local; | |
1309 | }; | |
1310 | ||
1311 | typedef struct compiler | |
1312 | { | |
1313 | struct dynamic_array code[ 1 ]; | |
1314 | struct dynamic_array constants[ 1 ]; | |
1315 | struct dynamic_array labels[ 1 ]; | |
1316 | struct dynamic_array rules[ 1 ]; | |
1317 | struct dynamic_array actions[ 1 ]; | |
1318 | struct dynamic_array cleanups[ 1 ]; | |
1319 | struct dynamic_array loop_scopes[ 1 ]; | |
1320 | } compiler; | |
1321 | ||
1322 | static void compiler_init( compiler * c ) | |
1323 | { | |
1324 | dynamic_array_init( c->code ); | |
1325 | dynamic_array_init( c->constants ); | |
1326 | dynamic_array_init( c->labels ); | |
1327 | dynamic_array_init( c->rules ); | |
1328 | dynamic_array_init( c->actions ); | |
1329 | dynamic_array_init( c->cleanups ); | |
1330 | dynamic_array_init( c->loop_scopes ); | |
1331 | } | |
1332 | ||
1333 | static void compiler_free( compiler * c ) | |
1334 | { | |
1335 | int i; | |
1336 | dynamic_array_free( c->actions ); | |
1337 | dynamic_array_free( c->rules ); | |
1338 | for ( i = 0; i < c->labels->size; ++i ) | |
1339 | dynamic_array_free( dynamic_array_at( struct label_info, c->labels, i | |
1340 | ).uses ); | |
1341 | dynamic_array_free( c->labels ); | |
1342 | dynamic_array_free( c->constants ); | |
1343 | dynamic_array_free( c->code ); | |
1344 | dynamic_array_free( c->cleanups ); | |
1345 | dynamic_array_free( c->loop_scopes ); | |
1346 | } | |
1347 | ||
1348 | static void compile_emit_instruction( compiler * c, instruction instr ) | |
1349 | { | |
1350 | dynamic_array_push( c->code, instr ); | |
1351 | } | |
1352 | ||
1353 | static int compile_new_label( compiler * c ) | |
1354 | { | |
1355 | int result = c->labels->size; | |
1356 | struct label_info info; | |
1357 | info.absolute_position = -1; | |
1358 | dynamic_array_init( info.uses ); | |
1359 | dynamic_array_push( c->labels, info ); | |
1360 | return result; | |
1361 | } | |
1362 | ||
1363 | static void compile_set_label( compiler * c, int label ) | |
1364 | { | |
1365 | struct label_info * const l = &dynamic_array_at( struct label_info, | |
1366 | c->labels, label ); | |
1367 | int const pos = c->code->size; | |
1368 | int i; | |
1369 | assert( l->absolute_position == -1 ); | |
1370 | l->absolute_position = pos; | |
1371 | for ( i = 0; i < l->uses->size; ++i ) | |
1372 | { | |
1373 | int id = dynamic_array_at( int, l->uses, i ); | |
1374 | int offset = (int)( pos - id - 1 ); | |
1375 | dynamic_array_at( instruction, c->code, id ).arg = offset; | |
1376 | } | |
1377 | } | |
1378 | ||
1379 | static void compile_emit( compiler * c, unsigned int op_code, int arg ) | |
1380 | { | |
1381 | instruction instr; | |
1382 | instr.op_code = op_code; | |
1383 | instr.arg = arg; | |
1384 | compile_emit_instruction( c, instr ); | |
1385 | } | |
1386 | ||
1387 | static void compile_emit_branch( compiler * c, unsigned int op_code, int label ) | |
1388 | { | |
1389 | struct label_info * const l = &dynamic_array_at( struct label_info, | |
1390 | c->labels, label ); | |
1391 | int const pos = c->code->size; | |
1392 | instruction instr; | |
1393 | instr.op_code = op_code; | |
1394 | if ( l->absolute_position == -1 ) | |
1395 | { | |
1396 | instr.arg = 0; | |
1397 | dynamic_array_push( l->uses, pos ); | |
1398 | } | |
1399 | else | |
1400 | instr.arg = (int)( l->absolute_position - pos - 1 ); | |
1401 | compile_emit_instruction( c, instr ); | |
1402 | } | |
1403 | ||
1404 | static int compile_emit_constant( compiler * c, OBJECT * value ) | |
1405 | { | |
1406 | OBJECT * copy = object_copy( value ); | |
1407 | dynamic_array_push( c->constants, copy ); | |
1408 | return c->constants->size - 1; | |
1409 | } | |
1410 | ||
1411 | static void compile_push_cleanup( compiler * c, unsigned int op_code, int arg ) | |
1412 | { | |
1413 | instruction instr; | |
1414 | instr.op_code = op_code; | |
1415 | instr.arg = arg; | |
1416 | dynamic_array_push( c->cleanups, instr ); | |
1417 | } | |
1418 | ||
1419 | static void compile_pop_cleanup( compiler * c ) | |
1420 | { | |
1421 | dynamic_array_pop( c->cleanups ); | |
1422 | } | |
1423 | ||
1424 | static void compile_emit_cleanups( compiler * c, int end ) | |
1425 | { | |
1426 | int i; | |
1427 | for ( i = c->cleanups->size; --i >= end; ) | |
1428 | { | |
1429 | compile_emit_instruction( c, dynamic_array_at( instruction, c->cleanups, i ) ); | |
1430 | } | |
1431 | } | |
1432 | ||
1433 | static void compile_emit_loop_jump( compiler * c, int type ) | |
1434 | { | |
1435 | struct loop_info * info = NULL; | |
1436 | int i; | |
1437 | for ( i = c->loop_scopes->size; --i >= 0; ) | |
1438 | { | |
1439 | struct loop_info * elem = &dynamic_array_at( struct loop_info, c->loop_scopes, i ); | |
1440 | if ( elem->type == type ) | |
1441 | { | |
1442 | info = elem; | |
1443 | break; | |
1444 | } | |
1445 | } | |
1446 | if ( info == NULL ) | |
1447 | { | |
1448 | printf( "warning: ignoring break statement used outside of loop\n" ); | |
1449 | return; | |
1450 | } | |
1451 | compile_emit_cleanups( c, info->cleanup_depth ); | |
1452 | compile_emit_branch( c, INSTR_JUMP, info->label ); | |
1453 | } | |
1454 | ||
1455 | static void compile_push_break_scope( compiler * c, int label ) | |
1456 | { | |
1457 | struct loop_info info; | |
1458 | info.type = LOOP_INFO_BREAK; | |
1459 | info.label = label; | |
1460 | info.cleanup_depth = c->cleanups->size; | |
1461 | dynamic_array_push( c->loop_scopes, info ); | |
1462 | } | |
1463 | ||
1464 | static void compile_push_continue_scope( compiler * c, int label ) | |
1465 | { | |
1466 | struct loop_info info; | |
1467 | info.type = LOOP_INFO_CONTINUE; | |
1468 | info.label = label; | |
1469 | info.cleanup_depth = c->cleanups->size; | |
1470 | dynamic_array_push( c->loop_scopes, info ); | |
1471 | } | |
1472 | ||
1473 | static void compile_pop_break_scope( compiler * c ) | |
1474 | { | |
1475 | assert( c->loop_scopes->size > 0 ); | |
1476 | assert( dynamic_array_at( struct loop_info, c->loop_scopes, c->loop_scopes->size - 1 ).type == LOOP_INFO_BREAK ); | |
1477 | dynamic_array_pop( c->loop_scopes ); | |
1478 | } | |
1479 | ||
1480 | static void compile_pop_continue_scope( compiler * c ) | |
1481 | { | |
1482 | assert( c->loop_scopes->size > 0 ); | |
1483 | assert( dynamic_array_at( struct loop_info, c->loop_scopes, c->loop_scopes->size - 1 ).type == LOOP_INFO_CONTINUE ); | |
1484 | dynamic_array_pop( c->loop_scopes ); | |
1485 | } | |
1486 | ||
1487 | static int compile_emit_rule( compiler * c, OBJECT * name, PARSE * parse, | |
1488 | int num_arguments, struct arg_list * arguments, int local ) | |
1489 | { | |
1490 | struct stored_rule rule; | |
1491 | rule.name = object_copy( name ); | |
1492 | rule.parse = parse; | |
1493 | rule.num_arguments = num_arguments; | |
1494 | rule.arguments = arguments; | |
1495 | rule.local = local; | |
1496 | dynamic_array_push( c->rules, rule ); | |
1497 | return (int)( c->rules->size - 1 ); | |
1498 | } | |
1499 | ||
1500 | static int compile_emit_actions( compiler * c, PARSE * parse ) | |
1501 | { | |
1502 | SUBACTION a; | |
1503 | a.name = object_copy( parse->string ); | |
1504 | a.command = function_compile_actions( object_str( parse->string1 ), | |
1505 | parse->file, parse->line ); | |
1506 | a.flags = parse->num; | |
1507 | dynamic_array_push( c->actions, a ); | |
1508 | return (int)( c->actions->size - 1 ); | |
1509 | } | |
1510 | ||
1511 | static JAM_FUNCTION * compile_to_function( compiler * c ) | |
1512 | { | |
1513 | JAM_FUNCTION * const result = BJAM_MALLOC( sizeof( JAM_FUNCTION ) ); | |
1514 | int i; | |
1515 | result->base.type = FUNCTION_JAM; | |
1516 | result->base.reference_count = 1; | |
1517 | result->base.formal_arguments = 0; | |
1518 | result->base.num_formal_arguments = 0; | |
1519 | ||
1520 | result->base.rulename = 0; | |
1521 | ||
1522 | result->code_size = c->code->size; | |
1523 | result->code = BJAM_MALLOC( c->code->size * sizeof( instruction ) ); | |
1524 | memcpy( result->code, c->code->data, c->code->size * sizeof( instruction ) ); | |
1525 | ||
1526 | result->constants = BJAM_MALLOC( c->constants->size * sizeof( OBJECT * ) ); | |
1527 | memcpy( result->constants, c->constants->data, c->constants->size * sizeof( | |
1528 | OBJECT * ) ); | |
1529 | result->num_constants = c->constants->size; | |
1530 | ||
1531 | result->num_subfunctions = c->rules->size; | |
1532 | result->functions = BJAM_MALLOC( c->rules->size * sizeof( SUBFUNCTION ) ); | |
1533 | for ( i = 0; i < c->rules->size; ++i ) | |
1534 | { | |
1535 | struct stored_rule * const rule = &dynamic_array_at( struct stored_rule, | |
1536 | c->rules, i ); | |
1537 | result->functions[ i ].name = rule->name; | |
1538 | result->functions[ i ].code = function_compile( rule->parse ); | |
1539 | result->functions[ i ].code->num_formal_arguments = rule->num_arguments; | |
1540 | result->functions[ i ].code->formal_arguments = rule->arguments; | |
1541 | result->functions[ i ].local = rule->local; | |
1542 | } | |
1543 | ||
1544 | result->actions = BJAM_MALLOC( c->actions->size * sizeof( SUBACTION ) ); | |
1545 | memcpy( result->actions, c->actions->data, c->actions->size * sizeof( | |
1546 | SUBACTION ) ); | |
1547 | result->num_subactions = c->actions->size; | |
1548 | ||
1549 | result->generic = 0; | |
1550 | ||
1551 | result->file = 0; | |
1552 | result->line = -1; | |
1553 | ||
1554 | return result; | |
1555 | } | |
1556 | ||
1557 | ||
1558 | /* | |
1559 | * Parsing of variable expansions | |
1560 | */ | |
1561 | ||
1562 | typedef struct VAR_PARSE_GROUP | |
1563 | { | |
1564 | struct dynamic_array elems[ 1 ]; | |
1565 | } VAR_PARSE_GROUP; | |
1566 | ||
1567 | typedef struct VAR_PARSE_ACTIONS | |
1568 | { | |
1569 | struct dynamic_array elems[ 1 ]; | |
1570 | } VAR_PARSE_ACTIONS; | |
1571 | ||
1572 | #define VAR_PARSE_TYPE_VAR 0 | |
1573 | #define VAR_PARSE_TYPE_STRING 1 | |
1574 | #define VAR_PARSE_TYPE_FILE 2 | |
1575 | ||
1576 | typedef struct _var_parse | |
1577 | { | |
1578 | int type; /* string, variable or file */ | |
1579 | } VAR_PARSE; | |
1580 | ||
1581 | typedef struct | |
1582 | { | |
1583 | VAR_PARSE base; | |
1584 | VAR_PARSE_GROUP * name; | |
1585 | VAR_PARSE_GROUP * subscript; | |
1586 | struct dynamic_array modifiers[ 1 ]; | |
1587 | } VAR_PARSE_VAR; | |
1588 | ||
1589 | typedef struct | |
1590 | { | |
1591 | VAR_PARSE base; | |
1592 | OBJECT * s; | |
1593 | } VAR_PARSE_STRING; | |
1594 | ||
1595 | typedef struct | |
1596 | { | |
1597 | VAR_PARSE base; | |
1598 | struct dynamic_array filename[ 1 ]; | |
1599 | struct dynamic_array contents[ 1 ]; | |
1600 | } VAR_PARSE_FILE; | |
1601 | ||
1602 | static void var_parse_free( VAR_PARSE * ); | |
1603 | ||
1604 | ||
1605 | /* | |
1606 | * VAR_PARSE_GROUP | |
1607 | */ | |
1608 | ||
1609 | static VAR_PARSE_GROUP * var_parse_group_new() | |
1610 | { | |
1611 | VAR_PARSE_GROUP * const result = BJAM_MALLOC( sizeof( VAR_PARSE_GROUP ) ); | |
1612 | dynamic_array_init( result->elems ); | |
1613 | return result; | |
1614 | } | |
1615 | ||
1616 | static void var_parse_group_free( VAR_PARSE_GROUP * group ) | |
1617 | { | |
1618 | int i; | |
1619 | for ( i = 0; i < group->elems->size; ++i ) | |
1620 | var_parse_free( dynamic_array_at( VAR_PARSE *, group->elems, i ) ); | |
1621 | dynamic_array_free( group->elems ); | |
1622 | BJAM_FREE( group ); | |
1623 | } | |
1624 | ||
1625 | static void var_parse_group_add( VAR_PARSE_GROUP * group, VAR_PARSE * elem ) | |
1626 | { | |
1627 | dynamic_array_push( group->elems, elem ); | |
1628 | } | |
1629 | ||
1630 | static void var_parse_group_maybe_add_constant( VAR_PARSE_GROUP * group, | |
1631 | char const * start, char const * end ) | |
1632 | { | |
1633 | if ( start != end ) | |
1634 | { | |
1635 | string buf[ 1 ]; | |
1636 | VAR_PARSE_STRING * const value = (VAR_PARSE_STRING *)BJAM_MALLOC( | |
1637 | sizeof(VAR_PARSE_STRING) ); | |
1638 | value->base.type = VAR_PARSE_TYPE_STRING; | |
1639 | string_new( buf ); | |
1640 | string_append_range( buf, start, end ); | |
1641 | value->s = object_new( buf->value ); | |
1642 | string_free( buf ); | |
1643 | var_parse_group_add( group, (VAR_PARSE *)value ); | |
1644 | } | |
1645 | } | |
1646 | ||
1647 | VAR_PARSE_STRING * var_parse_group_as_literal( VAR_PARSE_GROUP * group ) | |
1648 | { | |
1649 | if ( group->elems->size == 1 ) | |
1650 | { | |
1651 | VAR_PARSE * result = dynamic_array_at( VAR_PARSE *, group->elems, 0 ); | |
1652 | if ( result->type == VAR_PARSE_TYPE_STRING ) | |
1653 | return (VAR_PARSE_STRING *)result; | |
1654 | } | |
1655 | return 0; | |
1656 | } | |
1657 | ||
1658 | ||
1659 | /* | |
1660 | * VAR_PARSE_ACTIONS | |
1661 | */ | |
1662 | ||
1663 | static VAR_PARSE_ACTIONS * var_parse_actions_new() | |
1664 | { | |
1665 | VAR_PARSE_ACTIONS * const result = (VAR_PARSE_ACTIONS *)BJAM_MALLOC( | |
1666 | sizeof(VAR_PARSE_ACTIONS) ); | |
1667 | dynamic_array_init( result->elems ); | |
1668 | return result; | |
1669 | } | |
1670 | ||
1671 | static void var_parse_actions_free( VAR_PARSE_ACTIONS * actions ) | |
1672 | { | |
1673 | int i; | |
1674 | for ( i = 0; i < actions->elems->size; ++i ) | |
1675 | var_parse_group_free( dynamic_array_at( VAR_PARSE_GROUP *, | |
1676 | actions->elems, i ) ); | |
1677 | dynamic_array_free( actions->elems ); | |
1678 | BJAM_FREE( actions ); | |
1679 | } | |
1680 | ||
1681 | ||
1682 | /* | |
1683 | * VAR_PARSE_VAR | |
1684 | */ | |
1685 | ||
1686 | static VAR_PARSE_VAR * var_parse_var_new() | |
1687 | { | |
1688 | VAR_PARSE_VAR * result = BJAM_MALLOC( sizeof( VAR_PARSE_VAR ) ); | |
1689 | result->base.type = VAR_PARSE_TYPE_VAR; | |
1690 | result->name = var_parse_group_new(); | |
1691 | result->subscript = 0; | |
1692 | dynamic_array_init( result->modifiers ); | |
1693 | return result; | |
1694 | } | |
1695 | ||
1696 | static void var_parse_var_free( VAR_PARSE_VAR * var ) | |
1697 | { | |
1698 | int i; | |
1699 | var_parse_group_free( var->name ); | |
1700 | if ( var->subscript ) | |
1701 | var_parse_group_free( var->subscript ); | |
1702 | for ( i = 0; i < var->modifiers->size; ++i ) | |
1703 | var_parse_group_free( dynamic_array_at( VAR_PARSE_GROUP *, | |
1704 | var->modifiers, i ) ); | |
1705 | dynamic_array_free( var->modifiers ); | |
1706 | BJAM_FREE( var ); | |
1707 | } | |
1708 | ||
1709 | static VAR_PARSE_GROUP * var_parse_var_new_modifier( VAR_PARSE_VAR * var ) | |
1710 | { | |
1711 | VAR_PARSE_GROUP * result = var_parse_group_new(); | |
1712 | dynamic_array_push( var->modifiers, result ); | |
1713 | return result; | |
1714 | } | |
1715 | ||
1716 | ||
1717 | /* | |
1718 | * VAR_PARSE_STRING | |
1719 | */ | |
1720 | ||
1721 | static void var_parse_string_free( VAR_PARSE_STRING * string ) | |
1722 | { | |
1723 | object_free( string->s ); | |
1724 | BJAM_FREE( string ); | |
1725 | } | |
1726 | ||
1727 | ||
1728 | /* | |
1729 | * VAR_PARSE_FILE | |
1730 | */ | |
1731 | ||
1732 | static VAR_PARSE_FILE * var_parse_file_new( void ) | |
1733 | { | |
1734 | VAR_PARSE_FILE * const result = (VAR_PARSE_FILE *)BJAM_MALLOC( sizeof( | |
1735 | VAR_PARSE_FILE ) ); | |
1736 | result->base.type = VAR_PARSE_TYPE_FILE; | |
1737 | dynamic_array_init( result->filename ); | |
1738 | dynamic_array_init( result->contents ); | |
1739 | return result; | |
1740 | } | |
1741 | ||
1742 | static void var_parse_file_free( VAR_PARSE_FILE * file ) | |
1743 | { | |
1744 | int i; | |
1745 | for ( i = 0; i < file->filename->size; ++i ) | |
1746 | var_parse_group_free( dynamic_array_at( VAR_PARSE_GROUP *, | |
1747 | file->filename, i ) ); | |
1748 | dynamic_array_free( file->filename ); | |
1749 | for ( i = 0; i < file->contents->size; ++i ) | |
1750 | var_parse_group_free( dynamic_array_at( VAR_PARSE_GROUP *, | |
1751 | file->contents, i ) ); | |
1752 | dynamic_array_free( file->contents ); | |
1753 | BJAM_FREE( file ); | |
1754 | } | |
1755 | ||
1756 | ||
1757 | /* | |
1758 | * VAR_PARSE | |
1759 | */ | |
1760 | ||
1761 | static void var_parse_free( VAR_PARSE * parse ) | |
1762 | { | |
1763 | switch ( parse->type ) | |
1764 | { | |
1765 | case VAR_PARSE_TYPE_VAR: | |
1766 | var_parse_var_free( (VAR_PARSE_VAR *)parse ); | |
1767 | break; | |
1768 | ||
1769 | case VAR_PARSE_TYPE_STRING: | |
1770 | var_parse_string_free( (VAR_PARSE_STRING *)parse ); | |
1771 | break; | |
1772 | ||
1773 | case VAR_PARSE_TYPE_FILE: | |
1774 | var_parse_file_free( (VAR_PARSE_FILE *)parse ); | |
1775 | break; | |
1776 | ||
1777 | default: | |
1778 | assert( !"Invalid type" ); | |
1779 | } | |
1780 | } | |
1781 | ||
1782 | ||
1783 | /* | |
1784 | * Compile VAR_PARSE | |
1785 | */ | |
1786 | ||
1787 | static void var_parse_group_compile( VAR_PARSE_GROUP const * parse, | |
1788 | compiler * c ); | |
1789 | ||
1790 | static void var_parse_var_compile( VAR_PARSE_VAR const * parse, compiler * c ) | |
1791 | { | |
1792 | int expand_name = 0; | |
1793 | int is_get_grist = 0; | |
1794 | int has_modifiers = 0; | |
1795 | /* Special case common modifiers */ | |
1796 | if ( parse->modifiers->size == 1 ) | |
1797 | { | |
1798 | VAR_PARSE_GROUP * mod = dynamic_array_at( VAR_PARSE_GROUP *, parse->modifiers, 0 ); | |
1799 | if ( mod->elems->size == 1 ) | |
1800 | { | |
1801 | VAR_PARSE * mod1 = dynamic_array_at( VAR_PARSE *, mod->elems, 0 ); | |
1802 | if ( mod1->type == VAR_PARSE_TYPE_STRING ) | |
1803 | { | |
1804 | OBJECT * s = ( (VAR_PARSE_STRING *)mod1 )->s; | |
1805 | if ( ! strcmp ( object_str( s ), "G" ) ) | |
1806 | { | |
1807 | is_get_grist = 1; | |
1808 | } | |
1809 | } | |
1810 | } | |
1811 | } | |
1812 | /* If there are modifiers, emit them in reverse order. */ | |
1813 | if ( parse->modifiers->size > 0 && !is_get_grist ) | |
1814 | { | |
1815 | int i; | |
1816 | has_modifiers = 1; | |
1817 | for ( i = 0; i < parse->modifiers->size; ++i ) | |
1818 | var_parse_group_compile( dynamic_array_at( VAR_PARSE_GROUP *, | |
1819 | parse->modifiers, parse->modifiers->size - i - 1 ), c ); | |
1820 | } | |
1821 | ||
1822 | /* If there is a subscript, emit it. */ | |
1823 | if ( parse->subscript ) | |
1824 | var_parse_group_compile( parse->subscript, c ); | |
1825 | ||
1826 | /* If the variable name is empty, look it up. */ | |
1827 | if ( parse->name->elems->size == 0 ) | |
1828 | compile_emit( c, INSTR_PUSH_VAR, compile_emit_constant( c, | |
1829 | constant_empty ) ); | |
1830 | /* If the variable name does not need to be expanded, look it up. */ | |
1831 | else if ( parse->name->elems->size == 1 && dynamic_array_at( VAR_PARSE *, | |
1832 | parse->name->elems, 0 )->type == VAR_PARSE_TYPE_STRING ) | |
1833 | { | |
1834 | OBJECT * const name = ( (VAR_PARSE_STRING *)dynamic_array_at( | |
1835 | VAR_PARSE *, parse->name->elems, 0 ) )->s; | |
1836 | int const idx = get_argument_index( object_str( name ) ); | |
1837 | if ( idx != -1 ) | |
1838 | compile_emit( c, INSTR_PUSH_ARG, idx ); | |
1839 | else | |
1840 | compile_emit( c, INSTR_PUSH_VAR, compile_emit_constant( c, name ) ); | |
1841 | } | |
1842 | /* Otherwise, push the var names and use the group instruction. */ | |
1843 | else | |
1844 | { | |
1845 | var_parse_group_compile( parse->name, c ); | |
1846 | expand_name = 1; | |
1847 | } | |
1848 | ||
1849 | /** Select the instruction for expanding the variable. */ | |
1850 | if ( !has_modifiers && !parse->subscript && !expand_name ) | |
1851 | ; | |
1852 | else if ( !has_modifiers && !parse->subscript && expand_name ) | |
1853 | compile_emit( c, INSTR_PUSH_GROUP, 0 ); | |
1854 | else if ( !has_modifiers && parse->subscript && !expand_name ) | |
1855 | compile_emit( c, INSTR_APPLY_INDEX, 0 ); | |
1856 | else if ( !has_modifiers && parse->subscript && expand_name ) | |
1857 | compile_emit( c, INSTR_APPLY_INDEX_GROUP, 0 ); | |
1858 | else if ( has_modifiers && !parse->subscript && !expand_name ) | |
1859 | compile_emit( c, INSTR_APPLY_MODIFIERS, parse->modifiers->size ); | |
1860 | else if ( has_modifiers && !parse->subscript && expand_name ) | |
1861 | compile_emit( c, INSTR_APPLY_MODIFIERS_GROUP, parse->modifiers->size ); | |
1862 | else if ( has_modifiers && parse->subscript && !expand_name ) | |
1863 | compile_emit( c, INSTR_APPLY_INDEX_MODIFIERS, parse->modifiers->size ); | |
1864 | else if ( has_modifiers && parse->subscript && expand_name ) | |
1865 | compile_emit( c, INSTR_APPLY_INDEX_MODIFIERS_GROUP, | |
1866 | parse->modifiers->size ); | |
1867 | ||
1868 | /* Now apply any special modifiers */ | |
1869 | if ( is_get_grist ) | |
1870 | { | |
1871 | compile_emit( c, INSTR_GET_GRIST, 0 ); | |
1872 | } | |
1873 | } | |
1874 | ||
1875 | static void var_parse_string_compile( VAR_PARSE_STRING const * parse, | |
1876 | compiler * c ) | |
1877 | { | |
1878 | compile_emit( c, INSTR_PUSH_CONSTANT, compile_emit_constant( c, parse->s ) | |
1879 | ); | |
1880 | } | |
1881 | ||
1882 | static void var_parse_file_compile( VAR_PARSE_FILE const * parse, compiler * c ) | |
1883 | { | |
1884 | int i; | |
1885 | for ( i = 0; i < parse->filename->size; ++i ) | |
1886 | var_parse_group_compile( dynamic_array_at( VAR_PARSE_GROUP *, | |
1887 | parse->filename, parse->filename->size - i - 1 ), c ); | |
1888 | compile_emit( c, INSTR_APPEND_STRINGS, parse->filename->size ); | |
1889 | for ( i = 0; i < parse->contents->size; ++i ) | |
1890 | var_parse_group_compile( dynamic_array_at( VAR_PARSE_GROUP *, | |
1891 | parse->contents, parse->contents->size - i - 1 ), c ); | |
1892 | compile_emit( c, INSTR_WRITE_FILE, parse->contents->size ); | |
1893 | } | |
1894 | ||
1895 | static void var_parse_compile( VAR_PARSE const * parse, compiler * c ) | |
1896 | { | |
1897 | switch ( parse->type ) | |
1898 | { | |
1899 | case VAR_PARSE_TYPE_VAR: | |
1900 | var_parse_var_compile( (VAR_PARSE_VAR const *)parse, c ); | |
1901 | break; | |
1902 | ||
1903 | case VAR_PARSE_TYPE_STRING: | |
1904 | var_parse_string_compile( (VAR_PARSE_STRING const *)parse, c ); | |
1905 | break; | |
1906 | ||
1907 | case VAR_PARSE_TYPE_FILE: | |
1908 | var_parse_file_compile( (VAR_PARSE_FILE const *)parse, c ); | |
1909 | break; | |
1910 | ||
1911 | default: | |
1912 | assert( !"Unknown var parse type." ); | |
1913 | } | |
1914 | } | |
1915 | ||
1916 | static void var_parse_group_compile( VAR_PARSE_GROUP const * parse, compiler * c | |
1917 | ) | |
1918 | { | |
1919 | /* Emit the elements in reverse order. */ | |
1920 | int i; | |
1921 | for ( i = 0; i < parse->elems->size; ++i ) | |
1922 | var_parse_compile( dynamic_array_at( VAR_PARSE *, parse->elems, | |
1923 | parse->elems->size - i - 1 ), c ); | |
1924 | /* If there are no elements, emit an empty string. */ | |
1925 | if ( parse->elems->size == 0 ) | |
1926 | compile_emit( c, INSTR_PUSH_CONSTANT, compile_emit_constant( c, | |
1927 | constant_empty ) ); | |
1928 | /* If there is more than one element, combine them. */ | |
1929 | if ( parse->elems->size > 1 ) | |
1930 | compile_emit( c, INSTR_COMBINE_STRINGS, parse->elems->size ); | |
1931 | } | |
1932 | ||
1933 | static void var_parse_actions_compile( VAR_PARSE_ACTIONS const * actions, | |
1934 | compiler * c ) | |
1935 | { | |
1936 | int i; | |
1937 | for ( i = 0; i < actions->elems->size; ++i ) | |
1938 | var_parse_group_compile( dynamic_array_at( VAR_PARSE_GROUP *, | |
1939 | actions->elems, actions->elems->size - i - 1 ), c ); | |
1940 | compile_emit( c, INSTR_OUTPUT_STRINGS, actions->elems->size ); | |
1941 | } | |
1942 | ||
1943 | ||
1944 | /* | |
1945 | * Parse VAR_PARSE_VAR | |
1946 | */ | |
1947 | ||
1948 | static VAR_PARSE * parse_at_file( char const * start, char const * mid, | |
1949 | char const * end ); | |
1950 | static VAR_PARSE * parse_variable( char const * * string ); | |
1951 | static int try_parse_variable( char const * * s_, char const * * string, | |
1952 | VAR_PARSE_GROUP * out ); | |
1953 | static void balance_parentheses( char const * * s_, char const * * string, | |
1954 | VAR_PARSE_GROUP * out ); | |
1955 | static void parse_var_string( char const * first, char const * last, | |
1956 | struct dynamic_array * out ); | |
1957 | ||
1958 | ||
1959 | /* | |
1960 | * Parses a string that can contain variables to expand. | |
1961 | */ | |
1962 | ||
1963 | static VAR_PARSE_GROUP * parse_expansion( char const * * string ) | |
1964 | { | |
1965 | VAR_PARSE_GROUP * result = var_parse_group_new(); | |
1966 | char const * s = *string; | |
1967 | for ( ; ; ) | |
1968 | { | |
1969 | if ( try_parse_variable( &s, string, result ) ) {} | |
1970 | else if ( s[ 0 ] == '\0' ) | |
1971 | { | |
1972 | var_parse_group_maybe_add_constant( result, *string, s ); | |
1973 | return result; | |
1974 | } | |
1975 | else | |
1976 | ++s; | |
1977 | } | |
1978 | } | |
1979 | ||
1980 | static VAR_PARSE_ACTIONS * parse_actions( char const * string ) | |
1981 | { | |
1982 | VAR_PARSE_ACTIONS * const result = var_parse_actions_new(); | |
1983 | parse_var_string( string, string + strlen( string ), result->elems ); | |
1984 | return result; | |
1985 | } | |
1986 | ||
1987 | /* | |
1988 | * Checks whether the string a *s_ starts with a variable expansion "$(". | |
1989 | * *string should point to the first unemitted character before *s. If *s_ | |
1990 | * starts with variable expansion, appends elements to out up to the closing | |
1991 | * ")", and adjusts *s_ and *string to point to next character. Returns 1 if s_ | |
1992 | * starts with a variable, 0 otherwise. | |
1993 | */ | |
1994 | ||
1995 | static int try_parse_variable( char const * * s_, char const * * string, | |
1996 | VAR_PARSE_GROUP * out ) | |
1997 | { | |
1998 | char const * s = *s_; | |
1999 | if ( s[ 0 ] == '$' && s[ 1 ] == '(' ) | |
2000 | { | |
2001 | var_parse_group_maybe_add_constant( out, *string, s ); | |
2002 | s += 2; | |
2003 | var_parse_group_add( out, parse_variable( &s ) ); | |
2004 | *string = s; | |
2005 | *s_ = s; | |
2006 | return 1; | |
2007 | } | |
2008 | if ( s[ 0 ] == '@' && s[ 1 ] == '(' ) | |
2009 | { | |
2010 | int depth = 1; | |
2011 | char const * ine; | |
2012 | char const * split = 0; | |
2013 | var_parse_group_maybe_add_constant( out, *string, s ); | |
2014 | s += 2; | |
2015 | ine = s; | |
2016 | ||
2017 | /* Scan the content of the response file @() section. */ | |
2018 | while ( *ine && ( depth > 0 ) ) | |
2019 | { | |
2020 | switch ( *ine ) | |
2021 | { | |
2022 | case '(': ++depth; break; | |
2023 | case ')': --depth; break; | |
2024 | case ':': | |
2025 | if ( ( depth == 1 ) && ( ine[ 1 ] == 'E' ) && ( ine[ 2 ] == '=' | |
2026 | ) ) | |
2027 | split = ine; | |
2028 | break; | |
2029 | } | |
2030 | ++ine; | |
2031 | } | |
2032 | ||
2033 | if ( !split || depth ) | |
2034 | return 0; | |
2035 | ||
2036 | var_parse_group_add( out, parse_at_file( s, split, ine - 1 ) ); | |
2037 | *string = ine; | |
2038 | *s_ = ine; | |
2039 | return 1; | |
2040 | } | |
2041 | return 0; | |
2042 | } | |
2043 | ||
2044 | ||
2045 | static char const * current_file = ""; | |
2046 | static int current_line; | |
2047 | ||
2048 | static void parse_error( char const * message ) | |
2049 | { | |
2050 | out_printf( "%s:%d: %s\n", current_file, current_line, message ); | |
2051 | } | |
2052 | ||
2053 | ||
2054 | /* | |
2055 | * Parses a single variable up to the closing ")" and adjusts *string to point | |
2056 | * to the next character. *string should point to the character immediately | |
2057 | * after the initial "$(". | |
2058 | */ | |
2059 | ||
2060 | static VAR_PARSE * parse_variable( char const * * string ) | |
2061 | { | |
2062 | VAR_PARSE_VAR * const result = var_parse_var_new(); | |
2063 | VAR_PARSE_GROUP * const name = result->name; | |
2064 | char const * s = *string; | |
2065 | for ( ; ; ) | |
2066 | { | |
2067 | if ( try_parse_variable( &s, string, name ) ) {} | |
2068 | else if ( s[ 0 ] == ':' ) | |
2069 | { | |
2070 | VAR_PARSE_GROUP * mod; | |
2071 | var_parse_group_maybe_add_constant( name, *string, s ); | |
2072 | ++s; | |
2073 | *string = s; | |
2074 | mod = var_parse_var_new_modifier( result ); | |
2075 | for ( ; ; ) | |
2076 | { | |
2077 | if ( try_parse_variable( &s, string, mod ) ) {} | |
2078 | else if ( s[ 0 ] == ')' ) | |
2079 | { | |
2080 | var_parse_group_maybe_add_constant( mod, *string, s ); | |
2081 | *string = ++s; | |
2082 | return (VAR_PARSE *)result; | |
2083 | } | |
2084 | else if ( s[ 0 ] == '(' ) | |
2085 | { | |
2086 | ++s; | |
2087 | balance_parentheses( &s, string, mod ); | |
2088 | } | |
2089 | else if ( s[ 0 ] == ':' ) | |
2090 | { | |
2091 | var_parse_group_maybe_add_constant( mod, *string, s ); | |
2092 | *string = ++s; | |
2093 | mod = var_parse_var_new_modifier( result ); | |
2094 | } | |
2095 | else if ( s[ 0 ] == '[' ) | |
2096 | { | |
2097 | parse_error("unexpected subscript"); | |
2098 | ++s; | |
2099 | } | |
2100 | else if ( s[ 0 ] == '\0' ) | |
2101 | { | |
2102 | parse_error( "unbalanced parentheses" ); | |
2103 | var_parse_group_maybe_add_constant( mod, *string, s ); | |
2104 | *string = s; | |
2105 | return (VAR_PARSE *)result; | |
2106 | } | |
2107 | else | |
2108 | ++s; | |
2109 | } | |
2110 | } | |
2111 | else if ( s[ 0 ] == '[' ) | |
2112 | { | |
2113 | VAR_PARSE_GROUP * subscript = var_parse_group_new(); | |
2114 | result->subscript = subscript; | |
2115 | var_parse_group_maybe_add_constant( name, *string, s ); | |
2116 | *string = ++s; | |
2117 | for ( ; ; ) | |
2118 | { | |
2119 | if ( try_parse_variable( &s, string, subscript ) ) {} | |
2120 | else if ( s[ 0 ] == ']' ) | |
2121 | { | |
2122 | var_parse_group_maybe_add_constant( subscript, *string, s ); | |
2123 | *string = ++s; | |
2124 | if ( s[ 0 ] != ')' && s[ 0 ] != ':' && s[ 0 ] != '\0' ) | |
2125 | parse_error( "unexpected text following []" ); | |
2126 | break; | |
2127 | } | |
2128 | else if ( isdigit( s[ 0 ] ) || s[ 0 ] == '-' ) | |
2129 | { | |
2130 | ++s; | |
2131 | } | |
2132 | else if ( s[ 0 ] == '\0' ) | |
2133 | { | |
2134 | parse_error( "malformed subscript" ); | |
2135 | break; | |
2136 | } | |
2137 | else | |
2138 | { | |
2139 | parse_error( "malformed subscript" ); | |
2140 | ++s; | |
2141 | } | |
2142 | } | |
2143 | } | |
2144 | else if ( s[ 0 ] == ')' ) | |
2145 | { | |
2146 | var_parse_group_maybe_add_constant( name, *string, s ); | |
2147 | *string = ++s; | |
2148 | return (VAR_PARSE *)result; | |
2149 | } | |
2150 | else if ( s[ 0 ] == '(' ) | |
2151 | { | |
2152 | ++s; | |
2153 | balance_parentheses( &s, string, name ); | |
2154 | } | |
2155 | else if ( s[ 0 ] == '\0' ) | |
2156 | { | |
2157 | parse_error( "unbalanced parentheses" ); | |
2158 | var_parse_group_maybe_add_constant( name, *string, s ); | |
2159 | *string = s; | |
2160 | return (VAR_PARSE *)result; | |
2161 | } | |
2162 | else | |
2163 | ++s; | |
2164 | } | |
2165 | } | |
2166 | ||
2167 | static void parse_var_string( char const * first, char const * last, | |
2168 | struct dynamic_array * out ) | |
2169 | { | |
2170 | char const * saved = first; | |
2171 | while ( first != last ) | |
2172 | { | |
2173 | /* Handle whitespace. */ | |
2174 | while ( first != last && isspace( *first ) ) ++first; | |
2175 | if ( saved != first ) | |
2176 | { | |
2177 | VAR_PARSE_GROUP * const group = var_parse_group_new(); | |
2178 | var_parse_group_maybe_add_constant( group, saved, first ); | |
2179 | saved = first; | |
2180 | dynamic_array_push( out, group ); | |
2181 | } | |
2182 | if ( first == last ) break; | |
2183 | ||
2184 | /* Handle non-whitespace */ | |
2185 | { | |
2186 | VAR_PARSE_GROUP * group = var_parse_group_new(); | |
2187 | for ( ; ; ) | |
2188 | { | |
2189 | if ( first == last || isspace( *first ) ) | |
2190 | { | |
2191 | var_parse_group_maybe_add_constant( group, saved, first ); | |
2192 | saved = first; | |
2193 | break; | |
2194 | } | |
2195 | if ( try_parse_variable( &first, &saved, group ) ) | |
2196 | assert( first <= last ); | |
2197 | else | |
2198 | ++first; | |
2199 | } | |
2200 | dynamic_array_push( out, group ); | |
2201 | } | |
2202 | } | |
2203 | } | |
2204 | ||
2205 | /* | |
2206 | * start should point to the character immediately following the opening "@(", | |
2207 | * mid should point to the ":E=", and end should point to the closing ")". | |
2208 | */ | |
2209 | ||
2210 | static VAR_PARSE * parse_at_file( char const * start, char const * mid, | |
2211 | char const * end ) | |
2212 | { | |
2213 | VAR_PARSE_FILE * result = var_parse_file_new(); | |
2214 | parse_var_string( start, mid, result->filename ); | |
2215 | parse_var_string( mid + 3, end, result->contents ); | |
2216 | return (VAR_PARSE *)result; | |
2217 | } | |
2218 | ||
2219 | /* | |
2220 | * Given that *s_ points to the character after a "(", parses up to the matching | |
2221 | * ")". *string should point to the first unemitted character before *s_. | |
2222 | * | |
2223 | * When the function returns, *s_ will point to the character after the ")", and | |
2224 | * *string will point to the first unemitted character before *s_. The range | |
2225 | * from *string to *s_ does not contain any variables that need to be expanded. | |
2226 | */ | |
2227 | ||
2228 | void balance_parentheses( char const * * s_, char const * * string, | |
2229 | VAR_PARSE_GROUP * out) | |
2230 | { | |
2231 | int depth = 1; | |
2232 | char const * s = *s_; | |
2233 | for ( ; ; ) | |
2234 | { | |
2235 | if ( try_parse_variable( &s, string, out ) ) { } | |
2236 | else if ( s[ 0 ] == ':' || s[ 0 ] == '[' ) | |
2237 | { | |
2238 | parse_error( "unbalanced parentheses" ); | |
2239 | ++s; | |
2240 | } | |
2241 | else if ( s[ 0 ] == '\0' ) | |
2242 | { | |
2243 | parse_error( "unbalanced parentheses" ); | |
2244 | break; | |
2245 | } | |
2246 | else if ( s[ 0 ] == ')' ) | |
2247 | { | |
2248 | ++s; | |
2249 | if ( --depth == 0 ) break; | |
2250 | } | |
2251 | else if ( s[ 0 ] == '(' ) | |
2252 | { | |
2253 | ++depth; | |
2254 | ++s; | |
2255 | } | |
2256 | else | |
2257 | ++s; | |
2258 | } | |
2259 | *s_ = s; | |
2260 | } | |
2261 | ||
2262 | ||
2263 | /* | |
2264 | * Main compile. | |
2265 | */ | |
2266 | ||
2267 | #define RESULT_STACK 0 | |
2268 | #define RESULT_RETURN 1 | |
2269 | #define RESULT_NONE 2 | |
2270 | ||
2271 | static void compile_parse( PARSE * parse, compiler * c, int result_location ); | |
2272 | static struct arg_list * arg_list_compile( PARSE * parse, int * num_arguments ); | |
2273 | ||
2274 | static void compile_condition( PARSE * parse, compiler * c, int branch_true, int label ) | |
2275 | { | |
2276 | assert( parse->type == PARSE_EVAL ); | |
2277 | switch ( parse->num ) | |
2278 | { | |
2279 | case EXPR_EXISTS: | |
2280 | compile_parse( parse->left, c, RESULT_STACK ); | |
2281 | if ( branch_true ) | |
2282 | compile_emit_branch( c, INSTR_JUMP_NOT_EMPTY, label ); | |
2283 | else | |
2284 | compile_emit_branch( c, INSTR_JUMP_EMPTY, label ); | |
2285 | break; | |
2286 | ||
2287 | case EXPR_EQUALS: | |
2288 | compile_parse( parse->left, c, RESULT_STACK ); | |
2289 | compile_parse( parse->right, c, RESULT_STACK ); | |
2290 | if ( branch_true ) | |
2291 | compile_emit_branch( c, INSTR_JUMP_EQ, label ); | |
2292 | else | |
2293 | compile_emit_branch( c, INSTR_JUMP_NE, label ); | |
2294 | break; | |
2295 | ||
2296 | case EXPR_NOTEQ: | |
2297 | compile_parse( parse->left, c, RESULT_STACK ); | |
2298 | compile_parse( parse->right, c, RESULT_STACK ); | |
2299 | if ( branch_true ) | |
2300 | compile_emit_branch( c, INSTR_JUMP_NE, label ); | |
2301 | else | |
2302 | compile_emit_branch( c, INSTR_JUMP_EQ, label ); | |
2303 | break; | |
2304 | ||
2305 | case EXPR_LESS: | |
2306 | compile_parse( parse->left, c, RESULT_STACK ); | |
2307 | compile_parse( parse->right, c, RESULT_STACK ); | |
2308 | if ( branch_true ) | |
2309 | compile_emit_branch( c, INSTR_JUMP_LT, label ); | |
2310 | else | |
2311 | compile_emit_branch( c, INSTR_JUMP_GE, label ); | |
2312 | break; | |
2313 | ||
2314 | case EXPR_LESSEQ: | |
2315 | compile_parse( parse->left, c, RESULT_STACK ); | |
2316 | compile_parse( parse->right, c, RESULT_STACK ); | |
2317 | if ( branch_true ) | |
2318 | compile_emit_branch( c, INSTR_JUMP_LE, label ); | |
2319 | else | |
2320 | compile_emit_branch( c, INSTR_JUMP_GT, label ); | |
2321 | break; | |
2322 | ||
2323 | case EXPR_MORE: | |
2324 | compile_parse( parse->left, c, RESULT_STACK ); | |
2325 | compile_parse( parse->right, c, RESULT_STACK ); | |
2326 | if ( branch_true ) | |
2327 | compile_emit_branch( c, INSTR_JUMP_GT, label ); | |
2328 | else | |
2329 | compile_emit_branch( c, INSTR_JUMP_LE, label ); | |
2330 | break; | |
2331 | ||
2332 | case EXPR_MOREEQ: | |
2333 | compile_parse( parse->left, c, RESULT_STACK ); | |
2334 | compile_parse( parse->right, c, RESULT_STACK ); | |
2335 | if ( branch_true ) | |
2336 | compile_emit_branch( c, INSTR_JUMP_GE, label ); | |
2337 | else | |
2338 | compile_emit_branch( c, INSTR_JUMP_LT, label ); | |
2339 | break; | |
2340 | ||
2341 | case EXPR_IN: | |
2342 | compile_parse( parse->left, c, RESULT_STACK ); | |
2343 | compile_parse( parse->right, c, RESULT_STACK ); | |
2344 | if ( branch_true ) | |
2345 | compile_emit_branch( c, INSTR_JUMP_IN, label ); | |
2346 | else | |
2347 | compile_emit_branch( c, INSTR_JUMP_NOT_IN, label ); | |
2348 | break; | |
2349 | ||
2350 | case EXPR_AND: | |
2351 | if ( branch_true ) | |
2352 | { | |
2353 | int f = compile_new_label( c ); | |
2354 | compile_condition( parse->left, c, 0, f ); | |
2355 | compile_condition( parse->right, c, 1, label ); | |
2356 | compile_set_label( c, f ); | |
2357 | } | |
2358 | else | |
2359 | { | |
2360 | compile_condition( parse->left, c, 0, label ); | |
2361 | compile_condition( parse->right, c, 0, label ); | |
2362 | } | |
2363 | break; | |
2364 | ||
2365 | case EXPR_OR: | |
2366 | if ( branch_true ) | |
2367 | { | |
2368 | compile_condition( parse->left, c, 1, label ); | |
2369 | compile_condition( parse->right, c, 1, label ); | |
2370 | } | |
2371 | else | |
2372 | { | |
2373 | int t = compile_new_label( c ); | |
2374 | compile_condition( parse->left, c, 1, t ); | |
2375 | compile_condition( parse->right, c, 0, label ); | |
2376 | compile_set_label( c, t ); | |
2377 | } | |
2378 | break; | |
2379 | ||
2380 | case EXPR_NOT: | |
2381 | compile_condition( parse->left, c, !branch_true, label ); | |
2382 | break; | |
2383 | } | |
2384 | } | |
2385 | ||
2386 | static void adjust_result( compiler * c, int actual_location, | |
2387 | int desired_location ) | |
2388 | { | |
2389 | if ( actual_location == desired_location ) | |
2390 | ; | |
2391 | else if ( actual_location == RESULT_STACK && desired_location == RESULT_RETURN ) | |
2392 | compile_emit( c, INSTR_SET_RESULT, 0 ); | |
2393 | else if ( actual_location == RESULT_STACK && desired_location == RESULT_NONE ) | |
2394 | compile_emit( c, INSTR_POP, 0 ); | |
2395 | else if ( actual_location == RESULT_RETURN && desired_location == RESULT_STACK ) | |
2396 | compile_emit( c, INSTR_PUSH_RESULT, 0 ); | |
2397 | else if ( actual_location == RESULT_RETURN && desired_location == RESULT_NONE ) | |
2398 | ; | |
2399 | else if ( actual_location == RESULT_NONE && desired_location == RESULT_STACK ) | |
2400 | compile_emit( c, INSTR_PUSH_EMPTY, 0 ); | |
2401 | else if ( actual_location == RESULT_NONE && desired_location == RESULT_RETURN ) | |
2402 | { | |
2403 | compile_emit( c, INSTR_PUSH_EMPTY, 0 ); | |
2404 | compile_emit( c, INSTR_SET_RESULT, 0 ); | |
2405 | } | |
2406 | else | |
2407 | assert( !"invalid result location" ); | |
2408 | } | |
2409 | ||
2410 | static char const * parse_type( PARSE * parse ) | |
2411 | { | |
2412 | switch ( parse->type ) | |
2413 | { | |
2414 | case PARSE_APPEND: return "append"; | |
2415 | case PARSE_EVAL: return "eval"; | |
2416 | case PARSE_RULES: return "rules"; | |
2417 | default: return "unknown"; | |
2418 | } | |
2419 | } | |
2420 | ||
2421 | static void compile_append_chain( PARSE * parse, compiler * c ) | |
2422 | { | |
2423 | assert( parse->type == PARSE_APPEND ); | |
2424 | if ( parse->left->type == PARSE_NULL ) | |
2425 | compile_parse( parse->right, c, RESULT_STACK ); | |
2426 | else | |
2427 | { | |
2428 | if ( parse->left->type == PARSE_APPEND ) | |
2429 | compile_append_chain( parse->left, c ); | |
2430 | else | |
2431 | compile_parse( parse->left, c, RESULT_STACK ); | |
2432 | compile_parse( parse->right, c, RESULT_STACK ); | |
2433 | compile_emit( c, INSTR_PUSH_APPEND, 0 ); | |
2434 | } | |
2435 | } | |
2436 | ||
2437 | static void compile_parse( PARSE * parse, compiler * c, int result_location ) | |
2438 | { | |
b32b8144 FG |
2439 | #ifdef JAM_DEBUGGER |
2440 | if ( debug_is_debugging() ) | |
2441 | compile_emit( c, INSTR_DEBUG_LINE, parse->line ); | |
2442 | #endif | |
7c673cae FG |
2443 | if ( parse->type == PARSE_APPEND ) |
2444 | { | |
2445 | compile_append_chain( parse, c ); | |
2446 | adjust_result( c, RESULT_STACK, result_location ); | |
2447 | } | |
2448 | else if ( parse->type == PARSE_EVAL ) | |
2449 | { | |
2450 | /* FIXME: This is only needed because of the bizarre parsing of | |
2451 | * conditions. | |
2452 | */ | |
2453 | if ( parse->num == EXPR_EXISTS ) | |
2454 | compile_parse( parse->left, c, result_location ); | |
2455 | else | |
2456 | { | |
2457 | int f = compile_new_label( c ); | |
2458 | int end = compile_new_label( c ); | |
2459 | ||
2460 | out_printf( "%s:%d: Conditional used as list (check operator " | |
2461 | "precedence).\n", object_str( parse->file ), parse->line ); | |
2462 | ||
2463 | /* Emit the condition */ | |
2464 | compile_condition( parse, c, 0, f ); | |
2465 | compile_emit( c, INSTR_PUSH_CONSTANT, compile_emit_constant( c, | |
2466 | constant_true ) ); | |
2467 | compile_emit_branch( c, INSTR_JUMP, end ); | |
2468 | compile_set_label( c, f ); | |
2469 | compile_emit( c, INSTR_PUSH_EMPTY, 0 ); | |
2470 | compile_set_label( c, end ); | |
2471 | adjust_result( c, RESULT_STACK, result_location ); | |
2472 | } | |
2473 | } | |
2474 | else if ( parse->type == PARSE_FOREACH ) | |
2475 | { | |
2476 | int var = compile_emit_constant( c, parse->string ); | |
2477 | int top = compile_new_label( c ); | |
2478 | int end = compile_new_label( c ); | |
2479 | int continue_ = compile_new_label( c ); | |
2480 | ||
2481 | /* | |
2482 | * Evaluate the list. | |
2483 | */ | |
2484 | compile_parse( parse->left, c, RESULT_STACK ); | |
2485 | ||
2486 | /* Localize the loop variable */ | |
2487 | if ( parse->num ) | |
2488 | { | |
2489 | compile_emit( c, INSTR_PUSH_EMPTY, 0 ); | |
2490 | compile_emit( c, INSTR_PUSH_LOCAL, var ); | |
2491 | compile_emit( c, INSTR_SWAP, 1 ); | |
2492 | compile_push_cleanup( c, INSTR_POP_LOCAL, var ); | |
2493 | } | |
2494 | ||
2495 | compile_emit( c, INSTR_FOR_INIT, 0 ); | |
2496 | compile_set_label( c, top ); | |
2497 | compile_emit_branch( c, INSTR_FOR_LOOP, end ); | |
2498 | compile_emit( c, INSTR_SET, var ); | |
2499 | ||
2500 | compile_push_break_scope( c, end ); | |
2501 | compile_push_cleanup( c, INSTR_FOR_POP, 0 ); | |
2502 | compile_push_continue_scope( c, continue_ ); | |
2503 | ||
2504 | /* Run the loop body */ | |
2505 | compile_parse( parse->right, c, RESULT_NONE ); | |
2506 | ||
2507 | compile_pop_continue_scope( c ); | |
2508 | compile_pop_cleanup( c ); | |
2509 | compile_pop_break_scope( c ); | |
2510 | ||
2511 | compile_set_label( c, continue_ ); | |
2512 | compile_emit_branch( c, INSTR_JUMP, top ); | |
2513 | compile_set_label( c, end ); | |
2514 | ||
2515 | if ( parse->num ) | |
2516 | { | |
2517 | compile_pop_cleanup( c ); | |
2518 | compile_emit( c, INSTR_POP_LOCAL, var ); | |
2519 | } | |
2520 | ||
2521 | adjust_result( c, RESULT_NONE, result_location); | |
2522 | } | |
2523 | else if ( parse->type == PARSE_IF ) | |
2524 | { | |
2525 | int f = compile_new_label( c ); | |
2526 | /* Emit the condition */ | |
2527 | compile_condition( parse->left, c, 0, f ); | |
2528 | /* Emit the if block */ | |
2529 | compile_parse( parse->right, c, result_location ); | |
2530 | if ( parse->third->type != PARSE_NULL || result_location != RESULT_NONE ) | |
2531 | { | |
2532 | /* Emit the else block */ | |
2533 | int end = compile_new_label( c ); | |
2534 | compile_emit_branch( c, INSTR_JUMP, end ); | |
2535 | compile_set_label( c, f ); | |
2536 | compile_parse( parse->third, c, result_location ); | |
2537 | compile_set_label( c, end ); | |
2538 | } | |
2539 | else | |
2540 | compile_set_label( c, f ); | |
2541 | ||
2542 | } | |
2543 | else if ( parse->type == PARSE_WHILE ) | |
2544 | { | |
2545 | int nested_result = result_location == RESULT_NONE | |
2546 | ? RESULT_NONE | |
2547 | : RESULT_RETURN; | |
2548 | int test = compile_new_label( c ); | |
2549 | int top = compile_new_label( c ); | |
2550 | int end = compile_new_label( c ); | |
2551 | /* Make sure that we return an empty list if the loop runs zero times. | |
2552 | */ | |
2553 | adjust_result( c, RESULT_NONE, nested_result ); | |
2554 | /* Jump to the loop test. */ | |
2555 | compile_emit_branch( c, INSTR_JUMP, test ); | |
2556 | compile_set_label( c, top ); | |
2557 | /* Emit the loop body. */ | |
2558 | compile_push_break_scope( c, end ); | |
2559 | compile_push_continue_scope( c, test ); | |
2560 | compile_parse( parse->right, c, nested_result ); | |
2561 | compile_pop_continue_scope( c ); | |
2562 | compile_pop_break_scope( c ); | |
2563 | /* Emit the condition. */ | |
2564 | compile_set_label( c, test ); | |
2565 | compile_condition( parse->left, c, 1, top ); | |
2566 | compile_set_label( c, end ); | |
2567 | ||
2568 | adjust_result( c, nested_result, result_location ); | |
2569 | } | |
2570 | else if ( parse->type == PARSE_INCLUDE ) | |
2571 | { | |
2572 | compile_parse( parse->left, c, RESULT_STACK ); | |
2573 | compile_emit( c, INSTR_INCLUDE, 0 ); | |
2574 | compile_emit( c, INSTR_BIND_MODULE_VARIABLES, 0 ); | |
2575 | adjust_result( c, RESULT_NONE, result_location ); | |
2576 | } | |
2577 | else if ( parse->type == PARSE_MODULE ) | |
2578 | { | |
2579 | int const nested_result = result_location == RESULT_NONE | |
2580 | ? RESULT_NONE | |
2581 | : RESULT_RETURN; | |
2582 | compile_parse( parse->left, c, RESULT_STACK ); | |
2583 | compile_emit( c, INSTR_PUSH_MODULE, 0 ); | |
2584 | compile_push_cleanup( c, INSTR_POP_MODULE, 0 ); | |
2585 | compile_parse( parse->right, c, nested_result ); | |
2586 | compile_pop_cleanup( c ); | |
2587 | compile_emit( c, INSTR_POP_MODULE, 0 ); | |
2588 | adjust_result( c, nested_result, result_location ); | |
2589 | } | |
2590 | else if ( parse->type == PARSE_CLASS ) | |
2591 | { | |
2592 | /* Evaluate the class name. */ | |
2593 | compile_parse( parse->left->right, c, RESULT_STACK ); | |
2594 | /* Evaluate the base classes. */ | |
2595 | if ( parse->left->left ) | |
2596 | compile_parse( parse->left->left->right, c, RESULT_STACK ); | |
2597 | else | |
2598 | compile_emit( c, INSTR_PUSH_EMPTY, 0 ); | |
2599 | compile_emit( c, INSTR_CLASS, 0 ); | |
2600 | compile_push_cleanup( c, INSTR_POP_MODULE, 0 ); | |
2601 | compile_parse( parse->right, c, RESULT_NONE ); | |
2602 | compile_emit( c, INSTR_BIND_MODULE_VARIABLES, 0 ); | |
2603 | compile_pop_cleanup( c ); | |
2604 | compile_emit( c, INSTR_POP_MODULE, 0 ); | |
2605 | ||
2606 | adjust_result( c, RESULT_NONE, result_location ); | |
2607 | } | |
2608 | else if ( parse->type == PARSE_LIST ) | |
2609 | { | |
2610 | OBJECT * const o = parse->string; | |
2611 | char const * s = object_str( o ); | |
2612 | VAR_PARSE_GROUP * group; | |
2613 | current_file = object_str( parse->file ); | |
2614 | current_line = parse->line; | |
2615 | group = parse_expansion( &s ); | |
2616 | var_parse_group_compile( group, c ); | |
2617 | var_parse_group_free( group ); | |
2618 | adjust_result( c, RESULT_STACK, result_location ); | |
2619 | } | |
2620 | else if ( parse->type == PARSE_LOCAL ) | |
2621 | { | |
2622 | int nested_result = result_location == RESULT_NONE | |
2623 | ? RESULT_NONE | |
2624 | : RESULT_RETURN; | |
2625 | /* This should be left recursive group of compile_appends. */ | |
2626 | PARSE * vars = parse->left; | |
2627 | ||
2628 | /* Special case an empty list of vars */ | |
2629 | if ( vars->type == PARSE_NULL ) | |
2630 | { | |
2631 | compile_parse( parse->right, c, RESULT_NONE ); | |
2632 | compile_parse( parse->third, c, result_location ); | |
2633 | nested_result = result_location; | |
2634 | } | |
2635 | /* Check whether there is exactly one variable with a constant name. */ | |
2636 | else if ( vars->left->type == PARSE_NULL && | |
2637 | vars->right->type == PARSE_LIST ) | |
2638 | { | |
2639 | char const * s = object_str( vars->right->string ); | |
2640 | VAR_PARSE_GROUP * group; | |
2641 | current_file = object_str( parse->file ); | |
2642 | current_line = parse->line; | |
2643 | group = parse_expansion( &s ); | |
2644 | if ( group->elems->size == 1 && dynamic_array_at( VAR_PARSE *, | |
2645 | group->elems, 0 )->type == VAR_PARSE_TYPE_STRING ) | |
2646 | { | |
2647 | int const name = compile_emit_constant( c, ( | |
2648 | (VAR_PARSE_STRING *)dynamic_array_at( VAR_PARSE *, | |
2649 | group->elems, 0 ) )->s ); | |
2650 | var_parse_group_free( group ); | |
2651 | compile_parse( parse->right, c, RESULT_STACK ); | |
2652 | compile_emit( c, INSTR_PUSH_LOCAL, name ); | |
2653 | compile_push_cleanup( c, INSTR_POP_LOCAL, name ); | |
2654 | compile_parse( parse->third, c, nested_result ); | |
2655 | compile_pop_cleanup( c ); | |
2656 | compile_emit( c, INSTR_POP_LOCAL, name ); | |
2657 | } | |
2658 | else | |
2659 | { | |
2660 | var_parse_group_compile( group, c ); | |
2661 | var_parse_group_free( group ); | |
2662 | compile_parse( parse->right, c, RESULT_STACK ); | |
2663 | compile_emit( c, INSTR_PUSH_LOCAL_GROUP, 0 ); | |
2664 | compile_push_cleanup( c, INSTR_POP_LOCAL_GROUP, 0 ); | |
2665 | compile_parse( parse->third, c, nested_result ); | |
2666 | compile_pop_cleanup( c ); | |
2667 | compile_emit( c, INSTR_POP_LOCAL_GROUP, 0 ); | |
2668 | } | |
2669 | } | |
2670 | else | |
2671 | { | |
2672 | compile_parse( parse->left, c, RESULT_STACK ); | |
2673 | compile_parse( parse->right, c, RESULT_STACK ); | |
2674 | compile_emit( c, INSTR_PUSH_LOCAL_GROUP, 0 ); | |
2675 | compile_push_cleanup( c, INSTR_POP_LOCAL_GROUP, 0 ); | |
2676 | compile_parse( parse->third, c, nested_result ); | |
2677 | compile_pop_cleanup( c ); | |
2678 | compile_emit( c, INSTR_POP_LOCAL_GROUP, 0 ); | |
2679 | } | |
2680 | adjust_result( c, nested_result, result_location ); | |
2681 | } | |
2682 | else if ( parse->type == PARSE_ON ) | |
2683 | { | |
2684 | if ( parse->right->type == PARSE_APPEND && | |
2685 | parse->right->left->type == PARSE_NULL && | |
2686 | parse->right->right->type == PARSE_LIST ) | |
2687 | { | |
2688 | /* [ on $(target) return $(variable) ] */ | |
2689 | PARSE * value = parse->right->right; | |
2690 | OBJECT * const o = value->string; | |
2691 | char const * s = object_str( o ); | |
2692 | VAR_PARSE_GROUP * group; | |
2693 | OBJECT * varname = 0; | |
2694 | current_file = object_str( value->file ); | |
2695 | current_line = value->line; | |
2696 | group = parse_expansion( &s ); | |
2697 | if ( group->elems->size == 1 ) | |
2698 | { | |
2699 | VAR_PARSE * one = dynamic_array_at( VAR_PARSE *, group->elems, 0 ); | |
2700 | if ( one->type == VAR_PARSE_TYPE_VAR ) | |
2701 | { | |
2702 | VAR_PARSE_VAR * var = ( VAR_PARSE_VAR * )one; | |
2703 | if ( var->modifiers->size == 0 && !var->subscript && var->name->elems->size == 1 ) | |
2704 | { | |
2705 | VAR_PARSE * name = dynamic_array_at( VAR_PARSE *, var->name->elems, 0 ); | |
2706 | if ( name->type == VAR_PARSE_TYPE_STRING ) | |
2707 | { | |
2708 | varname = ( ( VAR_PARSE_STRING * )name )->s; | |
2709 | } | |
2710 | } | |
2711 | } | |
2712 | } | |
2713 | if ( varname ) | |
2714 | { | |
2715 | /* We have one variable with a fixed name and no modifiers. */ | |
2716 | compile_parse( parse->left, c, RESULT_STACK ); | |
2717 | compile_emit( c, INSTR_GET_ON, compile_emit_constant( c, varname ) ); | |
2718 | } | |
2719 | else | |
2720 | { | |
2721 | /* Too complex. Fall back on push/pop. */ | |
2722 | int end = compile_new_label( c ); | |
2723 | compile_parse( parse->left, c, RESULT_STACK ); | |
2724 | compile_emit_branch( c, INSTR_PUSH_ON, end ); | |
2725 | compile_push_cleanup( c, INSTR_POP_ON, 0 ); | |
2726 | var_parse_group_compile( group, c ); | |
2727 | compile_pop_cleanup( c ); | |
2728 | compile_emit( c, INSTR_POP_ON, 0 ); | |
2729 | compile_set_label( c, end ); | |
2730 | } | |
2731 | var_parse_group_free( group ); | |
2732 | } | |
2733 | else | |
2734 | { | |
2735 | int end = compile_new_label( c ); | |
2736 | compile_parse( parse->left, c, RESULT_STACK ); | |
2737 | compile_emit_branch( c, INSTR_PUSH_ON, end ); | |
2738 | compile_push_cleanup( c, INSTR_POP_ON, 0 ); | |
2739 | compile_parse( parse->right, c, RESULT_STACK ); | |
2740 | compile_pop_cleanup( c ); | |
2741 | compile_emit( c, INSTR_POP_ON, 0 ); | |
2742 | compile_set_label( c, end ); | |
2743 | } | |
2744 | adjust_result( c, RESULT_STACK, result_location ); | |
2745 | } | |
2746 | else if ( parse->type == PARSE_RULE ) | |
2747 | { | |
2748 | PARSE * p; | |
2749 | int n = 0; | |
2750 | VAR_PARSE_GROUP * group; | |
2751 | char const * s = object_str( parse->string ); | |
2752 | ||
2753 | if ( parse->left->left || parse->left->right->type != PARSE_NULL ) | |
2754 | for ( p = parse->left; p; p = p->left ) | |
2755 | { | |
2756 | compile_parse( p->right, c, RESULT_STACK ); | |
2757 | ++n; | |
2758 | } | |
2759 | ||
2760 | current_file = object_str( parse->file ); | |
2761 | current_line = parse->line; | |
2762 | group = parse_expansion( &s ); | |
2763 | ||
2764 | if ( group->elems->size == 2 && | |
2765 | dynamic_array_at( VAR_PARSE *, group->elems, 0 )->type == VAR_PARSE_TYPE_VAR && | |
2766 | dynamic_array_at( VAR_PARSE *, group->elems, 1 )->type == VAR_PARSE_TYPE_STRING && | |
2767 | ( object_str( ( (VAR_PARSE_STRING *)dynamic_array_at( VAR_PARSE *, group->elems, 1 ) )->s )[ 0 ] == '.' ) ) | |
2768 | { | |
2769 | VAR_PARSE_STRING * access = (VAR_PARSE_STRING *)dynamic_array_at( VAR_PARSE *, group->elems, 1 ); | |
2770 | OBJECT * member = object_new( object_str( access->s ) + 1 ); | |
2771 | /* Emit the object */ | |
2772 | var_parse_var_compile( (VAR_PARSE_VAR *)dynamic_array_at( VAR_PARSE *, group->elems, 0 ), c ); | |
2773 | var_parse_group_free( group ); | |
2774 | compile_emit( c, INSTR_CALL_MEMBER_RULE, n ); | |
2775 | compile_emit( c, compile_emit_constant( c, member ), parse->line ); | |
2776 | object_free( member ); | |
2777 | } | |
2778 | else | |
2779 | { | |
2780 | var_parse_group_compile( group, c ); | |
2781 | var_parse_group_free( group ); | |
2782 | compile_emit( c, INSTR_CALL_RULE, n ); | |
2783 | compile_emit( c, compile_emit_constant( c, parse->string ), parse->line ); | |
2784 | } | |
2785 | ||
2786 | adjust_result( c, RESULT_STACK, result_location ); | |
2787 | } | |
2788 | else if ( parse->type == PARSE_RULES ) | |
2789 | { | |
2790 | do compile_parse( parse->left, c, RESULT_NONE ); | |
2791 | while ( ( parse = parse->right )->type == PARSE_RULES ); | |
2792 | compile_parse( parse, c, result_location ); | |
2793 | } | |
2794 | else if ( parse->type == PARSE_SET ) | |
2795 | { | |
2796 | PARSE * vars = parse->left; | |
2797 | unsigned int op_code; | |
2798 | unsigned int op_code_group; | |
2799 | ||
2800 | switch ( parse->num ) | |
2801 | { | |
2802 | case ASSIGN_APPEND: op_code = INSTR_APPEND; op_code_group = INSTR_APPEND_GROUP; break; | |
2803 | case ASSIGN_DEFAULT: op_code = INSTR_DEFAULT; op_code_group = INSTR_DEFAULT_GROUP; break; | |
2804 | default: op_code = INSTR_SET; op_code_group = INSTR_SET_GROUP; break; | |
2805 | } | |
2806 | ||
2807 | /* Check whether there is exactly one variable with a constant name. */ | |
2808 | if ( vars->type == PARSE_LIST ) | |
2809 | { | |
2810 | char const * s = object_str( vars->string ); | |
2811 | VAR_PARSE_GROUP * group; | |
2812 | current_file = object_str( parse->file ); | |
2813 | current_line = parse->line; | |
2814 | group = parse_expansion( &s ); | |
2815 | if ( group->elems->size == 1 && dynamic_array_at( VAR_PARSE *, | |
2816 | group->elems, 0 )->type == VAR_PARSE_TYPE_STRING ) | |
2817 | { | |
2818 | int const name = compile_emit_constant( c, ( | |
2819 | (VAR_PARSE_STRING *)dynamic_array_at( VAR_PARSE *, | |
2820 | group->elems, 0 ) )->s ); | |
2821 | var_parse_group_free( group ); | |
2822 | compile_parse( parse->right, c, RESULT_STACK ); | |
2823 | if ( result_location != RESULT_NONE ) | |
2824 | { | |
2825 | compile_emit( c, INSTR_SET_RESULT, 1 ); | |
2826 | } | |
2827 | compile_emit( c, op_code, name ); | |
2828 | } | |
2829 | else | |
2830 | { | |
2831 | var_parse_group_compile( group, c ); | |
2832 | var_parse_group_free( group ); | |
2833 | compile_parse( parse->right, c, RESULT_STACK ); | |
2834 | if ( result_location != RESULT_NONE ) | |
2835 | { | |
2836 | compile_emit( c, INSTR_SET_RESULT, 1 ); | |
2837 | } | |
2838 | compile_emit( c, op_code_group, 0 ); | |
2839 | } | |
2840 | } | |
2841 | else | |
2842 | { | |
2843 | compile_parse( parse->left, c, RESULT_STACK ); | |
2844 | compile_parse( parse->right, c, RESULT_STACK ); | |
2845 | if ( result_location != RESULT_NONE ) | |
2846 | { | |
2847 | compile_emit( c, INSTR_SET_RESULT, 1 ); | |
2848 | } | |
2849 | compile_emit( c, op_code_group, 0 ); | |
2850 | } | |
2851 | if ( result_location != RESULT_NONE ) | |
2852 | { | |
2853 | adjust_result( c, RESULT_RETURN, result_location ); | |
2854 | } | |
2855 | } | |
2856 | else if ( parse->type == PARSE_SETCOMP ) | |
2857 | { | |
2858 | int n_args; | |
2859 | struct arg_list * args = arg_list_compile( parse->right, &n_args ); | |
2860 | int const rule_id = compile_emit_rule( c, parse->string, parse->left, | |
2861 | n_args, args, parse->num ); | |
2862 | compile_emit( c, INSTR_RULE, rule_id ); | |
2863 | adjust_result( c, RESULT_NONE, result_location ); | |
2864 | } | |
2865 | else if ( parse->type == PARSE_SETEXEC ) | |
2866 | { | |
2867 | int const actions_id = compile_emit_actions( c, parse ); | |
2868 | compile_parse( parse->left, c, RESULT_STACK ); | |
2869 | compile_emit( c, INSTR_ACTIONS, actions_id ); | |
2870 | adjust_result( c, RESULT_NONE, result_location ); | |
2871 | } | |
2872 | else if ( parse->type == PARSE_SETTINGS ) | |
2873 | { | |
2874 | compile_parse( parse->left, c, RESULT_STACK ); | |
2875 | compile_parse( parse->third, c, RESULT_STACK ); | |
2876 | compile_parse( parse->right, c, RESULT_STACK ); | |
2877 | ||
2878 | switch ( parse->num ) | |
2879 | { | |
2880 | case ASSIGN_APPEND: compile_emit( c, INSTR_APPEND_ON, 0 ); break; | |
2881 | case ASSIGN_DEFAULT: compile_emit( c, INSTR_DEFAULT_ON, 0 ); break; | |
2882 | default: compile_emit( c, INSTR_SET_ON, 0 ); break; | |
2883 | } | |
2884 | ||
2885 | adjust_result( c, RESULT_STACK, result_location ); | |
2886 | } | |
2887 | else if ( parse->type == PARSE_SWITCH ) | |
2888 | { | |
2889 | int const switch_end = compile_new_label( c ); | |
2890 | compile_parse( parse->left, c, RESULT_STACK ); | |
2891 | ||
2892 | for ( parse = parse->right; parse; parse = parse->right ) | |
2893 | { | |
2894 | int const id = compile_emit_constant( c, parse->left->string ); | |
2895 | int const next_case = compile_new_label( c ); | |
2896 | compile_emit( c, INSTR_PUSH_CONSTANT, id ); | |
2897 | compile_emit_branch( c, INSTR_JUMP_NOT_GLOB, next_case ); | |
2898 | compile_parse( parse->left->left, c, result_location ); | |
2899 | compile_emit_branch( c, INSTR_JUMP, switch_end ); | |
2900 | compile_set_label( c, next_case ); | |
2901 | } | |
2902 | compile_emit( c, INSTR_POP, 0 ); | |
2903 | adjust_result( c, RESULT_NONE, result_location ); | |
2904 | compile_set_label( c, switch_end ); | |
2905 | } | |
2906 | else if ( parse->type == PARSE_RETURN ) | |
2907 | { | |
2908 | compile_parse( parse->left, c, RESULT_RETURN ); | |
2909 | compile_emit_cleanups( c, 0 ); | |
2910 | compile_emit( c, INSTR_RETURN, 0 ); /* 0 for return in the middle of a function. */ | |
2911 | } | |
2912 | else if ( parse->type == PARSE_BREAK ) | |
2913 | { | |
2914 | compile_emit_loop_jump( c, LOOP_INFO_BREAK ); | |
2915 | } | |
2916 | else if ( parse->type == PARSE_CONTINUE ) | |
2917 | { | |
2918 | compile_emit_loop_jump( c, LOOP_INFO_CONTINUE ); | |
2919 | } | |
2920 | else if ( parse->type == PARSE_NULL ) | |
2921 | adjust_result( c, RESULT_NONE, result_location ); | |
2922 | else | |
2923 | assert( !"unknown PARSE type." ); | |
2924 | } | |
2925 | ||
2926 | OBJECT * function_rulename( FUNCTION * function ) | |
2927 | { | |
2928 | return function->rulename; | |
2929 | } | |
2930 | ||
2931 | void function_set_rulename( FUNCTION * function, OBJECT * rulename ) | |
2932 | { | |
2933 | function->rulename = rulename; | |
2934 | } | |
2935 | ||
2936 | void function_location( FUNCTION * function_, OBJECT * * file, int * line ) | |
2937 | { | |
2938 | if ( function_->type == FUNCTION_BUILTIN ) | |
2939 | { | |
2940 | *file = constant_builtin; | |
2941 | *line = -1; | |
2942 | } | |
2943 | #ifdef HAVE_PYTHON | |
2944 | if ( function_->type == FUNCTION_PYTHON ) | |
2945 | { | |
2946 | *file = constant_builtin; | |
2947 | *line = -1; | |
2948 | } | |
2949 | #endif | |
2950 | else | |
2951 | { | |
2952 | JAM_FUNCTION * function = (JAM_FUNCTION *)function_; | |
2953 | assert( function_->type == FUNCTION_JAM ); | |
2954 | *file = function->file; | |
2955 | *line = function->line; | |
2956 | } | |
2957 | } | |
2958 | ||
2959 | static struct arg_list * arg_list_compile_builtin( char const * * args, | |
2960 | int * num_arguments ); | |
2961 | ||
2962 | FUNCTION * function_builtin( LIST * ( * func )( FRAME * frame, int flags ), | |
2963 | int flags, char const * * args ) | |
2964 | { | |
2965 | BUILTIN_FUNCTION * result = BJAM_MALLOC( sizeof( BUILTIN_FUNCTION ) ); | |
2966 | result->base.type = FUNCTION_BUILTIN; | |
2967 | result->base.reference_count = 1; | |
2968 | result->base.rulename = 0; | |
2969 | result->base.formal_arguments = arg_list_compile_builtin( args, | |
2970 | &result->base.num_formal_arguments ); | |
2971 | result->func = func; | |
2972 | result->flags = flags; | |
2973 | return (FUNCTION *)result; | |
2974 | } | |
2975 | ||
2976 | FUNCTION * function_compile( PARSE * parse ) | |
2977 | { | |
2978 | compiler c[ 1 ]; | |
2979 | JAM_FUNCTION * result; | |
2980 | compiler_init( c ); | |
2981 | compile_parse( parse, c, RESULT_RETURN ); | |
2982 | compile_emit( c, INSTR_RETURN, 1 ); | |
2983 | result = compile_to_function( c ); | |
2984 | compiler_free( c ); | |
2985 | result->file = object_copy( parse->file ); | |
2986 | result->line = parse->line; | |
2987 | return (FUNCTION *)result; | |
2988 | } | |
2989 | ||
2990 | FUNCTION * function_compile_actions( char const * actions, OBJECT * file, | |
2991 | int line ) | |
2992 | { | |
2993 | compiler c[ 1 ]; | |
2994 | JAM_FUNCTION * result; | |
2995 | VAR_PARSE_ACTIONS * parse; | |
2996 | current_file = object_str( file ); | |
2997 | current_line = line; | |
2998 | parse = parse_actions( actions ); | |
2999 | compiler_init( c ); | |
3000 | var_parse_actions_compile( parse, c ); | |
3001 | var_parse_actions_free( parse ); | |
3002 | compile_emit( c, INSTR_RETURN, 1 ); | |
3003 | result = compile_to_function( c ); | |
3004 | compiler_free( c ); | |
3005 | result->file = object_copy( file ); | |
3006 | result->line = line; | |
3007 | return (FUNCTION *)result; | |
3008 | } | |
3009 | ||
3010 | static void argument_list_print( struct arg_list * args, int num_args ); | |
3011 | ||
3012 | ||
3013 | /* Define delimiters for type check elements in argument lists (and return type | |
3014 | * specifications, eventually). | |
3015 | */ | |
3016 | # define TYPE_OPEN_DELIM '[' | |
3017 | # define TYPE_CLOSE_DELIM ']' | |
3018 | ||
3019 | /* | |
3020 | * is_type_name() - true iff the given string represents a type check | |
3021 | * specification. | |
3022 | */ | |
3023 | ||
3024 | int is_type_name( char const * s ) | |
3025 | { | |
3026 | return s[ 0 ] == TYPE_OPEN_DELIM && s[ strlen( s ) - 1 ] == | |
3027 | TYPE_CLOSE_DELIM; | |
3028 | } | |
3029 | ||
3030 | static void argument_error( char const * message, FUNCTION * procedure, | |
3031 | FRAME * frame, OBJECT * arg ) | |
3032 | { | |
3033 | extern void print_source_line( FRAME * ); | |
3034 | LOL * actual = frame->args; | |
3035 | backtrace_line( frame->prev ); | |
3036 | out_printf( "*** argument error\n* rule %s ( ", frame->rulename ); | |
3037 | argument_list_print( procedure->formal_arguments, | |
3038 | procedure->num_formal_arguments ); | |
3039 | out_printf( " )\n* called with: ( " ); | |
3040 | lol_print( actual ); | |
3041 | out_printf( " )\n* %s %s\n", message, arg ? object_str ( arg ) : "" ); | |
3042 | function_location( procedure, &frame->file, &frame->line ); | |
3043 | print_source_line( frame ); | |
3044 | out_printf( "see definition of rule '%s' being called\n", frame->rulename ); | |
3045 | backtrace( frame->prev ); | |
3046 | exit( EXITBAD ); | |
3047 | } | |
3048 | ||
3049 | static void type_check_range( OBJECT * type_name, LISTITER iter, LISTITER end, | |
3050 | FRAME * caller, FUNCTION * called, OBJECT * arg_name ) | |
3051 | { | |
3052 | static module_t * typecheck = 0; | |
3053 | ||
3054 | /* If nothing to check, bail now. */ | |
3055 | if ( iter == end || !type_name ) | |
3056 | return; | |
3057 | ||
3058 | if ( !typecheck ) | |
3059 | typecheck = bindmodule( constant_typecheck ); | |
3060 | ||
3061 | /* If the checking rule can not be found, also bail. */ | |
3062 | if ( !typecheck->rules || !hash_find( typecheck->rules, type_name ) ) | |
3063 | return; | |
3064 | ||
3065 | for ( ; iter != end; iter = list_next( iter ) ) | |
3066 | { | |
3067 | LIST * error; | |
3068 | FRAME frame[ 1 ]; | |
3069 | frame_init( frame ); | |
3070 | frame->module = typecheck; | |
3071 | frame->prev = caller; | |
3072 | frame->prev_user = caller->module->user_module | |
3073 | ? caller | |
3074 | : caller->prev_user; | |
3075 | ||
3076 | /* Prepare the argument list */ | |
3077 | lol_add( frame->args, list_new( object_copy( list_item( iter ) ) ) ); | |
3078 | error = evaluate_rule( bindrule( type_name, frame->module ), type_name, frame ); | |
3079 | ||
3080 | if ( !list_empty( error ) ) | |
3081 | argument_error( object_str( list_front( error ) ), called, caller, | |
3082 | arg_name ); | |
3083 | ||
3084 | frame_free( frame ); | |
3085 | } | |
3086 | } | |
3087 | ||
3088 | static void type_check( OBJECT * type_name, LIST * values, FRAME * caller, | |
3089 | FUNCTION * called, OBJECT * arg_name ) | |
3090 | { | |
3091 | type_check_range( type_name, list_begin( values ), list_end( values ), | |
3092 | caller, called, arg_name ); | |
3093 | } | |
3094 | ||
3095 | void argument_list_check( struct arg_list * formal, int formal_count, | |
3096 | FUNCTION * function, FRAME * frame ) | |
3097 | { | |
3098 | LOL * all_actual = frame->args; | |
3099 | int i; | |
3100 | ||
3101 | for ( i = 0; i < formal_count; ++i ) | |
3102 | { | |
3103 | LIST * actual = lol_get( all_actual, i ); | |
3104 | LISTITER actual_iter = list_begin( actual ); | |
3105 | LISTITER const actual_end = list_end( actual ); | |
3106 | int j; | |
3107 | for ( j = 0; j < formal[ i ].size; ++j ) | |
3108 | { | |
3109 | struct argument * formal_arg = &formal[ i ].args[ j ]; | |
3110 | LIST * value; | |
3111 | ||
3112 | switch ( formal_arg->flags ) | |
3113 | { | |
3114 | case ARG_ONE: | |
3115 | if ( actual_iter == actual_end ) | |
3116 | argument_error( "missing argument", function, frame, | |
3117 | formal_arg->arg_name ); | |
3118 | type_check_range( formal_arg->type_name, actual_iter, | |
3119 | list_next( actual_iter ), frame, function, | |
3120 | formal_arg->arg_name ); | |
3121 | actual_iter = list_next( actual_iter ); | |
3122 | break; | |
3123 | case ARG_OPTIONAL: | |
3124 | if ( actual_iter == actual_end ) | |
3125 | value = L0; | |
3126 | else | |
3127 | { | |
3128 | type_check_range( formal_arg->type_name, actual_iter, | |
3129 | list_next( actual_iter ), frame, function, | |
3130 | formal_arg->arg_name ); | |
3131 | actual_iter = list_next( actual_iter ); | |
3132 | } | |
3133 | break; | |
3134 | case ARG_PLUS: | |
3135 | if ( actual_iter == actual_end ) | |
3136 | argument_error( "missing argument", function, frame, | |
3137 | formal_arg->arg_name ); | |
3138 | /* fallthrough */ | |
3139 | case ARG_STAR: | |
3140 | type_check_range( formal_arg->type_name, actual_iter, | |
3141 | actual_end, frame, function, formal_arg->arg_name ); | |
3142 | actual_iter = actual_end; | |
3143 | break; | |
3144 | case ARG_VARIADIC: | |
3145 | return; | |
3146 | } | |
3147 | } | |
3148 | ||
3149 | if ( actual_iter != actual_end ) | |
3150 | argument_error( "extra argument", function, frame, list_item( | |
3151 | actual_iter ) ); | |
3152 | } | |
3153 | ||
3154 | for ( ; i < all_actual->count; ++i ) | |
3155 | { | |
3156 | LIST * actual = lol_get( all_actual, i ); | |
3157 | if ( !list_empty( actual ) ) | |
3158 | argument_error( "extra argument", function, frame, list_front( | |
3159 | actual ) ); | |
3160 | } | |
3161 | } | |
3162 | ||
3163 | void argument_list_push( struct arg_list * formal, int formal_count, | |
3164 | FUNCTION * function, FRAME * frame, STACK * s ) | |
3165 | { | |
3166 | LOL * all_actual = frame->args; | |
3167 | int i; | |
3168 | ||
3169 | for ( i = 0; i < formal_count; ++i ) | |
3170 | { | |
3171 | LIST * actual = lol_get( all_actual, i ); | |
3172 | LISTITER actual_iter = list_begin( actual ); | |
3173 | LISTITER const actual_end = list_end( actual ); | |
3174 | int j; | |
3175 | for ( j = 0; j < formal[ i ].size; ++j ) | |
3176 | { | |
3177 | struct argument * formal_arg = &formal[ i ].args[ j ]; | |
3178 | LIST * value; | |
3179 | ||
3180 | switch ( formal_arg->flags ) | |
3181 | { | |
3182 | case ARG_ONE: | |
3183 | if ( actual_iter == actual_end ) | |
3184 | argument_error( "missing argument", function, frame, | |
3185 | formal_arg->arg_name ); | |
3186 | value = list_new( object_copy( list_item( actual_iter ) ) ); | |
3187 | actual_iter = list_next( actual_iter ); | |
3188 | break; | |
3189 | case ARG_OPTIONAL: | |
3190 | if ( actual_iter == actual_end ) | |
3191 | value = L0; | |
3192 | else | |
3193 | { | |
3194 | value = list_new( object_copy( list_item( actual_iter ) ) ); | |
3195 | actual_iter = list_next( actual_iter ); | |
3196 | } | |
3197 | break; | |
3198 | case ARG_PLUS: | |
3199 | if ( actual_iter == actual_end ) | |
3200 | argument_error( "missing argument", function, frame, | |
3201 | formal_arg->arg_name ); | |
3202 | /* fallthrough */ | |
3203 | case ARG_STAR: | |
3204 | value = list_copy_range( actual, actual_iter, actual_end ); | |
3205 | actual_iter = actual_end; | |
3206 | break; | |
3207 | case ARG_VARIADIC: | |
3208 | return; | |
3209 | } | |
3210 | ||
3211 | type_check( formal_arg->type_name, value, frame, function, | |
3212 | formal_arg->arg_name ); | |
3213 | ||
3214 | if ( formal_arg->index != -1 ) | |
3215 | { | |
3216 | LIST * * const old = &frame->module->fixed_variables[ | |
3217 | formal_arg->index ]; | |
3218 | stack_push( s, *old ); | |
3219 | *old = value; | |
3220 | } | |
3221 | else | |
3222 | stack_push( s, var_swap( frame->module, formal_arg->arg_name, | |
3223 | value ) ); | |
3224 | } | |
3225 | ||
3226 | if ( actual_iter != actual_end ) | |
3227 | argument_error( "extra argument", function, frame, list_item( | |
3228 | actual_iter ) ); | |
3229 | } | |
3230 | ||
3231 | for ( ; i < all_actual->count; ++i ) | |
3232 | { | |
3233 | LIST * const actual = lol_get( all_actual, i ); | |
3234 | if ( !list_empty( actual ) ) | |
3235 | argument_error( "extra argument", function, frame, list_front( | |
3236 | actual ) ); | |
3237 | } | |
3238 | } | |
3239 | ||
3240 | void argument_list_pop( struct arg_list * formal, int formal_count, | |
3241 | FRAME * frame, STACK * s ) | |
3242 | { | |
3243 | int i; | |
3244 | for ( i = formal_count - 1; i >= 0; --i ) | |
3245 | { | |
3246 | int j; | |
3247 | for ( j = formal[ i ].size - 1; j >= 0 ; --j ) | |
3248 | { | |
3249 | struct argument * formal_arg = &formal[ i ].args[ j ]; | |
3250 | ||
3251 | if ( formal_arg->flags == ARG_VARIADIC ) | |
3252 | continue; | |
3253 | if ( formal_arg->index != -1 ) | |
3254 | { | |
3255 | LIST * const old = stack_pop( s ); | |
3256 | LIST * * const pos = &frame->module->fixed_variables[ | |
3257 | formal_arg->index ]; | |
3258 | list_free( *pos ); | |
3259 | *pos = old; | |
3260 | } | |
3261 | else | |
3262 | var_set( frame->module, formal_arg->arg_name, stack_pop( s ), | |
3263 | VAR_SET ); | |
3264 | } | |
3265 | } | |
3266 | } | |
3267 | ||
3268 | ||
3269 | struct argument_compiler | |
3270 | { | |
3271 | struct dynamic_array args[ 1 ]; | |
3272 | struct argument arg; | |
3273 | int state; | |
3274 | #define ARGUMENT_COMPILER_START 0 | |
3275 | #define ARGUMENT_COMPILER_FOUND_TYPE 1 | |
3276 | #define ARGUMENT_COMPILER_FOUND_OBJECT 2 | |
3277 | #define ARGUMENT_COMPILER_DONE 3 | |
3278 | }; | |
3279 | ||
3280 | ||
3281 | static void argument_compiler_init( struct argument_compiler * c ) | |
3282 | { | |
3283 | dynamic_array_init( c->args ); | |
3284 | c->state = ARGUMENT_COMPILER_START; | |
3285 | } | |
3286 | ||
3287 | static void argument_compiler_free( struct argument_compiler * c ) | |
3288 | { | |
3289 | dynamic_array_free( c->args ); | |
3290 | } | |
3291 | ||
3292 | static void argument_compiler_add( struct argument_compiler * c, OBJECT * arg, | |
3293 | OBJECT * file, int line ) | |
3294 | { | |
3295 | switch ( c->state ) | |
3296 | { | |
3297 | case ARGUMENT_COMPILER_FOUND_OBJECT: | |
3298 | ||
3299 | if ( object_equal( arg, constant_question_mark ) ) | |
3300 | { | |
3301 | c->arg.flags = ARG_OPTIONAL; | |
3302 | } | |
3303 | else if ( object_equal( arg, constant_plus ) ) | |
3304 | { | |
3305 | c->arg.flags = ARG_PLUS; | |
3306 | } | |
3307 | else if ( object_equal( arg, constant_star ) ) | |
3308 | { | |
3309 | c->arg.flags = ARG_STAR; | |
3310 | } | |
3311 | ||
3312 | dynamic_array_push( c->args, c->arg ); | |
3313 | c->state = ARGUMENT_COMPILER_START; | |
3314 | ||
3315 | if ( c->arg.flags != ARG_ONE ) | |
3316 | break; | |
3317 | /* fall-through */ | |
3318 | ||
3319 | case ARGUMENT_COMPILER_START: | |
3320 | ||
3321 | c->arg.type_name = 0; | |
3322 | c->arg.index = -1; | |
3323 | c->arg.flags = ARG_ONE; | |
3324 | ||
3325 | if ( is_type_name( object_str( arg ) ) ) | |
3326 | { | |
3327 | c->arg.type_name = object_copy( arg ); | |
3328 | c->state = ARGUMENT_COMPILER_FOUND_TYPE; | |
3329 | break; | |
3330 | } | |
3331 | /* fall-through */ | |
3332 | ||
3333 | case ARGUMENT_COMPILER_FOUND_TYPE: | |
3334 | ||
3335 | if ( is_type_name( object_str( arg ) ) ) | |
3336 | { | |
3337 | err_printf( "%s:%d: missing argument name before type name: %s\n", | |
3338 | object_str( file ), line, object_str( arg ) ); | |
3339 | exit( EXITBAD ); | |
3340 | } | |
3341 | ||
3342 | c->arg.arg_name = object_copy( arg ); | |
3343 | if ( object_equal( arg, constant_star ) ) | |
3344 | { | |
3345 | c->arg.flags = ARG_VARIADIC; | |
3346 | dynamic_array_push( c->args, c->arg ); | |
3347 | c->state = ARGUMENT_COMPILER_DONE; | |
3348 | } | |
3349 | else | |
3350 | { | |
3351 | c->state = ARGUMENT_COMPILER_FOUND_OBJECT; | |
3352 | } | |
3353 | break; | |
3354 | ||
3355 | case ARGUMENT_COMPILER_DONE: | |
3356 | break; | |
3357 | } | |
3358 | } | |
3359 | ||
3360 | static void argument_compiler_recurse( struct argument_compiler * c, | |
3361 | PARSE * parse ) | |
3362 | { | |
3363 | if ( parse->type == PARSE_APPEND ) | |
3364 | { | |
3365 | argument_compiler_recurse( c, parse->left ); | |
3366 | argument_compiler_recurse( c, parse->right ); | |
3367 | } | |
3368 | else if ( parse->type != PARSE_NULL ) | |
3369 | { | |
3370 | assert( parse->type == PARSE_LIST ); | |
3371 | argument_compiler_add( c, parse->string, parse->file, parse->line ); | |
3372 | } | |
3373 | } | |
3374 | ||
3375 | static struct arg_list arg_compile_impl( struct argument_compiler * c, | |
3376 | OBJECT * file, int line ) | |
3377 | { | |
3378 | struct arg_list result; | |
3379 | switch ( c->state ) | |
3380 | { | |
3381 | case ARGUMENT_COMPILER_START: | |
3382 | case ARGUMENT_COMPILER_DONE: | |
3383 | break; | |
3384 | case ARGUMENT_COMPILER_FOUND_TYPE: | |
3385 | err_printf( "%s:%d: missing argument name after type name: %s\n", | |
3386 | object_str( file ), line, object_str( c->arg.type_name ) ); | |
3387 | exit( EXITBAD ); | |
3388 | case ARGUMENT_COMPILER_FOUND_OBJECT: | |
3389 | dynamic_array_push( c->args, c->arg ); | |
3390 | break; | |
3391 | } | |
3392 | result.size = c->args->size; | |
3393 | result.args = BJAM_MALLOC( c->args->size * sizeof( struct argument ) ); | |
3394 | memcpy( result.args, c->args->data, c->args->size * sizeof( struct argument | |
3395 | ) ); | |
3396 | return result; | |
3397 | } | |
3398 | ||
3399 | static struct arg_list arg_compile( PARSE * parse ) | |
3400 | { | |
3401 | struct argument_compiler c[ 1 ]; | |
3402 | struct arg_list result; | |
3403 | argument_compiler_init( c ); | |
3404 | argument_compiler_recurse( c, parse ); | |
3405 | result = arg_compile_impl( c, parse->file, parse->line ); | |
3406 | argument_compiler_free( c ); | |
3407 | return result; | |
3408 | } | |
3409 | ||
3410 | struct argument_list_compiler | |
3411 | { | |
3412 | struct dynamic_array args[ 1 ]; | |
3413 | }; | |
3414 | ||
3415 | static void argument_list_compiler_init( struct argument_list_compiler * c ) | |
3416 | { | |
3417 | dynamic_array_init( c->args ); | |
3418 | } | |
3419 | ||
3420 | static void argument_list_compiler_free( struct argument_list_compiler * c ) | |
3421 | { | |
3422 | dynamic_array_free( c->args ); | |
3423 | } | |
3424 | ||
3425 | static void argument_list_compiler_add( struct argument_list_compiler * c, | |
3426 | PARSE * parse ) | |
3427 | { | |
3428 | struct arg_list args = arg_compile( parse ); | |
3429 | dynamic_array_push( c->args, args ); | |
3430 | } | |
3431 | ||
3432 | static void argument_list_compiler_recurse( struct argument_list_compiler * c, | |
3433 | PARSE * parse ) | |
3434 | { | |
3435 | if ( parse ) | |
3436 | { | |
3437 | argument_list_compiler_add( c, parse->right ); | |
3438 | argument_list_compiler_recurse( c, parse->left ); | |
3439 | } | |
3440 | } | |
3441 | ||
3442 | static struct arg_list * arg_list_compile( PARSE * parse, int * num_arguments ) | |
3443 | { | |
3444 | if ( parse ) | |
3445 | { | |
3446 | struct argument_list_compiler c[ 1 ]; | |
3447 | struct arg_list * result; | |
3448 | argument_list_compiler_init( c ); | |
3449 | argument_list_compiler_recurse( c, parse ); | |
3450 | *num_arguments = c->args->size; | |
3451 | result = BJAM_MALLOC( c->args->size * sizeof( struct arg_list ) ); | |
3452 | memcpy( result, c->args->data, c->args->size * sizeof( struct arg_list ) | |
3453 | ); | |
3454 | argument_list_compiler_free( c ); | |
3455 | return result; | |
3456 | } | |
3457 | *num_arguments = 0; | |
3458 | return 0; | |
3459 | } | |
3460 | ||
3461 | static struct arg_list * arg_list_compile_builtin( char const * * args, | |
3462 | int * num_arguments ) | |
3463 | { | |
3464 | if ( args ) | |
3465 | { | |
3466 | struct argument_list_compiler c[ 1 ]; | |
3467 | struct arg_list * result; | |
3468 | argument_list_compiler_init( c ); | |
3469 | while ( *args ) | |
3470 | { | |
3471 | struct argument_compiler arg_comp[ 1 ]; | |
3472 | struct arg_list arg; | |
3473 | argument_compiler_init( arg_comp ); | |
3474 | for ( ; *args; ++args ) | |
3475 | { | |
3476 | OBJECT * token; | |
3477 | if ( strcmp( *args, ":" ) == 0 ) | |
3478 | { | |
3479 | ++args; | |
3480 | break; | |
3481 | } | |
3482 | token = object_new( *args ); | |
3483 | argument_compiler_add( arg_comp, token, constant_builtin, -1 ); | |
3484 | object_free( token ); | |
3485 | } | |
3486 | arg = arg_compile_impl( arg_comp, constant_builtin, -1 ); | |
3487 | dynamic_array_push( c->args, arg ); | |
3488 | argument_compiler_free( arg_comp ); | |
3489 | } | |
3490 | *num_arguments = c->args->size; | |
3491 | result = BJAM_MALLOC( c->args->size * sizeof( struct arg_list ) ); | |
3492 | memcpy( result, c->args->data, c->args->size * sizeof( struct arg_list ) | |
3493 | ); | |
3494 | argument_list_compiler_free( c ); | |
3495 | return result; | |
3496 | } | |
3497 | *num_arguments = 0; | |
3498 | return 0; | |
3499 | } | |
3500 | ||
3501 | static void argument_list_print( struct arg_list * args, int num_args ) | |
3502 | { | |
3503 | if ( args ) | |
3504 | { | |
3505 | int i; | |
3506 | for ( i = 0; i < num_args; ++i ) | |
3507 | { | |
3508 | int j; | |
3509 | if ( i ) out_printf( " : " ); | |
3510 | for ( j = 0; j < args[ i ].size; ++j ) | |
3511 | { | |
3512 | struct argument * formal_arg = &args[ i ].args[ j ]; | |
3513 | if ( j ) out_printf( " " ); | |
3514 | if ( formal_arg->type_name ) | |
3515 | out_printf( "%s ", object_str( formal_arg->type_name ) ); | |
3516 | out_printf( "%s", object_str( formal_arg->arg_name ) ); | |
3517 | switch ( formal_arg->flags ) | |
3518 | { | |
3519 | case ARG_OPTIONAL: out_printf( " ?" ); break; | |
3520 | case ARG_PLUS: out_printf( " +" ); break; | |
3521 | case ARG_STAR: out_printf( " *" ); break; | |
3522 | } | |
3523 | } | |
3524 | } | |
3525 | } | |
3526 | } | |
3527 | ||
3528 | ||
3529 | struct arg_list * argument_list_bind_variables( struct arg_list * formal, | |
3530 | int formal_count, module_t * module, int * counter ) | |
3531 | { | |
3532 | if ( formal ) | |
3533 | { | |
3534 | struct arg_list * result = (struct arg_list *)BJAM_MALLOC( sizeof( | |
3535 | struct arg_list ) * formal_count ); | |
3536 | int i; | |
3537 | ||
3538 | for ( i = 0; i < formal_count; ++i ) | |
3539 | { | |
3540 | int j; | |
3541 | struct argument * args = (struct argument *)BJAM_MALLOC( sizeof( | |
3542 | struct argument ) * formal[ i ].size ); | |
3543 | for ( j = 0; j < formal[ i ].size; ++j ) | |
3544 | { | |
3545 | args[ j ] = formal[ i ].args[ j ]; | |
3546 | if ( args[ j ].type_name ) | |
3547 | args[ j ].type_name = object_copy( args[ j ].type_name ); | |
3548 | args[ j ].arg_name = object_copy( args[ j ].arg_name ); | |
3549 | if ( args[ j ].flags != ARG_VARIADIC ) | |
3550 | args[ j ].index = module_add_fixed_var( module, | |
3551 | args[ j ].arg_name, counter ); | |
3552 | } | |
3553 | result[ i ].args = args; | |
3554 | result[ i ].size = formal[ i ].size; | |
3555 | } | |
3556 | ||
3557 | return result; | |
3558 | } | |
3559 | return 0; | |
3560 | } | |
3561 | ||
3562 | ||
3563 | void argument_list_free( struct arg_list * args, int args_count ) | |
3564 | { | |
3565 | int i; | |
3566 | for ( i = 0; i < args_count; ++i ) | |
3567 | { | |
3568 | int j; | |
3569 | for ( j = 0; j < args[ i ].size; ++j ) | |
3570 | { | |
3571 | if ( args[ i ].args[ j ].type_name ) | |
3572 | object_free( args[ i ].args[ j ].type_name ); | |
3573 | object_free( args[ i ].args[ j ].arg_name ); | |
3574 | } | |
3575 | BJAM_FREE( args[ i ].args ); | |
3576 | } | |
3577 | BJAM_FREE( args ); | |
3578 | } | |
3579 | ||
3580 | ||
3581 | FUNCTION * function_unbind_variables( FUNCTION * f ) | |
3582 | { | |
3583 | if ( f->type == FUNCTION_JAM ) | |
3584 | { | |
3585 | JAM_FUNCTION * const func = (JAM_FUNCTION *)f; | |
3586 | return func->generic ? func->generic : f; | |
3587 | } | |
3588 | #ifdef HAVE_PYTHON | |
3589 | if ( f->type == FUNCTION_PYTHON ) | |
3590 | return f; | |
3591 | #endif | |
3592 | assert( f->type == FUNCTION_BUILTIN ); | |
3593 | return f; | |
3594 | } | |
3595 | ||
3596 | FUNCTION * function_bind_variables( FUNCTION * f, module_t * module, | |
3597 | int * counter ) | |
3598 | { | |
3599 | if ( f->type == FUNCTION_BUILTIN ) | |
3600 | return f; | |
3601 | #ifdef HAVE_PYTHON | |
3602 | if ( f->type == FUNCTION_PYTHON ) | |
3603 | return f; | |
3604 | #endif | |
3605 | { | |
3606 | JAM_FUNCTION * func = (JAM_FUNCTION *)f; | |
3607 | JAM_FUNCTION * new_func = BJAM_MALLOC( sizeof( JAM_FUNCTION ) ); | |
3608 | instruction * code; | |
3609 | int i; | |
3610 | assert( f->type == FUNCTION_JAM ); | |
3611 | memcpy( new_func, func, sizeof( JAM_FUNCTION ) ); | |
3612 | new_func->base.reference_count = 1; | |
3613 | new_func->base.formal_arguments = argument_list_bind_variables( | |
3614 | f->formal_arguments, f->num_formal_arguments, module, counter ); | |
3615 | new_func->code = BJAM_MALLOC( func->code_size * sizeof( instruction ) ); | |
3616 | memcpy( new_func->code, func->code, func->code_size * sizeof( | |
3617 | instruction ) ); | |
3618 | new_func->generic = (FUNCTION *)func; | |
3619 | func = new_func; | |
3620 | for ( i = 0; ; ++i ) | |
3621 | { | |
3622 | OBJECT * key; | |
3623 | int op_code; | |
3624 | code = func->code + i; | |
3625 | switch ( code->op_code ) | |
3626 | { | |
3627 | case INSTR_PUSH_VAR: op_code = INSTR_PUSH_VAR_FIXED; break; | |
3628 | case INSTR_PUSH_LOCAL: op_code = INSTR_PUSH_LOCAL_FIXED; break; | |
3629 | case INSTR_POP_LOCAL: op_code = INSTR_POP_LOCAL_FIXED; break; | |
3630 | case INSTR_SET: op_code = INSTR_SET_FIXED; break; | |
3631 | case INSTR_APPEND: op_code = INSTR_APPEND_FIXED; break; | |
3632 | case INSTR_DEFAULT: op_code = INSTR_DEFAULT_FIXED; break; | |
3633 | case INSTR_RETURN: | |
3634 | if( code->arg == 1 ) return (FUNCTION *)new_func; | |
3635 | else continue; | |
3636 | case INSTR_CALL_MEMBER_RULE: | |
3637 | case INSTR_CALL_RULE: ++i; continue; | |
3638 | case INSTR_PUSH_MODULE: | |
3639 | { | |
3640 | int depth = 1; | |
3641 | ++i; | |
3642 | while ( depth > 0 ) | |
3643 | { | |
3644 | code = func->code + i; | |
3645 | switch ( code->op_code ) | |
3646 | { | |
3647 | case INSTR_PUSH_MODULE: | |
3648 | case INSTR_CLASS: | |
3649 | ++depth; | |
3650 | break; | |
3651 | case INSTR_POP_MODULE: | |
3652 | --depth; | |
3653 | break; | |
3654 | case INSTR_CALL_RULE: | |
3655 | ++i; | |
3656 | break; | |
3657 | } | |
3658 | ++i; | |
3659 | } | |
3660 | --i; | |
3661 | } | |
3662 | default: continue; | |
3663 | } | |
3664 | key = func->constants[ code->arg ]; | |
3665 | if ( !( object_equal( key, constant_TMPDIR ) || | |
3666 | object_equal( key, constant_TMPNAME ) || | |
3667 | object_equal( key, constant_TMPFILE ) || | |
3668 | object_equal( key, constant_STDOUT ) || | |
3669 | object_equal( key, constant_STDERR ) ) ) | |
3670 | { | |
3671 | code->op_code = op_code; | |
3672 | code->arg = module_add_fixed_var( module, key, counter ); | |
3673 | } | |
3674 | } | |
3675 | } | |
3676 | } | |
3677 | ||
b32b8144 FG |
3678 | LIST * function_get_variables( FUNCTION * f ) |
3679 | { | |
3680 | if ( f->type == FUNCTION_BUILTIN ) | |
3681 | return L0; | |
3682 | #ifdef HAVE_PYTHON | |
3683 | if ( f->type == FUNCTION_PYTHON ) | |
3684 | return L0; | |
3685 | #endif | |
3686 | { | |
3687 | JAM_FUNCTION * func = (JAM_FUNCTION *)f; | |
3688 | LIST * result = L0; | |
3689 | instruction * code; | |
3690 | int i; | |
3691 | assert( f->type == FUNCTION_JAM ); | |
3692 | if ( func->generic ) func = ( JAM_FUNCTION * )func->generic; | |
3693 | ||
3694 | for ( i = 0; ; ++i ) | |
3695 | { | |
3696 | OBJECT * var; | |
3697 | code = func->code + i; | |
3698 | switch ( code->op_code ) | |
3699 | { | |
3700 | case INSTR_PUSH_LOCAL: break; | |
3701 | case INSTR_RETURN: return result; | |
3702 | case INSTR_CALL_MEMBER_RULE: | |
3703 | case INSTR_CALL_RULE: ++i; continue; | |
3704 | case INSTR_PUSH_MODULE: | |
3705 | { | |
3706 | int depth = 1; | |
3707 | ++i; | |
3708 | while ( depth > 0 ) | |
3709 | { | |
3710 | code = func->code + i; | |
3711 | switch ( code->op_code ) | |
3712 | { | |
3713 | case INSTR_PUSH_MODULE: | |
3714 | case INSTR_CLASS: | |
3715 | ++depth; | |
3716 | break; | |
3717 | case INSTR_POP_MODULE: | |
3718 | --depth; | |
3719 | break; | |
3720 | case INSTR_CALL_RULE: | |
3721 | ++i; | |
3722 | break; | |
3723 | } | |
3724 | ++i; | |
3725 | } | |
3726 | --i; | |
3727 | } | |
3728 | default: continue; | |
3729 | } | |
3730 | var = func->constants[ code->arg ]; | |
3731 | if ( !( object_equal( var, constant_TMPDIR ) || | |
3732 | object_equal( var, constant_TMPNAME ) || | |
3733 | object_equal( var, constant_TMPFILE ) || | |
3734 | object_equal( var, constant_STDOUT ) || | |
3735 | object_equal( var, constant_STDERR ) ) ) | |
3736 | { | |
3737 | result = list_push_back( result, var ); | |
3738 | } | |
3739 | } | |
3740 | } | |
3741 | } | |
3742 | ||
7c673cae FG |
3743 | void function_refer( FUNCTION * func ) |
3744 | { | |
3745 | ++func->reference_count; | |
3746 | } | |
3747 | ||
3748 | void function_free( FUNCTION * function_ ) | |
3749 | { | |
3750 | int i; | |
3751 | ||
3752 | if ( --function_->reference_count != 0 ) | |
3753 | return; | |
3754 | ||
3755 | if ( function_->formal_arguments ) | |
3756 | argument_list_free( function_->formal_arguments, | |
3757 | function_->num_formal_arguments ); | |
3758 | ||
3759 | if ( function_->type == FUNCTION_JAM ) | |
3760 | { | |
3761 | JAM_FUNCTION * func = (JAM_FUNCTION *)function_; | |
3762 | ||
3763 | BJAM_FREE( func->code ); | |
3764 | ||
3765 | if ( func->generic ) | |
3766 | function_free( func->generic ); | |
3767 | else | |
3768 | { | |
3769 | if ( function_->rulename ) object_free( function_->rulename ); | |
3770 | ||
3771 | for ( i = 0; i < func->num_constants; ++i ) | |
3772 | object_free( func->constants[ i ] ); | |
3773 | BJAM_FREE( func->constants ); | |
3774 | ||
3775 | for ( i = 0; i < func->num_subfunctions; ++i ) | |
3776 | { | |
3777 | object_free( func->functions[ i ].name ); | |
3778 | function_free( func->functions[ i ].code ); | |
3779 | } | |
3780 | BJAM_FREE( func->functions ); | |
3781 | ||
3782 | for ( i = 0; i < func->num_subactions; ++i ) | |
3783 | { | |
3784 | object_free( func->actions[ i ].name ); | |
3785 | function_free( func->actions[ i ].command ); | |
3786 | } | |
3787 | BJAM_FREE( func->actions ); | |
3788 | ||
3789 | object_free( func->file ); | |
3790 | } | |
3791 | } | |
3792 | #ifdef HAVE_PYTHON | |
3793 | else if ( function_->type == FUNCTION_PYTHON ) | |
3794 | { | |
3795 | PYTHON_FUNCTION * func = (PYTHON_FUNCTION *)function_; | |
3796 | Py_DECREF( func->python_function ); | |
3797 | if ( function_->rulename ) object_free( function_->rulename ); | |
3798 | } | |
3799 | #endif | |
3800 | else | |
3801 | { | |
3802 | assert( function_->type == FUNCTION_BUILTIN ); | |
3803 | if ( function_->rulename ) object_free( function_->rulename ); | |
3804 | } | |
3805 | ||
3806 | BJAM_FREE( function_ ); | |
3807 | } | |
3808 | ||
3809 | ||
3810 | /* Alignment check for stack */ | |
3811 | ||
3812 | struct align_var_edits | |
3813 | { | |
3814 | char ch; | |
3815 | VAR_EDITS e; | |
3816 | }; | |
3817 | ||
3818 | struct align_expansion_item | |
3819 | { | |
3820 | char ch; | |
3821 | expansion_item e; | |
3822 | }; | |
3823 | ||
3824 | static char check_align_var_edits[ sizeof(struct align_var_edits) <= sizeof(VAR_EDITS) + sizeof(void *) ? 1 : -1 ]; | |
3825 | static char check_align_expansion_item[ sizeof(struct align_expansion_item) <= sizeof(expansion_item) + sizeof(void *) ? 1 : -1 ]; | |
3826 | ||
3827 | static char check_ptr_size1[ sizeof(LIST *) <= sizeof(void *) ? 1 : -1 ]; | |
3828 | static char check_ptr_size2[ sizeof(char *) <= sizeof(void *) ? 1 : -1 ]; | |
3829 | ||
3830 | void function_run_actions( FUNCTION * function, FRAME * frame, STACK * s, | |
3831 | string * out ) | |
3832 | { | |
3833 | *(string * *)stack_allocate( s, sizeof( string * ) ) = out; | |
3834 | list_free( function_run( function, frame, s ) ); | |
3835 | stack_deallocate( s, sizeof( string * ) ); | |
3836 | } | |
3837 | ||
3838 | /* | |
3839 | * WARNING: The instruction set is tuned for Jam and is not really generic. Be | |
3840 | * especially careful about stack push/pop. | |
3841 | */ | |
3842 | ||
3843 | LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) | |
3844 | { | |
3845 | JAM_FUNCTION * function; | |
3846 | instruction * code; | |
3847 | LIST * l; | |
3848 | LIST * r; | |
3849 | LIST * result = L0; | |
3850 | void * saved_stack = s->data; | |
3851 | ||
3852 | PROFILE_ENTER_LOCAL(function_run); | |
3853 | ||
b32b8144 FG |
3854 | #ifdef JAM_DEBUGGER |
3855 | frame->function = function_; | |
3856 | #endif | |
3857 | ||
7c673cae FG |
3858 | if ( function_->type == FUNCTION_BUILTIN ) |
3859 | { | |
3860 | PROFILE_ENTER_LOCAL(function_run_FUNCTION_BUILTIN); | |
3861 | BUILTIN_FUNCTION const * const f = (BUILTIN_FUNCTION *)function_; | |
3862 | if ( function_->formal_arguments ) | |
3863 | argument_list_check( function_->formal_arguments, | |
3864 | function_->num_formal_arguments, function_, frame ); | |
b32b8144 FG |
3865 | |
3866 | debug_on_enter_function( frame, f->base.rulename, NULL, -1 ); | |
3867 | result = f->func( frame, f->flags ); | |
3868 | debug_on_exit_function( f->base.rulename ); | |
7c673cae FG |
3869 | PROFILE_EXIT_LOCAL(function_run_FUNCTION_BUILTIN); |
3870 | PROFILE_EXIT_LOCAL(function_run); | |
b32b8144 | 3871 | return result; |
7c673cae FG |
3872 | } |
3873 | ||
3874 | #ifdef HAVE_PYTHON | |
3875 | else if ( function_->type == FUNCTION_PYTHON ) | |
3876 | { | |
3877 | PROFILE_ENTER_LOCAL(function_run_FUNCTION_PYTHON); | |
3878 | PYTHON_FUNCTION * f = (PYTHON_FUNCTION *)function_; | |
b32b8144 FG |
3879 | debug_on_enter_function( frame, f->base.rulename, NULL, -1 ); |
3880 | result = call_python_function( f, frame ); | |
3881 | debug_on_exit_function( f->base.rulename ); | |
7c673cae FG |
3882 | PROFILE_EXIT_LOCAL(function_run_FUNCTION_PYTHON); |
3883 | PROFILE_EXIT_LOCAL(function_run); | |
b32b8144 | 3884 | return result; |
7c673cae FG |
3885 | } |
3886 | #endif | |
3887 | ||
3888 | assert( function_->type == FUNCTION_JAM ); | |
3889 | ||
3890 | if ( function_->formal_arguments ) | |
3891 | argument_list_push( function_->formal_arguments, | |
3892 | function_->num_formal_arguments, function_, frame, s ); | |
3893 | ||
3894 | function = (JAM_FUNCTION *)function_; | |
b32b8144 | 3895 | debug_on_enter_function( frame, function->base.rulename, function->file, function->line ); |
7c673cae FG |
3896 | code = function->code; |
3897 | for ( ; ; ) | |
3898 | { | |
3899 | switch ( code->op_code ) | |
3900 | { | |
3901 | ||
3902 | /* | |
3903 | * Basic stack manipulation | |
3904 | */ | |
3905 | ||
3906 | case INSTR_PUSH_EMPTY: | |
3907 | { | |
3908 | PROFILE_ENTER_LOCAL(function_run_INSTR_PUSH_EMPTY); | |
3909 | stack_push( s, L0 ); | |
3910 | PROFILE_EXIT_LOCAL(function_run_INSTR_PUSH_EMPTY); | |
3911 | break; | |
3912 | } | |
3913 | ||
3914 | case INSTR_PUSH_CONSTANT: | |
3915 | { | |
3916 | PROFILE_ENTER_LOCAL(function_run_INSTR_PUSH_CONSTANT); | |
3917 | OBJECT * value = function_get_constant( function, code->arg ); | |
3918 | stack_push( s, list_new( object_copy( value ) ) ); | |
3919 | PROFILE_EXIT_LOCAL(function_run_INSTR_PUSH_CONSTANT); | |
3920 | break; | |
3921 | } | |
3922 | ||
3923 | case INSTR_PUSH_ARG: | |
3924 | { | |
3925 | PROFILE_ENTER_LOCAL(function_run_INSTR_PUSH_ARG); | |
3926 | stack_push( s, frame_get_local( frame, code->arg ) ); | |
3927 | PROFILE_EXIT_LOCAL(function_run_INSTR_PUSH_ARG); | |
3928 | break; | |
3929 | } | |
3930 | ||
3931 | case INSTR_PUSH_VAR: | |
3932 | { | |
3933 | PROFILE_ENTER_LOCAL(function_run_INSTR_PUSH_VAR); | |
3934 | stack_push( s, function_get_variable( function, frame, code->arg ) ); | |
3935 | PROFILE_EXIT_LOCAL(function_run_INSTR_PUSH_VAR); | |
3936 | break; | |
3937 | } | |
3938 | ||
3939 | case INSTR_PUSH_VAR_FIXED: | |
3940 | { | |
3941 | PROFILE_ENTER_LOCAL(function_run_INSTR_PUSH_VAR_FIXED); | |
3942 | stack_push( s, list_copy( frame->module->fixed_variables[ code->arg | |
3943 | ] ) ); | |
3944 | PROFILE_EXIT_LOCAL(function_run_INSTR_PUSH_VAR_FIXED); | |
3945 | break; | |
3946 | } | |
3947 | ||
3948 | case INSTR_PUSH_GROUP: | |
3949 | { | |
3950 | PROFILE_ENTER_LOCAL(function_run_INSTR_PUSH_GROUP); | |
3951 | LIST * value = L0; | |
3952 | LISTITER iter; | |
3953 | LISTITER end; | |
3954 | l = stack_pop( s ); | |
3955 | for ( iter = list_begin( l ), end = list_end( l ); iter != end; | |
3956 | iter = list_next( iter ) ) | |
3957 | value = list_append( value, function_get_named_variable( | |
3958 | function, frame, list_item( iter ) ) ); | |
3959 | list_free( l ); | |
3960 | stack_push( s, value ); | |
3961 | PROFILE_EXIT_LOCAL(function_run_INSTR_PUSH_GROUP); | |
3962 | break; | |
3963 | } | |
3964 | ||
3965 | case INSTR_PUSH_APPEND: | |
3966 | { | |
3967 | PROFILE_ENTER_LOCAL(function_run_INSTR_PUSH_APPEND); | |
3968 | r = stack_pop( s ); | |
3969 | l = stack_pop( s ); | |
3970 | stack_push( s, list_append( l, r ) ); | |
3971 | PROFILE_EXIT_LOCAL(function_run_INSTR_PUSH_APPEND); | |
3972 | break; | |
3973 | } | |
3974 | ||
3975 | case INSTR_SWAP: | |
3976 | { | |
3977 | PROFILE_ENTER_LOCAL(function_run_INSTR_SWAP); | |
3978 | l = stack_top( s ); | |
3979 | stack_set( s, 0, stack_at( s, code->arg ) ); | |
3980 | stack_set( s, code->arg, l ); | |
3981 | PROFILE_EXIT_LOCAL(function_run_INSTR_SWAP); | |
3982 | break; | |
3983 | } | |
3984 | ||
3985 | case INSTR_POP: | |
3986 | { | |
3987 | PROFILE_ENTER_LOCAL(function_run_INSTR_POP); | |
3988 | list_free( stack_pop( s ) ); | |
3989 | PROFILE_EXIT_LOCAL(function_run_INSTR_POP); | |
3990 | break; | |
3991 | } | |
3992 | ||
3993 | /* | |
3994 | * Branch instructions | |
3995 | */ | |
3996 | ||
3997 | case INSTR_JUMP: | |
3998 | { | |
3999 | PROFILE_ENTER_LOCAL(function_run_INSTR_JUMP); | |
4000 | code += code->arg; | |
4001 | PROFILE_EXIT_LOCAL(function_run_INSTR_JUMP); | |
4002 | break; | |
4003 | } | |
4004 | ||
4005 | case INSTR_JUMP_EMPTY: | |
4006 | { | |
4007 | PROFILE_ENTER_LOCAL(function_run_INSTR_JUMP_EMPTY); | |
4008 | l = stack_pop( s ); | |
4009 | if ( !list_cmp( l, L0 ) ) code += code->arg; | |
4010 | list_free( l ); | |
4011 | PROFILE_EXIT_LOCAL(function_run_INSTR_JUMP_EMPTY); | |
4012 | break; | |
4013 | } | |
4014 | ||
4015 | case INSTR_JUMP_NOT_EMPTY: | |
4016 | { | |
4017 | PROFILE_ENTER_LOCAL(function_run_INSTR_JUMP_NOT_EMPTY); | |
4018 | l = stack_pop( s ); | |
4019 | if ( list_cmp( l, L0 ) ) code += code->arg; | |
4020 | list_free( l ); | |
4021 | PROFILE_EXIT_LOCAL(function_run_INSTR_JUMP_NOT_EMPTY); | |
4022 | break; | |
4023 | } | |
4024 | ||
4025 | case INSTR_JUMP_LT: | |
4026 | { | |
4027 | PROFILE_ENTER_LOCAL(function_run_INSTR_JUMP_LT); | |
4028 | r = stack_pop( s ); | |
4029 | l = stack_pop( s ); | |
4030 | if ( list_cmp( l, r ) < 0 ) code += code->arg; | |
4031 | list_free( l ); | |
4032 | list_free( r ); | |
4033 | PROFILE_EXIT_LOCAL(function_run_INSTR_JUMP_LT); | |
4034 | break; | |
4035 | } | |
4036 | ||
4037 | case INSTR_JUMP_LE: | |
4038 | { | |
4039 | PROFILE_ENTER_LOCAL(function_run_INSTR_JUMP_LE); | |
4040 | r = stack_pop( s ); | |
4041 | l = stack_pop( s ); | |
4042 | if ( list_cmp( l, r ) <= 0 ) code += code->arg; | |
4043 | list_free( l ); | |
4044 | list_free( r ); | |
4045 | PROFILE_EXIT_LOCAL(function_run_INSTR_JUMP_LE); | |
4046 | break; | |
4047 | } | |
4048 | ||
4049 | case INSTR_JUMP_GT: | |
4050 | { | |
4051 | PROFILE_ENTER_LOCAL(function_run_INSTR_JUMP_GT); | |
4052 | r = stack_pop( s ); | |
4053 | l = stack_pop( s ); | |
4054 | if ( list_cmp( l, r ) > 0 ) code += code->arg; | |
4055 | list_free( l ); | |
4056 | list_free( r ); | |
4057 | PROFILE_EXIT_LOCAL(function_run_INSTR_JUMP_GT); | |
4058 | break; | |
4059 | } | |
4060 | ||
4061 | case INSTR_JUMP_GE: | |
4062 | { | |
4063 | PROFILE_ENTER_LOCAL(function_run_INSTR_JUMP_GE); | |
4064 | r = stack_pop( s ); | |
4065 | l = stack_pop( s ); | |
4066 | if ( list_cmp( l, r ) >= 0 ) code += code->arg; | |
4067 | list_free( l ); | |
4068 | list_free( r ); | |
4069 | PROFILE_EXIT_LOCAL(function_run_INSTR_JUMP_GE); | |
4070 | break; | |
4071 | } | |
4072 | ||
4073 | case INSTR_JUMP_EQ: | |
4074 | { | |
4075 | PROFILE_ENTER_LOCAL(function_run_INSTR_JUMP_EQ); | |
4076 | r = stack_pop( s ); | |
4077 | l = stack_pop( s ); | |
4078 | if ( list_cmp( l, r ) == 0 ) code += code->arg; | |
4079 | list_free( l ); | |
4080 | list_free( r ); | |
4081 | PROFILE_EXIT_LOCAL(function_run_INSTR_JUMP_EQ); | |
4082 | break; | |
4083 | } | |
4084 | ||
4085 | case INSTR_JUMP_NE: | |
4086 | { | |
4087 | PROFILE_ENTER_LOCAL(function_run_INSTR_JUMP_NE); | |
4088 | r = stack_pop(s); | |
4089 | l = stack_pop(s); | |
4090 | if ( list_cmp(l, r) != 0 ) code += code->arg; | |
4091 | list_free(l); | |
4092 | list_free(r); | |
4093 | PROFILE_EXIT_LOCAL(function_run_INSTR_JUMP_NE); | |
4094 | break; | |
4095 | } | |
4096 | ||
4097 | case INSTR_JUMP_IN: | |
4098 | { | |
4099 | PROFILE_ENTER_LOCAL(function_run_INSTR_JUMP_IN); | |
4100 | r = stack_pop(s); | |
4101 | l = stack_pop(s); | |
4102 | if ( list_is_sublist( l, r ) ) code += code->arg; | |
4103 | list_free(l); | |
4104 | list_free(r); | |
4105 | PROFILE_EXIT_LOCAL(function_run_INSTR_JUMP_IN); | |
4106 | break; | |
4107 | } | |
4108 | ||
4109 | case INSTR_JUMP_NOT_IN: | |
4110 | { | |
4111 | PROFILE_ENTER_LOCAL(function_run_INSTR_JUMP_NOT_IN); | |
4112 | r = stack_pop( s ); | |
4113 | l = stack_pop( s ); | |
4114 | if ( !list_is_sublist( l, r ) ) code += code->arg; | |
4115 | list_free( l ); | |
4116 | list_free( r ); | |
4117 | PROFILE_EXIT_LOCAL(function_run_INSTR_JUMP_NOT_IN); | |
4118 | break; | |
4119 | } | |
4120 | ||
4121 | /* | |
4122 | * For | |
4123 | */ | |
4124 | ||
4125 | case INSTR_FOR_INIT: | |
4126 | { | |
4127 | PROFILE_ENTER_LOCAL(function_run_INSTR_FOR_INIT); | |
4128 | l = stack_top( s ); | |
4129 | *(LISTITER *)stack_allocate( s, sizeof( LISTITER ) ) = | |
4130 | list_begin( l ); | |
4131 | PROFILE_EXIT_LOCAL(function_run_INSTR_FOR_INIT); | |
4132 | break; | |
4133 | } | |
4134 | ||
4135 | case INSTR_FOR_LOOP: | |
4136 | { | |
4137 | PROFILE_ENTER_LOCAL(function_run_INSTR_FOR_LOOP); | |
4138 | LISTITER iter = *(LISTITER *)stack_get( s ); | |
4139 | stack_deallocate( s, sizeof( LISTITER ) ); | |
4140 | l = stack_top( s ); | |
4141 | if ( iter == list_end( l ) ) | |
4142 | { | |
4143 | list_free( stack_pop( s ) ); | |
4144 | code += code->arg; | |
4145 | } | |
4146 | else | |
4147 | { | |
4148 | r = list_new( object_copy( list_item( iter ) ) ); | |
4149 | iter = list_next( iter ); | |
4150 | *(LISTITER *)stack_allocate( s, sizeof( LISTITER ) ) = iter; | |
4151 | stack_push( s, r ); | |
4152 | } | |
4153 | PROFILE_EXIT_LOCAL(function_run_INSTR_FOR_LOOP); | |
4154 | break; | |
4155 | } | |
4156 | ||
4157 | case INSTR_FOR_POP: | |
4158 | { | |
4159 | PROFILE_ENTER_LOCAL(function_run_INSTR_FOR_POP); | |
4160 | stack_deallocate( s, sizeof( LISTITER ) ); | |
4161 | list_free( stack_pop( s ) ); | |
4162 | PROFILE_EXIT_LOCAL(function_run_INSTR_FOR_POP); | |
4163 | break; | |
4164 | } | |
4165 | ||
4166 | /* | |
4167 | * Switch | |
4168 | */ | |
4169 | ||
4170 | case INSTR_JUMP_NOT_GLOB: | |
4171 | { | |
4172 | PROFILE_ENTER_LOCAL(function_run_INSTR_JUMP_NOT_GLOB); | |
4173 | char const * pattern; | |
4174 | char const * match; | |
4175 | l = stack_pop( s ); | |
4176 | r = stack_top( s ); | |
4177 | pattern = list_empty( l ) ? "" : object_str( list_front( l ) ); | |
4178 | match = list_empty( r ) ? "" : object_str( list_front( r ) ); | |
4179 | if ( glob( pattern, match ) ) | |
4180 | code += code->arg; | |
4181 | else | |
4182 | list_free( stack_pop( s ) ); | |
4183 | list_free( l ); | |
4184 | PROFILE_EXIT_LOCAL(function_run_INSTR_JUMP_NOT_GLOB); | |
4185 | break; | |
4186 | } | |
4187 | ||
4188 | /* | |
4189 | * Return | |
4190 | */ | |
4191 | ||
4192 | case INSTR_SET_RESULT: | |
4193 | { | |
4194 | PROFILE_ENTER_LOCAL(function_run_INSTR_SET_RESULT); | |
4195 | list_free( result ); | |
4196 | if ( !code->arg ) | |
4197 | result = stack_pop( s ); | |
4198 | else | |
4199 | result = list_copy( stack_top( s ) ); | |
4200 | PROFILE_EXIT_LOCAL(function_run_INSTR_SET_RESULT); | |
4201 | break; | |
4202 | } | |
4203 | ||
4204 | case INSTR_PUSH_RESULT: | |
4205 | { | |
4206 | PROFILE_ENTER_LOCAL(function_run_INSTR_PUSH_RESULT); | |
4207 | stack_push( s, result ); | |
4208 | result = L0; | |
4209 | PROFILE_EXIT_LOCAL(function_run_INSTR_PUSH_RESULT); | |
4210 | break; | |
4211 | } | |
4212 | ||
4213 | case INSTR_RETURN: | |
4214 | { | |
4215 | PROFILE_ENTER_LOCAL(function_run_INSTR_RETURN); | |
4216 | if ( function_->formal_arguments ) | |
4217 | argument_list_pop( function_->formal_arguments, | |
4218 | function_->num_formal_arguments, frame, s ); | |
4219 | #ifndef NDEBUG | |
4220 | if ( !( saved_stack == s->data ) ) | |
4221 | { | |
4222 | frame->file = function->file; | |
4223 | frame->line = function->line; | |
4224 | backtrace_line( frame ); | |
4225 | out_printf( "error: stack check failed.\n" ); | |
4226 | backtrace( frame ); | |
4227 | assert( saved_stack == s->data ); | |
4228 | } | |
4229 | #endif | |
4230 | assert( saved_stack == s->data ); | |
b32b8144 | 4231 | debug_on_exit_function( function->base.rulename ); |
7c673cae FG |
4232 | PROFILE_EXIT_LOCAL(function_run_INSTR_RETURN); |
4233 | PROFILE_EXIT_LOCAL(function_run); | |
4234 | return result; | |
4235 | } | |
4236 | ||
4237 | /* | |
4238 | * Local variables | |
4239 | */ | |
4240 | ||
4241 | case INSTR_PUSH_LOCAL: | |
4242 | { | |
4243 | PROFILE_ENTER_LOCAL(function_run_INSTR_PUSH_LOCAL); | |
4244 | LIST * value = stack_pop( s ); | |
4245 | stack_push( s, function_swap_variable( function, frame, code->arg, | |
4246 | value ) ); | |
4247 | PROFILE_EXIT_LOCAL(function_run_INSTR_PUSH_LOCAL); | |
4248 | break; | |
4249 | } | |
4250 | ||
4251 | case INSTR_POP_LOCAL: | |
4252 | { | |
4253 | PROFILE_ENTER_LOCAL(function_run_INSTR_POP_LOCAL); | |
4254 | function_set_variable( function, frame, code->arg, stack_pop( s ) ); | |
4255 | PROFILE_EXIT_LOCAL(function_run_INSTR_POP_LOCAL); | |
4256 | break; | |
4257 | } | |
4258 | ||
4259 | case INSTR_PUSH_LOCAL_FIXED: | |
4260 | { | |
4261 | PROFILE_ENTER_LOCAL(function_run_INSTR_PUSH_LOCAL_FIXED); | |
4262 | LIST * value = stack_pop( s ); | |
4263 | LIST * * ptr = &frame->module->fixed_variables[ code->arg ]; | |
4264 | assert( code->arg < frame->module->num_fixed_variables ); | |
4265 | stack_push( s, *ptr ); | |
4266 | *ptr = value; | |
4267 | PROFILE_EXIT_LOCAL(function_run_INSTR_PUSH_LOCAL_FIXED); | |
4268 | break; | |
4269 | } | |
4270 | ||
4271 | case INSTR_POP_LOCAL_FIXED: | |
4272 | { | |
4273 | PROFILE_ENTER_LOCAL(function_run_INSTR_POP_LOCAL_FIXED); | |
4274 | LIST * value = stack_pop( s ); | |
4275 | LIST * * ptr = &frame->module->fixed_variables[ code->arg ]; | |
4276 | assert( code->arg < frame->module->num_fixed_variables ); | |
4277 | list_free( *ptr ); | |
4278 | *ptr = value; | |
4279 | PROFILE_EXIT_LOCAL(function_run_INSTR_POP_LOCAL_FIXED); | |
4280 | break; | |
4281 | } | |
4282 | ||
4283 | case INSTR_PUSH_LOCAL_GROUP: | |
4284 | { | |
4285 | PROFILE_ENTER_LOCAL(function_run_INSTR_PUSH_LOCAL_GROUP); | |
4286 | LIST * const value = stack_pop( s ); | |
4287 | LISTITER iter; | |
4288 | LISTITER end; | |
4289 | l = stack_pop( s ); | |
4290 | for ( iter = list_begin( l ), end = list_end( l ); iter != end; | |
4291 | iter = list_next( iter ) ) | |
4292 | stack_push( s, function_swap_named_variable( function, frame, | |
4293 | list_item( iter ), list_copy( value ) ) ); | |
4294 | list_free( value ); | |
4295 | stack_push( s, l ); | |
4296 | PROFILE_EXIT_LOCAL(function_run_INSTR_PUSH_LOCAL_GROUP); | |
4297 | break; | |
4298 | } | |
4299 | ||
4300 | case INSTR_POP_LOCAL_GROUP: | |
4301 | { | |
4302 | PROFILE_ENTER_LOCAL(function_run_INSTR_POP_LOCAL_GROUP); | |
4303 | LISTITER iter; | |
4304 | LISTITER end; | |
4305 | r = stack_pop( s ); | |
4306 | l = list_reverse( r ); | |
4307 | list_free( r ); | |
4308 | for ( iter = list_begin( l ), end = list_end( l ); iter != end; | |
4309 | iter = list_next( iter ) ) | |
4310 | function_set_named_variable( function, frame, list_item( iter ), | |
4311 | stack_pop( s ) ); | |
4312 | list_free( l ); | |
4313 | PROFILE_EXIT_LOCAL(function_run_INSTR_POP_LOCAL_GROUP); | |
4314 | break; | |
4315 | } | |
4316 | ||
4317 | /* | |
4318 | * on $(TARGET) variables | |
4319 | */ | |
4320 | ||
4321 | case INSTR_PUSH_ON: | |
4322 | { | |
4323 | PROFILE_ENTER_LOCAL(function_run_INSTR_PUSH_ON); | |
4324 | LIST * targets = stack_top( s ); | |
4325 | if ( !list_empty( targets ) ) | |
4326 | { | |
4327 | /* FIXME: push the state onto the stack instead of using | |
4328 | * pushsettings. | |
4329 | */ | |
4330 | TARGET * t = bindtarget( list_front( targets ) ); | |
4331 | pushsettings( frame->module, t->settings ); | |
4332 | } | |
4333 | else | |
4334 | { | |
4335 | /* [ on $(TARGET) ... ] is ignored if $(TARGET) is empty. */ | |
4336 | list_free( stack_pop( s ) ); | |
4337 | stack_push( s, L0 ); | |
4338 | code += code->arg; | |
4339 | } | |
4340 | PROFILE_EXIT_LOCAL(function_run_INSTR_PUSH_ON); | |
4341 | break; | |
4342 | } | |
4343 | ||
4344 | case INSTR_POP_ON: | |
4345 | { | |
4346 | PROFILE_ENTER_LOCAL(function_run_INSTR_POP_ON); | |
4347 | LIST * result = stack_pop( s ); | |
4348 | LIST * targets = stack_pop( s ); | |
4349 | if ( !list_empty( targets ) ) | |
4350 | { | |
4351 | TARGET * t = bindtarget( list_front( targets ) ); | |
4352 | popsettings( frame->module, t->settings ); | |
4353 | } | |
4354 | list_free( targets ); | |
4355 | stack_push( s, result ); | |
4356 | PROFILE_EXIT_LOCAL(function_run_INSTR_POP_ON); | |
4357 | break; | |
4358 | } | |
4359 | ||
4360 | case INSTR_SET_ON: | |
4361 | { | |
4362 | PROFILE_ENTER_LOCAL(function_run_INSTR_SET_ON); | |
4363 | LIST * targets = stack_pop( s ); | |
4364 | LIST * value = stack_pop( s ); | |
4365 | LIST * vars = stack_pop( s ); | |
4366 | LISTITER iter = list_begin( targets ); | |
4367 | LISTITER const end = list_end( targets ); | |
4368 | for ( ; iter != end; iter = list_next( iter ) ) | |
4369 | { | |
4370 | TARGET * t = bindtarget( list_item( iter ) ); | |
4371 | LISTITER vars_iter = list_begin( vars ); | |
4372 | LISTITER const vars_end = list_end( vars ); | |
4373 | for ( ; vars_iter != vars_end; vars_iter = list_next( vars_iter | |
4374 | ) ) | |
4375 | t->settings = addsettings( t->settings, VAR_SET, list_item( | |
4376 | vars_iter ), list_copy( value ) ); | |
4377 | } | |
4378 | list_free( vars ); | |
4379 | list_free( targets ); | |
4380 | stack_push( s, value ); | |
4381 | PROFILE_EXIT_LOCAL(function_run_INSTR_SET_ON); | |
4382 | break; | |
4383 | } | |
4384 | ||
4385 | case INSTR_APPEND_ON: | |
4386 | { | |
4387 | PROFILE_ENTER_LOCAL(function_run_INSTR_APPEND_ON); | |
4388 | LIST * targets = stack_pop( s ); | |
4389 | LIST * value = stack_pop( s ); | |
4390 | LIST * vars = stack_pop( s ); | |
4391 | LISTITER iter = list_begin( targets ); | |
4392 | LISTITER const end = list_end( targets ); | |
4393 | for ( ; iter != end; iter = list_next( iter ) ) | |
4394 | { | |
4395 | TARGET * const t = bindtarget( list_item( iter ) ); | |
4396 | LISTITER vars_iter = list_begin( vars ); | |
4397 | LISTITER const vars_end = list_end( vars ); | |
4398 | for ( ; vars_iter != vars_end; vars_iter = list_next( vars_iter | |
4399 | ) ) | |
4400 | t->settings = addsettings( t->settings, VAR_APPEND, | |
4401 | list_item( vars_iter ), list_copy( value ) ); | |
4402 | } | |
4403 | list_free( vars ); | |
4404 | list_free( targets ); | |
4405 | stack_push( s, value ); | |
4406 | PROFILE_EXIT_LOCAL(function_run_INSTR_APPEND_ON); | |
4407 | break; | |
4408 | } | |
4409 | ||
4410 | case INSTR_DEFAULT_ON: | |
4411 | { | |
4412 | PROFILE_ENTER_LOCAL(function_run_INSTR_DEFAULT_ON); | |
4413 | LIST * targets = stack_pop( s ); | |
4414 | LIST * value = stack_pop( s ); | |
4415 | LIST * vars = stack_pop( s ); | |
4416 | LISTITER iter = list_begin( targets ); | |
4417 | LISTITER const end = list_end( targets ); | |
4418 | for ( ; iter != end; iter = list_next( iter ) ) | |
4419 | { | |
4420 | TARGET * t = bindtarget( list_item( iter ) ); | |
4421 | LISTITER vars_iter = list_begin( vars ); | |
4422 | LISTITER const vars_end = list_end( vars ); | |
4423 | for ( ; vars_iter != vars_end; vars_iter = list_next( vars_iter | |
4424 | ) ) | |
4425 | t->settings = addsettings( t->settings, VAR_DEFAULT, | |
4426 | list_item( vars_iter ), list_copy( value ) ); | |
4427 | } | |
4428 | list_free( vars ); | |
4429 | list_free( targets ); | |
4430 | stack_push( s, value ); | |
4431 | PROFILE_EXIT_LOCAL(function_run_INSTR_DEFAULT_ON); | |
4432 | break; | |
4433 | } | |
4434 | ||
4435 | /* [ on $(target) return $(variable) ] */ | |
4436 | case INSTR_GET_ON: | |
4437 | { | |
4438 | PROFILE_ENTER_LOCAL(function_run_INSTR_GET_ON); | |
4439 | LIST * targets = stack_pop( s ); | |
4440 | LIST * result = L0; | |
4441 | if ( !list_empty( targets ) ) | |
4442 | { | |
4443 | OBJECT * varname = function->constants[ code->arg ]; | |
4444 | TARGET * t = bindtarget( list_front( targets ) ); | |
4445 | SETTINGS * s = t->settings; | |
4446 | int found = 0; | |
4447 | for ( ; s != 0; s = s->next ) | |
4448 | { | |
4449 | if ( object_equal( s->symbol, varname ) ) | |
4450 | { | |
4451 | result = s->value; | |
4452 | found = 1; | |
4453 | break; | |
4454 | } | |
4455 | } | |
4456 | if ( !found ) | |
4457 | { | |
4458 | result = var_get( frame->module, varname ) ; | |
4459 | } | |
4460 | } | |
4461 | list_free( targets ); | |
4462 | stack_push( s, list_copy( result ) ); | |
4463 | PROFILE_EXIT_LOCAL(function_run_INSTR_GET_ON); | |
4464 | break; | |
4465 | } | |
4466 | ||
4467 | /* | |
4468 | * Variable setting | |
4469 | */ | |
4470 | ||
4471 | case INSTR_SET: | |
4472 | { | |
4473 | PROFILE_ENTER_LOCAL(function_run_INSTR_SET); | |
4474 | function_set_variable( function, frame, code->arg, | |
4475 | stack_pop( s ) ); | |
4476 | PROFILE_EXIT_LOCAL(function_run_INSTR_SET); | |
4477 | break; | |
4478 | } | |
4479 | ||
4480 | case INSTR_APPEND: | |
4481 | { | |
4482 | PROFILE_ENTER_LOCAL(function_run_INSTR_APPEND); | |
4483 | function_append_variable( function, frame, code->arg, | |
4484 | stack_pop( s ) ); | |
4485 | PROFILE_EXIT_LOCAL(function_run_INSTR_APPEND); | |
4486 | break; | |
4487 | } | |
4488 | ||
4489 | case INSTR_DEFAULT: | |
4490 | { | |
4491 | PROFILE_ENTER_LOCAL(function_run_INSTR_DEFAULT); | |
4492 | function_default_variable( function, frame, code->arg, | |
4493 | stack_pop( s ) ); | |
4494 | PROFILE_EXIT_LOCAL(function_run_INSTR_DEFAULT); | |
4495 | break; | |
4496 | } | |
4497 | ||
4498 | case INSTR_SET_FIXED: | |
4499 | { | |
4500 | PROFILE_ENTER_LOCAL(function_run_INSTR_SET_FIXED); | |
4501 | LIST * * ptr = &frame->module->fixed_variables[ code->arg ]; | |
4502 | assert( code->arg < frame->module->num_fixed_variables ); | |
4503 | list_free( *ptr ); | |
4504 | *ptr = stack_pop( s ); | |
4505 | PROFILE_EXIT_LOCAL(function_run_INSTR_SET_FIXED); | |
4506 | break; | |
4507 | } | |
4508 | ||
4509 | case INSTR_APPEND_FIXED: | |
4510 | { | |
4511 | PROFILE_ENTER_LOCAL(function_run_INSTR_APPEND_FIXED); | |
4512 | LIST * * ptr = &frame->module->fixed_variables[ code->arg ]; | |
4513 | assert( code->arg < frame->module->num_fixed_variables ); | |
4514 | *ptr = list_append( *ptr, stack_pop( s ) ); | |
4515 | PROFILE_EXIT_LOCAL(function_run_INSTR_APPEND_FIXED); | |
4516 | break; | |
4517 | } | |
4518 | ||
4519 | case INSTR_DEFAULT_FIXED: | |
4520 | { | |
4521 | PROFILE_ENTER_LOCAL(function_run_INSTR_DEFAULT_FIXED); | |
4522 | LIST * * ptr = &frame->module->fixed_variables[ code->arg ]; | |
4523 | LIST * value = stack_pop( s ); | |
4524 | assert( code->arg < frame->module->num_fixed_variables ); | |
4525 | if ( list_empty( *ptr ) ) | |
4526 | *ptr = value; | |
4527 | else | |
4528 | list_free( value ); | |
4529 | PROFILE_EXIT_LOCAL(function_run_INSTR_DEFAULT_FIXED); | |
4530 | break; | |
4531 | } | |
4532 | ||
4533 | case INSTR_SET_GROUP: | |
4534 | { | |
4535 | PROFILE_ENTER_LOCAL(function_run_INSTR_SET_GROUP); | |
4536 | LIST * value = stack_pop( s ); | |
4537 | LIST * vars = stack_pop( s ); | |
4538 | LISTITER iter = list_begin( vars ); | |
4539 | LISTITER const end = list_end( vars ); | |
4540 | for ( ; iter != end; iter = list_next( iter ) ) | |
4541 | function_set_named_variable( function, frame, list_item( iter ), | |
4542 | list_copy( value ) ); | |
4543 | list_free( vars ); | |
4544 | list_free( value ); | |
4545 | PROFILE_EXIT_LOCAL(function_run_INSTR_SET_GROUP); | |
4546 | break; | |
4547 | } | |
4548 | ||
4549 | case INSTR_APPEND_GROUP: | |
4550 | { | |
4551 | PROFILE_ENTER_LOCAL(function_run_INSTR_APPEND_GROUP); | |
4552 | LIST * value = stack_pop( s ); | |
4553 | LIST * vars = stack_pop( s ); | |
4554 | LISTITER iter = list_begin( vars ); | |
4555 | LISTITER const end = list_end( vars ); | |
4556 | for ( ; iter != end; iter = list_next( iter ) ) | |
4557 | function_append_named_variable( function, frame, list_item( iter | |
4558 | ), list_copy( value ) ); | |
4559 | list_free( vars ); | |
4560 | list_free( value ); | |
4561 | PROFILE_EXIT_LOCAL(function_run_INSTR_APPEND_GROUP); | |
4562 | break; | |
4563 | } | |
4564 | ||
4565 | case INSTR_DEFAULT_GROUP: | |
4566 | { | |
4567 | PROFILE_ENTER_LOCAL(function_run_INSTR_DEFAULT_GROUP); | |
4568 | LIST * value = stack_pop( s ); | |
4569 | LIST * vars = stack_pop( s ); | |
4570 | LISTITER iter = list_begin( vars ); | |
4571 | LISTITER const end = list_end( vars ); | |
4572 | for ( ; iter != end; iter = list_next( iter ) ) | |
4573 | function_default_named_variable( function, frame, list_item( | |
4574 | iter ), list_copy( value ) ); | |
4575 | list_free( vars ); | |
4576 | list_free( value ); | |
4577 | PROFILE_EXIT_LOCAL(function_run_INSTR_DEFAULT_GROUP); | |
4578 | break; | |
4579 | } | |
4580 | ||
4581 | /* | |
4582 | * Rules | |
4583 | */ | |
4584 | ||
4585 | case INSTR_CALL_RULE: | |
4586 | { | |
4587 | PROFILE_ENTER_LOCAL(function_run_INSTR_CALL_RULE); | |
4588 | char const * unexpanded = object_str( function_get_constant( | |
4589 | function, code[ 1 ].op_code ) ); | |
4590 | LIST * result = function_call_rule( function, frame, s, code->arg, | |
4591 | unexpanded, function->file, code[ 1 ].arg ); | |
4592 | stack_push( s, result ); | |
4593 | ++code; | |
4594 | PROFILE_EXIT_LOCAL(function_run_INSTR_CALL_RULE); | |
4595 | break; | |
4596 | } | |
4597 | ||
4598 | case INSTR_CALL_MEMBER_RULE: | |
4599 | { | |
4600 | PROFILE_ENTER_LOCAL(function_run_INSTR_CALL_MEMBER_RULE); | |
4601 | OBJECT * rule_name = function_get_constant( function, code[1].op_code ); | |
4602 | LIST * result = function_call_member_rule( function, frame, s, code->arg, rule_name, function->file, code[1].arg ); | |
4603 | stack_push( s, result ); | |
4604 | ++code; | |
4605 | PROFILE_EXIT_LOCAL(function_run_INSTR_CALL_MEMBER_RULE); | |
4606 | break; | |
4607 | } | |
4608 | ||
4609 | case INSTR_RULE: | |
4610 | { | |
4611 | PROFILE_ENTER_LOCAL(function_run_INSTR_RULE); | |
4612 | function_set_rule( function, frame, s, code->arg ); | |
4613 | PROFILE_EXIT_LOCAL(function_run_INSTR_RULE); | |
4614 | break; | |
4615 | } | |
4616 | ||
4617 | case INSTR_ACTIONS: | |
4618 | { | |
4619 | PROFILE_ENTER_LOCAL(function_run_INSTR_ACTIONS); | |
4620 | function_set_actions( function, frame, s, code->arg ); | |
4621 | PROFILE_EXIT_LOCAL(function_run_INSTR_ACTIONS); | |
4622 | break; | |
4623 | } | |
4624 | ||
4625 | /* | |
4626 | * Variable expansion | |
4627 | */ | |
4628 | ||
4629 | case INSTR_APPLY_MODIFIERS: | |
4630 | { | |
4631 | PROFILE_ENTER_LOCAL(function_run_INSTR_APPLY_MODIFIERS); | |
4632 | int n; | |
4633 | int i; | |
4634 | l = stack_pop( s ); | |
4635 | n = expand_modifiers( s, code->arg ); | |
4636 | stack_push( s, l ); | |
4637 | l = apply_modifiers( s, n ); | |
4638 | list_free( stack_pop( s ) ); | |
4639 | stack_deallocate( s, n * sizeof( VAR_EDITS ) ); | |
4640 | for ( i = 0; i < code->arg; ++i ) | |
4641 | list_free( stack_pop( s ) ); /* pop modifiers */ | |
4642 | stack_push( s, l ); | |
4643 | PROFILE_EXIT_LOCAL(function_run_INSTR_APPLY_MODIFIERS); | |
4644 | break; | |
4645 | } | |
4646 | ||
4647 | case INSTR_APPLY_INDEX: | |
4648 | { | |
4649 | PROFILE_ENTER_LOCAL(function_run_INSTR_APPLY_INDEX); | |
4650 | l = apply_subscript( s ); | |
4651 | list_free( stack_pop( s ) ); | |
4652 | list_free( stack_pop( s ) ); | |
4653 | stack_push( s, l ); | |
4654 | PROFILE_EXIT_LOCAL(function_run_INSTR_APPLY_INDEX); | |
4655 | break; | |
4656 | } | |
4657 | ||
4658 | case INSTR_APPLY_INDEX_MODIFIERS: | |
4659 | { | |
4660 | PROFILE_ENTER_LOCAL(function_run_INSTR_APPLY_INDEX_MODIFIERS); | |
4661 | int i; | |
4662 | int n; | |
4663 | l = stack_pop( s ); | |
4664 | r = stack_pop( s ); | |
4665 | n = expand_modifiers( s, code->arg ); | |
4666 | stack_push( s, r ); | |
4667 | stack_push( s, l ); | |
4668 | l = apply_subscript_and_modifiers( s, n ); | |
4669 | list_free( stack_pop( s ) ); | |
4670 | list_free( stack_pop( s ) ); | |
4671 | stack_deallocate( s, n * sizeof( VAR_EDITS ) ); | |
4672 | for ( i = 0; i < code->arg; ++i ) | |
4673 | list_free( stack_pop( s ) ); /* pop modifiers */ | |
4674 | stack_push( s, l ); | |
4675 | PROFILE_EXIT_LOCAL(function_run_INSTR_APPLY_INDEX_MODIFIERS); | |
4676 | break; | |
4677 | } | |
4678 | ||
4679 | case INSTR_APPLY_MODIFIERS_GROUP: | |
4680 | { | |
4681 | PROFILE_ENTER_LOCAL(function_run_INSTR_APPLY_MODIFIERS_GROUP); | |
4682 | int i; | |
4683 | LIST * const vars = stack_pop( s ); | |
4684 | int const n = expand_modifiers( s, code->arg ); | |
4685 | LIST * result = L0; | |
4686 | LISTITER iter = list_begin( vars ); | |
4687 | LISTITER const end = list_end( vars ); | |
4688 | for ( ; iter != end; iter = list_next( iter ) ) | |
4689 | { | |
4690 | stack_push( s, function_get_named_variable( function, frame, | |
4691 | list_item( iter ) ) ); | |
4692 | result = list_append( result, apply_modifiers( s, n ) ); | |
4693 | list_free( stack_pop( s ) ); | |
4694 | } | |
4695 | list_free( vars ); | |
4696 | stack_deallocate( s, n * sizeof( VAR_EDITS ) ); | |
4697 | for ( i = 0; i < code->arg; ++i ) | |
4698 | list_free( stack_pop( s ) ); /* pop modifiers */ | |
4699 | stack_push( s, result ); | |
4700 | PROFILE_EXIT_LOCAL(function_run_INSTR_APPLY_MODIFIERS_GROUP); | |
4701 | break; | |
4702 | } | |
4703 | ||
4704 | case INSTR_APPLY_INDEX_GROUP: | |
4705 | { | |
4706 | PROFILE_ENTER_LOCAL(function_run_INSTR_APPLY_INDEX_GROUP); | |
4707 | LIST * vars = stack_pop( s ); | |
4708 | LIST * result = L0; | |
4709 | LISTITER iter = list_begin( vars ); | |
4710 | LISTITER const end = list_end( vars ); | |
4711 | for ( ; iter != end; iter = list_next( iter ) ) | |
4712 | { | |
4713 | stack_push( s, function_get_named_variable( function, frame, | |
4714 | list_item( iter ) ) ); | |
4715 | result = list_append( result, apply_subscript( s ) ); | |
4716 | list_free( stack_pop( s ) ); | |
4717 | } | |
4718 | list_free( vars ); | |
4719 | list_free( stack_pop( s ) ); | |
4720 | stack_push( s, result ); | |
4721 | PROFILE_EXIT_LOCAL(function_run_INSTR_APPLY_INDEX_GROUP); | |
4722 | break; | |
4723 | } | |
4724 | ||
4725 | case INSTR_APPLY_INDEX_MODIFIERS_GROUP: | |
4726 | { | |
4727 | PROFILE_ENTER_LOCAL(function_run_INSTR_APPLY_INDEX_MODIFIERS_GROUP); | |
4728 | int i; | |
4729 | LIST * const vars = stack_pop( s ); | |
4730 | LIST * const r = stack_pop( s ); | |
4731 | int const n = expand_modifiers( s, code->arg ); | |
4732 | LIST * result = L0; | |
4733 | LISTITER iter = list_begin( vars ); | |
4734 | LISTITER const end = list_end( vars ); | |
4735 | stack_push( s, r ); | |
4736 | for ( ; iter != end; iter = list_next( iter ) ) | |
4737 | { | |
4738 | stack_push( s, function_get_named_variable( function, frame, | |
4739 | list_item( iter ) ) ); | |
4740 | result = list_append( result, apply_subscript_and_modifiers( s, | |
4741 | n ) ); | |
4742 | list_free( stack_pop( s ) ); | |
4743 | } | |
4744 | list_free( stack_pop( s ) ); | |
4745 | list_free( vars ); | |
4746 | stack_deallocate( s, n * sizeof( VAR_EDITS ) ); | |
4747 | for ( i = 0; i < code->arg; ++i ) | |
4748 | list_free( stack_pop( s ) ); /* pop modifiers */ | |
4749 | stack_push( s, result ); | |
4750 | PROFILE_EXIT_LOCAL(function_run_INSTR_APPLY_INDEX_MODIFIERS_GROUP); | |
4751 | break; | |
4752 | } | |
4753 | ||
4754 | case INSTR_COMBINE_STRINGS: | |
4755 | { | |
4756 | PROFILE_ENTER_LOCAL(function_run_INSTR_COMBINE_STRINGS); | |
4757 | size_t const buffer_size = code->arg * sizeof( expansion_item ); | |
4758 | LIST * * const stack_pos = stack_get( s ); | |
4759 | expansion_item * items = stack_allocate( s, buffer_size ); | |
4760 | LIST * result; | |
4761 | int i; | |
4762 | for ( i = 0; i < code->arg; ++i ) | |
4763 | items[ i ].values = stack_pos[ i ]; | |
4764 | result = expand( items, code->arg ); | |
4765 | stack_deallocate( s, buffer_size ); | |
4766 | for ( i = 0; i < code->arg; ++i ) | |
4767 | list_free( stack_pop( s ) ); | |
4768 | stack_push( s, result ); | |
4769 | PROFILE_EXIT_LOCAL(function_run_INSTR_COMBINE_STRINGS); | |
4770 | break; | |
4771 | } | |
4772 | ||
4773 | case INSTR_GET_GRIST: | |
4774 | { | |
4775 | PROFILE_ENTER_LOCAL(function_run_INSTR_GET_GRIST); | |
4776 | LIST * vals = stack_pop( s ); | |
4777 | LIST * result = L0; | |
4778 | LISTITER iter, end; | |
4779 | ||
4780 | for ( iter = list_begin( vals ), end = list_end( vals ); iter != end; ++iter ) | |
4781 | { | |
4782 | OBJECT * new_object; | |
4783 | const char * value = object_str( list_item( iter ) ); | |
4784 | const char * p; | |
4785 | if ( value[ 0 ] == '<' && ( p = strchr( value, '>' ) ) ) | |
4786 | { | |
4787 | if( p[ 1 ] ) | |
4788 | new_object = object_new_range( value, p - value + 1 ); | |
4789 | else | |
4790 | new_object = object_copy( list_item( iter ) ); | |
4791 | } | |
4792 | else | |
4793 | { | |
4794 | new_object = object_copy( constant_empty ); | |
4795 | } | |
4796 | result = list_push_back( result, new_object ); | |
4797 | } | |
4798 | ||
4799 | list_free( vals ); | |
4800 | stack_push( s, result ); | |
4801 | PROFILE_EXIT_LOCAL(function_run_INSTR_GET_GRIST); | |
4802 | break; | |
4803 | } | |
4804 | ||
4805 | case INSTR_INCLUDE: | |
4806 | { | |
4807 | PROFILE_ENTER_LOCAL(function_run_INSTR_INCLUDE); | |
4808 | LIST * nt = stack_pop( s ); | |
4809 | if ( !list_empty( nt ) ) | |
4810 | { | |
4811 | TARGET * const t = bindtarget( list_front( nt ) ); | |
4812 | list_free( nt ); | |
4813 | ||
4814 | /* DWA 2001/10/22 - Perforce Jam cleared the arguments here, | |
4815 | * which prevented an included file from being treated as part | |
4816 | * of the body of a rule. I did not see any reason to do that, | |
4817 | * so I lifted the restriction. | |
4818 | */ | |
4819 | ||
4820 | /* Bind the include file under the influence of "on-target" | |
4821 | * variables. Though they are targets, include files are not | |
4822 | * built with make(). | |
4823 | */ | |
4824 | ||
4825 | pushsettings( root_module(), t->settings ); | |
4826 | /* We do not expect that a file to be included is generated by | |
4827 | * some action. Therefore, pass 0 as third argument. If the name | |
4828 | * resolves to a directory, let it error out. | |
4829 | */ | |
4830 | object_free( t->boundname ); | |
4831 | t->boundname = search( t->name, &t->time, 0, 0 ); | |
4832 | popsettings( root_module(), t->settings ); | |
4833 | ||
4834 | parse_file( t->boundname, frame ); | |
b32b8144 FG |
4835 | #ifdef JAM_DEBUGGER |
4836 | frame->function = function_; | |
4837 | #endif | |
7c673cae FG |
4838 | } |
4839 | PROFILE_EXIT_LOCAL(function_run_INSTR_INCLUDE); | |
4840 | break; | |
4841 | } | |
4842 | ||
4843 | /* | |
4844 | * Classes and modules | |
4845 | */ | |
4846 | ||
4847 | case INSTR_PUSH_MODULE: | |
4848 | { | |
4849 | PROFILE_ENTER_LOCAL(function_run_INSTR_PUSH_MODULE); | |
4850 | LIST * const module_name = stack_pop( s ); | |
4851 | module_t * const outer_module = frame->module; | |
4852 | frame->module = !list_empty( module_name ) | |
4853 | ? bindmodule( list_front( module_name ) ) | |
4854 | : root_module(); | |
4855 | list_free( module_name ); | |
4856 | *(module_t * *)stack_allocate( s, sizeof( module_t * ) ) = | |
4857 | outer_module; | |
4858 | PROFILE_EXIT_LOCAL(function_run_INSTR_PUSH_MODULE); | |
4859 | break; | |
4860 | } | |
4861 | ||
4862 | case INSTR_POP_MODULE: | |
4863 | { | |
4864 | PROFILE_ENTER_LOCAL(function_run_INSTR_POP_MODULE); | |
4865 | module_t * const outer_module = *(module_t * *)stack_get( s ); | |
4866 | stack_deallocate( s, sizeof( module_t * ) ); | |
4867 | frame->module = outer_module; | |
4868 | PROFILE_EXIT_LOCAL(function_run_INSTR_POP_MODULE); | |
4869 | break; | |
4870 | } | |
4871 | ||
4872 | case INSTR_CLASS: | |
4873 | { | |
4874 | PROFILE_ENTER_LOCAL(function_run_INSTR_CLASS); | |
4875 | LIST * bases = stack_pop( s ); | |
4876 | LIST * name = stack_pop( s ); | |
4877 | OBJECT * class_module = make_class_module( name, bases, frame ); | |
4878 | ||
4879 | module_t * const outer_module = frame->module; | |
4880 | frame->module = bindmodule( class_module ); | |
4881 | object_free( class_module ); | |
4882 | ||
4883 | *(module_t * *)stack_allocate( s, sizeof( module_t * ) ) = | |
4884 | outer_module; | |
4885 | PROFILE_EXIT_LOCAL(function_run_INSTR_CLASS); | |
4886 | break; | |
4887 | } | |
4888 | ||
4889 | case INSTR_BIND_MODULE_VARIABLES: | |
4890 | { | |
4891 | PROFILE_ENTER_LOCAL(function_run_INSTR_BIND_MODULE_VARIABLES); | |
4892 | module_bind_variables( frame->module ); | |
4893 | PROFILE_EXIT_LOCAL(function_run_INSTR_BIND_MODULE_VARIABLES); | |
4894 | break; | |
4895 | } | |
4896 | ||
4897 | case INSTR_APPEND_STRINGS: | |
4898 | { | |
4899 | PROFILE_ENTER_LOCAL(function_run_INSTR_APPEND_STRINGS); | |
4900 | string buf[ 1 ]; | |
4901 | string_new( buf ); | |
4902 | combine_strings( s, code->arg, buf ); | |
4903 | stack_push( s, list_new( object_new( buf->value ) ) ); | |
4904 | string_free( buf ); | |
4905 | PROFILE_EXIT_LOCAL(function_run_INSTR_APPEND_STRINGS); | |
4906 | break; | |
4907 | } | |
4908 | ||
4909 | case INSTR_WRITE_FILE: | |
4910 | { | |
4911 | PROFILE_ENTER_LOCAL(function_run_INSTR_WRITE_FILE); | |
4912 | string buf[ 1 ]; | |
4913 | char const * out; | |
4914 | OBJECT * tmp_filename = 0; | |
4915 | int out_debug = DEBUG_EXEC ? 1 : 0; | |
4916 | FILE * out_file = 0; | |
4917 | string_new( buf ); | |
4918 | combine_strings( s, code->arg, buf ); | |
4919 | out = object_str( list_front( stack_top( s ) ) ); | |
4920 | ||
4921 | /* For stdout/stderr we will create a temp file and generate a | |
4922 | * command that outputs the content as needed. | |
4923 | */ | |
4924 | if ( ( strcmp( "STDOUT", out ) == 0 ) || | |
4925 | ( strcmp( "STDERR", out ) == 0 ) ) | |
4926 | { | |
4927 | int err_redir = strcmp( "STDERR", out ) == 0; | |
4928 | string result[ 1 ]; | |
4929 | ||
4930 | tmp_filename = path_tmpfile(); | |
4931 | ||
4932 | /* Construct os-specific cat command. */ | |
4933 | { | |
4934 | char * command = "cat"; | |
4935 | char * quote = "\""; | |
4936 | char * redirect = "1>&2"; | |
4937 | ||
4938 | #ifdef OS_NT | |
4939 | command = "type"; | |
4940 | quote = "\""; | |
4941 | #elif defined( OS_VMS ) | |
4942 | command = "pipe type"; | |
4943 | quote = ""; | |
4944 | ||
4945 | /* Get tmp file name is os-format. */ | |
4946 | { | |
4947 | string os_filename[ 1 ]; | |
4948 | ||
4949 | string_new( os_filename ); | |
4950 | path_translate_to_os( object_str( tmp_filename ), os_filename ); | |
4951 | object_free( tmp_filename ); | |
4952 | tmp_filename = object_new( os_filename->value ); | |
4953 | string_free( os_filename ); | |
4954 | } | |
4955 | #endif | |
4956 | ||
4957 | string_new( result ); | |
4958 | string_append( result, command ); | |
4959 | string_append( result, " " ); | |
4960 | string_append( result, quote ); | |
4961 | string_append( result, object_str( tmp_filename ) ); | |
4962 | string_append( result, quote ); | |
4963 | if ( err_redir ) | |
4964 | { | |
4965 | string_append( result, " " ); | |
4966 | string_append( result, redirect ); | |
4967 | } | |
4968 | } | |
4969 | ||
4970 | /* Replace STDXXX with the temporary file. */ | |
4971 | list_free( stack_pop( s ) ); | |
4972 | stack_push( s, list_new( object_new( result->value ) ) ); | |
4973 | out = object_str( tmp_filename ); | |
4974 | ||
4975 | string_free( result ); | |
4976 | ||
4977 | /* Make sure temp files created by this get nuked eventually. */ | |
4978 | file_remove_atexit( tmp_filename ); | |
4979 | } | |
4980 | ||
4981 | if ( !globs.noexec ) | |
4982 | { | |
4983 | string out_name[ 1 ]; | |
4984 | /* Handle "path to file" filenames. */ | |
4985 | if ( ( out[ 0 ] == '"' ) && ( out[ strlen( out ) - 1 ] == '"' ) | |
4986 | ) | |
4987 | { | |
4988 | string_copy( out_name, out + 1 ); | |
4989 | string_truncate( out_name, out_name->size - 1 ); | |
4990 | } | |
4991 | else | |
4992 | string_copy( out_name, out ); | |
4993 | out_file = fopen( out_name->value, "w" ); | |
4994 | ||
4995 | if ( !out_file ) | |
4996 | { | |
4997 | err_printf( "failed to write output file '%s'!\n", | |
4998 | out_name->value ); | |
4999 | exit( EXITBAD ); | |
5000 | } | |
5001 | string_free( out_name ); | |
5002 | } | |
5003 | ||
5004 | if ( out_debug ) out_printf( "\nfile %s\n", out ); | |
5005 | if ( out_file ) fputs( buf->value, out_file ); | |
5006 | if ( out_debug ) out_puts( buf->value ); | |
5007 | if ( out_file ) | |
5008 | { | |
5009 | fflush( out_file ); | |
5010 | fclose( out_file ); | |
5011 | } | |
5012 | string_free( buf ); | |
5013 | if ( tmp_filename ) | |
5014 | object_free( tmp_filename ); | |
5015 | ||
5016 | if ( out_debug ) out_putc( '\n' ); | |
5017 | PROFILE_EXIT_LOCAL(function_run_INSTR_WRITE_FILE); | |
5018 | break; | |
5019 | } | |
5020 | ||
5021 | case INSTR_OUTPUT_STRINGS: | |
5022 | { | |
5023 | PROFILE_ENTER_LOCAL(function_run_INSTR_OUTPUT_STRINGS); | |
5024 | string * const buf = *(string * *)( (char *)stack_get( s ) + ( | |
5025 | code->arg * sizeof( LIST * ) ) ); | |
5026 | combine_strings( s, code->arg, buf ); | |
5027 | PROFILE_EXIT_LOCAL(function_run_INSTR_OUTPUT_STRINGS); | |
5028 | break; | |
5029 | } | |
5030 | ||
b32b8144 FG |
5031 | case INSTR_DEBUG_LINE: |
5032 | { | |
5033 | debug_on_instruction( frame, function->file, code->arg ); | |
5034 | break; | |
5035 | } | |
5036 | ||
7c673cae FG |
5037 | } |
5038 | ++code; | |
5039 | } | |
5040 | ||
5041 | PROFILE_EXIT_LOCAL(function_run); | |
5042 | } | |
5043 | ||
5044 | ||
5045 | #ifdef HAVE_PYTHON | |
5046 | ||
5047 | static struct arg_list * arg_list_compile_python( PyObject * bjam_signature, | |
5048 | int * num_arguments ) | |
5049 | { | |
5050 | if ( bjam_signature ) | |
5051 | { | |
5052 | struct argument_list_compiler c[ 1 ]; | |
5053 | struct arg_list * result; | |
5054 | Py_ssize_t s; | |
5055 | Py_ssize_t i; | |
5056 | argument_list_compiler_init( c ); | |
5057 | ||
5058 | s = PySequence_Size( bjam_signature ); | |
5059 | for ( i = 0; i < s; ++i ) | |
5060 | { | |
5061 | struct argument_compiler arg_comp[ 1 ]; | |
5062 | struct arg_list arg; | |
5063 | PyObject * v = PySequence_GetItem( bjam_signature, i ); | |
5064 | Py_ssize_t j; | |
5065 | Py_ssize_t inner; | |
5066 | argument_compiler_init( arg_comp ); | |
5067 | ||
5068 | inner = PySequence_Size( v ); | |
5069 | for ( j = 0; j < inner; ++j ) | |
5070 | argument_compiler_add( arg_comp, object_new( PyString_AsString( | |
5071 | PySequence_GetItem( v, j ) ) ), constant_builtin, -1 ); | |
5072 | ||
5073 | arg = arg_compile_impl( arg_comp, constant_builtin, -1 ); | |
5074 | dynamic_array_push( c->args, arg ); | |
5075 | argument_compiler_free( arg_comp ); | |
5076 | Py_DECREF( v ); | |
5077 | } | |
5078 | ||
5079 | *num_arguments = c->args->size; | |
5080 | result = BJAM_MALLOC( c->args->size * sizeof( struct arg_list ) ); | |
5081 | memcpy( result, c->args->data, c->args->size * sizeof( struct arg_list ) | |
5082 | ); | |
5083 | argument_list_compiler_free( c ); | |
5084 | return result; | |
5085 | } | |
5086 | *num_arguments = 0; | |
5087 | return 0; | |
5088 | } | |
5089 | ||
5090 | FUNCTION * function_python( PyObject * function, PyObject * bjam_signature ) | |
5091 | { | |
5092 | PYTHON_FUNCTION * result = BJAM_MALLOC( sizeof( PYTHON_FUNCTION ) ); | |
5093 | ||
5094 | result->base.type = FUNCTION_PYTHON; | |
5095 | result->base.reference_count = 1; | |
5096 | result->base.rulename = 0; | |
5097 | result->base.formal_arguments = arg_list_compile_python( bjam_signature, | |
5098 | &result->base.num_formal_arguments ); | |
5099 | Py_INCREF( function ); | |
5100 | result->python_function = function; | |
5101 | ||
5102 | return (FUNCTION *)result; | |
5103 | } | |
5104 | ||
5105 | ||
5106 | static void argument_list_to_python( struct arg_list * formal, int formal_count, | |
5107 | FUNCTION * function, FRAME * frame, PyObject * kw ) | |
5108 | { | |
5109 | LOL * all_actual = frame->args; | |
5110 | int i; | |
5111 | ||
5112 | for ( i = 0; i < formal_count; ++i ) | |
5113 | { | |
5114 | LIST * actual = lol_get( all_actual, i ); | |
5115 | LISTITER actual_iter = list_begin( actual ); | |
5116 | LISTITER const actual_end = list_end( actual ); | |
5117 | int j; | |
5118 | for ( j = 0; j < formal[ i ].size; ++j ) | |
5119 | { | |
5120 | struct argument * formal_arg = &formal[ i ].args[ j ]; | |
5121 | PyObject * value; | |
5122 | LIST * l; | |
5123 | ||
5124 | switch ( formal_arg->flags ) | |
5125 | { | |
5126 | case ARG_ONE: | |
5127 | if ( actual_iter == actual_end ) | |
5128 | argument_error( "missing argument", function, frame, | |
5129 | formal_arg->arg_name ); | |
5130 | type_check_range( formal_arg->type_name, actual_iter, list_next( | |
5131 | actual_iter ), frame, function, formal_arg->arg_name ); | |
5132 | value = PyString_FromString( object_str( list_item( actual_iter | |
5133 | ) ) ); | |
5134 | actual_iter = list_next( actual_iter ); | |
5135 | break; | |
5136 | case ARG_OPTIONAL: | |
5137 | if ( actual_iter == actual_end ) | |
5138 | value = 0; | |
5139 | else | |
5140 | { | |
5141 | type_check_range( formal_arg->type_name, actual_iter, | |
5142 | list_next( actual_iter ), frame, function, | |
5143 | formal_arg->arg_name ); | |
5144 | value = PyString_FromString( object_str( list_item( | |
5145 | actual_iter ) ) ); | |
5146 | actual_iter = list_next( actual_iter ); | |
5147 | } | |
5148 | break; | |
5149 | case ARG_PLUS: | |
5150 | if ( actual_iter == actual_end ) | |
5151 | argument_error( "missing argument", function, frame, | |
5152 | formal_arg->arg_name ); | |
5153 | /* fallthrough */ | |
5154 | case ARG_STAR: | |
5155 | type_check_range( formal_arg->type_name, actual_iter, | |
5156 | actual_end, frame, function, formal_arg->arg_name ); | |
5157 | l = list_copy_range( actual, actual_iter, actual_end ); | |
5158 | value = list_to_python( l ); | |
5159 | list_free( l ); | |
5160 | actual_iter = actual_end; | |
5161 | break; | |
5162 | case ARG_VARIADIC: | |
5163 | return; | |
5164 | } | |
5165 | ||
5166 | if ( value ) | |
5167 | { | |
5168 | PyObject * key = PyString_FromString( object_str( | |
5169 | formal_arg->arg_name ) ); | |
5170 | PyDict_SetItem( kw, key, value ); | |
5171 | Py_DECREF( key ); | |
5172 | Py_DECREF( value ); | |
5173 | } | |
5174 | } | |
5175 | ||
5176 | if ( actual_iter != actual_end ) | |
5177 | argument_error( "extra argument", function, frame, list_item( | |
5178 | actual_iter ) ); | |
5179 | } | |
5180 | ||
5181 | for ( ; i < all_actual->count; ++i ) | |
5182 | { | |
5183 | LIST * const actual = lol_get( all_actual, i ); | |
5184 | if ( !list_empty( actual ) ) | |
5185 | argument_error( "extra argument", function, frame, list_front( | |
5186 | actual ) ); | |
5187 | } | |
5188 | } | |
5189 | ||
5190 | ||
5191 | /* Given a Python object, return a string to use in Jam code instead of the said | |
5192 | * object. | |
5193 | * | |
5194 | * If the object is a string, use the string value. | |
5195 | * If the object implemenets __jam_repr__ method, use that. | |
5196 | * Otherwise return 0. | |
5197 | */ | |
5198 | ||
5199 | OBJECT * python_to_string( PyObject * value ) | |
5200 | { | |
5201 | if ( PyString_Check( value ) ) | |
5202 | return object_new( PyString_AS_STRING( value ) ); | |
5203 | ||
5204 | /* See if this instance defines the special __jam_repr__ method. */ | |
5205 | if ( PyInstance_Check( value ) | |
5206 | && PyObject_HasAttrString( value, "__jam_repr__" ) ) | |
5207 | { | |
5208 | PyObject * repr = PyObject_GetAttrString( value, "__jam_repr__" ); | |
5209 | if ( repr ) | |
5210 | { | |
5211 | PyObject * arguments2 = PyTuple_New( 0 ); | |
5212 | PyObject * value2 = PyObject_Call( repr, arguments2, 0 ); | |
5213 | Py_DECREF( repr ); | |
5214 | Py_DECREF( arguments2 ); | |
5215 | if ( PyString_Check( value2 ) ) | |
5216 | return object_new( PyString_AS_STRING( value2 ) ); | |
5217 | Py_DECREF( value2 ); | |
5218 | } | |
5219 | } | |
5220 | return 0; | |
5221 | } | |
5222 | ||
5223 | ||
5224 | static module_t * python_module() | |
5225 | { | |
5226 | static module_t * python = 0; | |
5227 | if ( !python ) | |
5228 | python = bindmodule( constant_python ); | |
5229 | return python; | |
5230 | } | |
5231 | ||
5232 | ||
5233 | static LIST * call_python_function( PYTHON_FUNCTION * function, FRAME * frame ) | |
5234 | { | |
5235 | LIST * result = 0; | |
5236 | PyObject * arguments = 0; | |
5237 | PyObject * kw = NULL; | |
5238 | int i; | |
5239 | PyObject * py_result; | |
5240 | FRAME * prev_frame_before_python_call; | |
5241 | ||
5242 | if ( function->base.formal_arguments ) | |
5243 | { | |
5244 | arguments = PyTuple_New( 0 ); | |
5245 | kw = PyDict_New(); | |
5246 | argument_list_to_python( function->base.formal_arguments, | |
5247 | function->base.num_formal_arguments, &function->base, frame, kw ); | |
5248 | } | |
5249 | else | |
5250 | { | |
5251 | arguments = PyTuple_New( frame->args->count ); | |
5252 | for ( i = 0; i < frame->args->count; ++i ) | |
5253 | PyTuple_SetItem( arguments, i, list_to_python( lol_get( frame->args, | |
5254 | i ) ) ); | |
5255 | } | |
5256 | ||
5257 | frame->module = python_module(); | |
5258 | ||
5259 | prev_frame_before_python_call = frame_before_python_call; | |
5260 | frame_before_python_call = frame; | |
5261 | py_result = PyObject_Call( function->python_function, arguments, kw ); | |
5262 | frame_before_python_call = prev_frame_before_python_call; | |
5263 | Py_DECREF( arguments ); | |
5264 | Py_XDECREF( kw ); | |
5265 | if ( py_result != NULL ) | |
5266 | { | |
5267 | if ( PyList_Check( py_result ) ) | |
5268 | { | |
5269 | int size = PyList_Size( py_result ); | |
5270 | int i; | |
5271 | for ( i = 0; i < size; ++i ) | |
5272 | { | |
5273 | OBJECT * s = python_to_string( PyList_GetItem( py_result, i ) ); | |
5274 | if ( !s ) | |
5275 | err_printf( | |
5276 | "Non-string object returned by Python call.\n" ); | |
5277 | else | |
5278 | result = list_push_back( result, s ); | |
5279 | } | |
5280 | } | |
5281 | else if ( py_result == Py_None ) | |
5282 | { | |
5283 | result = L0; | |
5284 | } | |
5285 | else | |
5286 | { | |
5287 | OBJECT * const s = python_to_string( py_result ); | |
5288 | if ( s ) | |
5289 | result = list_new( s ); | |
5290 | else | |
5291 | /* We have tried all we could. Return empty list. There are | |
5292 | * cases, e.g. feature.feature function that should return a | |
5293 | * value for the benefit of Python code and which also can be | |
5294 | * called by Jam code, where no sensible value can be returned. | |
5295 | * We cannot even emit a warning, since there would be a pile of | |
5296 | * them. | |
5297 | */ | |
5298 | result = L0; | |
5299 | } | |
5300 | ||
5301 | Py_DECREF( py_result ); | |
5302 | } | |
5303 | else | |
5304 | { | |
5305 | PyErr_Print(); | |
5306 | err_printf( "Call failed\n" ); | |
5307 | } | |
5308 | ||
5309 | return result; | |
5310 | } | |
5311 | ||
5312 | #endif | |
5313 | ||
5314 | ||
5315 | void function_done( void ) | |
5316 | { | |
5317 | BJAM_FREE( stack ); | |
5318 | } |