-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcompiler.c
4827 lines (3860 loc) · 110 KB
/
compiler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* compiler.c
*
* MathMap
*
* Copyright (C) 2002-2007 Mark Probst
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <complex.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <glib.h>
#ifndef OPENSTEP
#include <gmodule.h>
#else
#include <mach-o/dyld.h>
#endif
#include "gtypes.h"
#include "mathmap.h"
#include "vars.h"
#include "internals.h"
#include "tags.h"
#include "exprtree.h"
#include "overload.h"
#include "internals.h"
#include "jump.h"
#include "scanner.h"
#include "bitvector.h"
#include "lispreader/pools.h"
#include "compiler.h"
#include "opmacros.h"
//#define NO_CONSTANTS_ANALYSIS
struct _value_t;
typedef struct
{
int number;
int last_index;
} temporary_t;
#define MAX_PROMOTABLE_TYPE TYPE_COMPLEX
#define CONST_MAX (CONST_Y | CONST_X | CONST_T)
typedef int type_t;
typedef struct _compvar_t
{
variable_t *var; /* 0 if compvar is a temporary */
temporary_t *temp; /* 0 if compvar is a variable */
int n; /* n/a if compvar is a temporary */
type_t type;
struct _value_t *current;
struct _value_t *values;
} compvar_t;
struct _statement_list_t;
struct _statement_t;
struct _native_register_t;
struct _pre_native_insn_t;
typedef struct _value_t
{
compvar_t *compvar;
int global_index;
int index; /* SSA index */
struct _statement_t *def;
struct _statement_list_t *uses;
struct _pre_native_insn_t *live_start;
struct _pre_native_insn_t *live_end;
struct _native_register_t *allocated_register;
unsigned int const_type : 3; /* defined in internals.h */
unsigned int least_const_type_directly_used_in : 3;
unsigned int least_const_type_multiply_used_in : 3;
unsigned int have_defined : 1; /* used in c code output */
struct _value_t *next; /* next value for same compvar */
} value_t;
typedef struct _value_list_t
{
value_t *value;
struct _value_list_t *next;
} value_list_t;
/* is_rhs_const_primary() assumes that PRIMARY_VALUE is the only non-const
* primary type. */
#define PRIMARY_VALUE 1
#define PRIMARY_CONST 2
typedef struct
{
int kind;
int const_type; /* only valid if type == PRIMARY_VALUE */
union
{
value_t *value;
runtime_value_t constant;
} v;
} primary_t;
#define MAKE_CONST_PRIMARY(name, c_type, type_name) \
runtime_value_t \
make_ ## name ## _runtime_value (c_type name ## _value) \
{ \
runtime_value_t value; \
value.name ## _value = name ## _value; \
return value; \
} \
primary_t \
make_ ## name ## _const_primary (c_type name ## _const) \
{ \
primary_t primary; \
primary.kind = PRIMARY_CONST; \
primary.const_type = type_name; \
primary.v.constant.name ## _value = name ## _const; \
return primary; \
}
// defined in compiler_types.h
MAKE_CONST_PRIMARY_FUNCS
MAKE_TYPE_C_TYPE_NAME
#define TYPE_PROP_CONST 1
#define TYPE_PROP_MAX 2
#define TYPE_PROP_MAX_FLOAT 3
typedef int type_prop_t;
typedef struct _operation_t
{
int index;
char *name;
int num_args;
type_prop_t type_prop;
int is_pure;
int is_foldable;
type_t const_type; /* used only if type_prop == TYPE_PROP_CONST */
type_t arg_types[MAX_OP_ARGS]; /* used only if type_prop == TYPE_PROP_CONST */
} operation_t;
#define RHS_PRIMARY 1
#define RHS_INTERNAL 2
#define RHS_OP 3
typedef struct
{
int kind;
union
{
primary_t primary;
internal_t *internal;
struct
{
operation_t *op;
primary_t args[MAX_OP_ARGS];
} op;
} v;
} rhs_t;
#define STMT_NIL 0
#define STMT_ASSIGN 1
#define STMT_PHI_ASSIGN 2
#define STMT_IF_COND 3
#define STMT_WHILE_LOOP 4
#define SLICE_XY_CONST 1
#define SLICE_X_CONST 2
#define SLICE_Y_CONST 4
#define SLICE_NO_CONST 8
typedef struct _statement_t
{
int kind;
union
{
struct
{
value_t *lhs;
rhs_t *rhs;
rhs_t *rhs2; /* only valid for STMT_PHI_ASSIGN */
value_t *old_value; /* only valid for STMT_PHI_ASSIGN */
} assign;
struct
{
rhs_t *condition;
struct _statement_t *consequent;
struct _statement_t *alternative;
struct _statement_t *exit;
} if_cond;
struct
{
struct _statement_t *entry;
rhs_t *invariant;
struct _statement_t *body;
} while_loop;
} v;
struct _statement_t *parent;
unsigned int slice_flags;
struct _statement_t *next;
} statement_t;
typedef struct _statement_list_t
{
statement_t *stmt;
struct _statement_list_t *next;
} statement_list_t;
#define PRE_NATIVE_INSN_LABEL 0
#define PRE_NATIVE_INSN_GOTO 1
#define PRE_NATIVE_INSN_ASSIGN 2
#define PRE_NATIVE_INSN_PHI_ASSIGN 3
#define PRE_NATIVE_INSN_IF_COND_FALSE_GOTO 4
typedef struct _pre_native_insn_t
{
int kind;
int index;
statement_t *stmt;
union
{
struct _pre_native_insn_t *target;
int phi_rhs;
} v;
struct _pre_native_insn_t *next;
} pre_native_insn_t;
static void init_op (int index, char *name, int num_args, type_prop_t type_prop,
type_t const_type, int is_pure, int is_foldable, ...);
static int rhs_is_foldable (rhs_t *rhs);
static type_t primary_type (primary_t *primary);
#include <complex.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_specfunc.h>
#include <gsl/gsl_sf_elljac.h>
#include "spec_func.h"
#include "builtins.h"
#include "noise.h"
#define RHS_ARG(i) (rhs->v.op.args[(i)])
#define OP_CONST_INT_VAL(i) ({ assert(RHS_ARG((i)).kind == PRIMARY_CONST); \
(RHS_ARG((i)).const_type == TYPE_INT ? RHS_ARG((i)).v.constant.int_value : \
({ assert(0); 0.0; })); })
#define OP_CONST_FLOAT_VAL(i) ({ assert(RHS_ARG((i)).kind == PRIMARY_CONST); \
(RHS_ARG((i)).const_type == TYPE_INT ? (float)(RHS_ARG((i)).v.constant.int_value) : \
RHS_ARG((i)).const_type == TYPE_FLOAT ? RHS_ARG((i)).v.constant.float_value : \
({ assert(0); 0.0; })); })
#define OP_CONST_COMPLEX_VAL(i) ({ assert(RHS_ARG((i)).kind == PRIMARY_CONST); \
(RHS_ARG((i)).const_type == TYPE_INT ? (complex float)(RHS_ARG((i)).v.constant.int_value) : \
RHS_ARG((i)).const_type == TYPE_FLOAT ? RHS_ARG((i)).v.constant.float_value : \
RHS_ARG((i)).const_type == TYPE_COMPLEX ? RHS_ARG((i)).v.constant.complex_value : \
({ assert(0); 0.0; })); })
#define OUTPUT_COLOR_INTERPRETER(c) ({ invocation->interpreter_output_color = (c); invocation->interpreter_ip = -1; 0; })
#define ORIG_VAL_INTERPRETER(x,y,d,f) ({ color_t c; \
if (invocation->antialiasing) \
c = get_orig_val_intersample_pixel(invocation, (x), (y), (d), (f)); \
else \
c = get_orig_val_pixel(invocation, (x), (y), (d), (f)); \
c; })
#include "opdefs.h"
static pools_t compiler_pools;
static operation_t ops[NUM_OPS];
static int next_temp_number = 1;
static statement_t *first_stmt = 0;
static statement_t **emit_loc = &first_stmt;
static statement_t dummy_stmt = { STMT_NIL };
static pre_native_insn_t *first_pre_native_insn;
static pre_native_insn_t *last_pre_native_insn;
#define STMT_STACK_SIZE 64
static statement_t *stmt_stack[STMT_STACK_SIZE];
static int stmt_stackp = 0;
#define CURRENT_STACK_TOP ((stmt_stackp > 0) ? stmt_stack[stmt_stackp - 1] : 0)
#define UNSAFE_EMIT_STMT(s,l) \
({ (s)->parent = CURRENT_STACK_TOP; \
(s)->next = (l); (l) = (s); })
/*** hash tables ***/
static GHashTable*
direct_hash_table_copy (GHashTable *table)
{
GHashTable *copy = g_hash_table_new(g_direct_hash, g_direct_equal);
void copy_entry (gpointer key, gpointer value, gpointer user_data)
{ g_hash_table_insert(copy, key, value); }
assert(copy != 0);
g_hash_table_foreach(table, ©_entry, 0);
return copy;
}
/*** value sets ***/
typedef bit_vector_t value_set_t;
/* This is updated by new_value. We assume that no new values are generated
* at the time value sets are used. */
static int next_value_global_index = 0;
static value_set_t*
new_value_set (void)
{
return new_bit_vector(next_value_global_index, 0);
}
static void
value_set_add (value_set_t *set, value_t *val)
{
bit_vector_set(set, val->global_index);
}
static int
value_set_contains (value_set_t *set, value_t *val)
{
return bit_vector_bit(set, val->global_index);
}
static value_set_t*
value_set_copy (value_set_t *set)
{
return copy_bit_vector(set);
}
static void
free_value_set (value_set_t *set)
{
free_bit_vector(set);
}
int
op_index (operation_t *op)
{
return op - ops;
}
#define alloc_stmt() ((statement_t*)pools_alloc(&compiler_pools, sizeof(statement_t)))
#define alloc_value() ((value_t*)pools_alloc(&compiler_pools, sizeof(value_t)))
#define alloc_rhs() ((rhs_t*)pools_alloc(&compiler_pools, sizeof(rhs_t)))
#define alloc_compvar() (compvar_t*)pools_alloc(&compiler_pools, sizeof(compvar_t))
#define alloc_primary() (primary_t*)pools_alloc(&compiler_pools, sizeof(primary_t))
static value_t*
new_value (compvar_t *compvar)
{
value_t *val = alloc_value();
val->compvar = compvar; /* dummy value */
val->global_index = next_value_global_index++;
val->index = -1;
val->def = &dummy_stmt;
val->uses = 0;
val->live_start = val->live_end = 0;
val->allocated_register = 0;
val->const_type = CONST_NONE;
val->least_const_type_directly_used_in = CONST_MAX;
val->least_const_type_multiply_used_in = CONST_MAX;
val->have_defined = 0;
val->next = 0;
return val;
}
compvar_t*
make_temporary (type_t type)
{
temporary_t *temp = (temporary_t*)pools_alloc(&compiler_pools, sizeof(temporary_t));
compvar_t *compvar = alloc_compvar();
value_t *val = new_value(compvar);
temp->number = next_temp_number++;
temp->last_index = 0;
compvar->var = 0;
compvar->temp = temp;
compvar->type = type;
compvar->current = val;
compvar->values = val;
return compvar;
}
compvar_t*
make_variable (variable_t *var, int n)
{
compvar_t *compvar = alloc_compvar();
value_t *val = new_value(compvar);
compvar->var = var;
compvar->temp = 0;
compvar->n = n;
compvar->type = TYPE_INT;
compvar->current = val;
compvar->values = val;
return compvar;
}
value_t*
make_lhs (compvar_t *compvar)
{
value_t *val = new_value(compvar);
val->next = compvar->values;
compvar->values = val;
return val;
}
statement_list_t*
prepend_statement (statement_t *stmt, statement_list_t *rest)
{
statement_list_t *lst = (statement_list_t*)pools_alloc(&compiler_pools, sizeof(statement_list_t));
lst->stmt = stmt;
lst->next = rest;
return lst;
}
void
add_use (value_t *val, statement_t *stmt)
{
val->uses = prepend_statement(stmt, val->uses);
}
void
remove_use (value_t *val, statement_t *stmt)
{
statement_list_t **lst = &val->uses;
while (*lst != 0)
{
statement_list_t *elem = *lst;
if (elem->stmt == stmt)
{
*lst = elem->next;
return;
}
lst = &(*lst)->next;
}
assert(0);
}
value_t*
make_value_copy (value_t *val)
{
return make_lhs(val->compvar);
}
void
set_value_current (value_t *val, value_t *new_current)
{
val->compvar->current = new_current;
}
value_t*
current_value (compvar_t *compvar)
{
return compvar->current;
}
void
assign_value_index_and_make_current (value_t *val)
{
if (val->compvar->var != 0)
val->index = ++val->compvar->var->last_index[val->compvar->n];
else
val->index = ++val->compvar->temp->last_index;
set_value_current(val, val);
}
primary_t
make_value_primary (value_t *value)
{
primary_t primary;
primary.kind = PRIMARY_VALUE;
primary.v.value = value;
return primary;
}
primary_t
make_compvar_primary (compvar_t *compvar)
{
return make_value_primary(current_value(compvar));
}
rhs_t*
make_int_const_rhs (int int_const)
{
rhs_t *rhs = alloc_rhs();
rhs->kind = RHS_PRIMARY;
rhs->v.primary = make_int_const_primary(int_const);
return rhs;
}
rhs_t*
make_float_const_rhs (float float_const)
{
rhs_t *rhs = alloc_rhs();
rhs->kind = RHS_PRIMARY;
rhs->v.primary = make_float_const_primary(float_const);
return rhs;
}
rhs_t*
make_value_rhs (value_t *val)
{
rhs_t *rhs = alloc_rhs();
assert(val != 0);
rhs->kind = RHS_PRIMARY;
rhs->v.primary.kind = PRIMARY_VALUE;
rhs->v.primary.v.value = val;
return rhs;
}
rhs_t*
make_primary_rhs (primary_t primary)
{
rhs_t *rhs = alloc_rhs();
rhs->kind = RHS_PRIMARY;
rhs->v.primary = primary;
return rhs;
}
rhs_t*
make_compvar_rhs (compvar_t *compvar)
{
return make_value_rhs(current_value(compvar));
}
rhs_t*
make_internal_rhs (internal_t *internal)
{
rhs_t *rhs = alloc_rhs();
rhs->kind = RHS_INTERNAL;
rhs->v.internal = internal;
return rhs;
}
static rhs_t*
make_op_rhs_from_array (int op_index, primary_t *args)
{
rhs_t *rhs = alloc_rhs();
rhs->kind = RHS_OP;
rhs->v.op.op = &ops[op_index];
memcpy(rhs->v.op.args, args, sizeof(primary_t) * rhs->v.op.op->num_args);
return rhs;
}
static rhs_t*
make_op_rhs (int op_index, ...)
{
primary_t args[MAX_OP_ARGS];
va_list ap;
int i;
va_start(ap, op_index);
for (i = 0; i < ops[op_index].num_args; ++i)
args[i] = va_arg(ap, primary_t);
va_end(ap);
return make_op_rhs_from_array(op_index, args);
}
statement_t*
find_phi_assign (statement_t *stmts, compvar_t *compvar)
{
for (; stmts != 0; stmts = stmts->next)
{
/* we assert this because this function is called before any
optimization takes place, hence no statements are changed to
nils */
assert(stmts->kind == STMT_PHI_ASSIGN);
if (stmts->v.assign.lhs->compvar == compvar)
return stmts;
}
return 0;
}
primary_t*
find_value_in_rhs (value_t *val, rhs_t *rhs)
{
switch (rhs->kind)
{
case RHS_PRIMARY :
if (rhs->v.primary.kind == PRIMARY_VALUE
&& rhs->v.primary.v.value == val)
return &rhs->v.primary;
else
return 0;
break;
case RHS_INTERNAL :
return 0;
case RHS_OP :
{
int i;
for (i = 0; i < rhs->v.op.op->num_args; ++i)
if (rhs->v.op.args[i].kind == PRIMARY_VALUE
&& rhs->v.op.args[i].v.value == val)
return &rhs->v.op.args[i];
return 0;
}
break;
}
return 0;
}
static void
for_each_value_in_rhs (rhs_t *rhs, void (*func) (value_t *value))
{
if (rhs->kind == RHS_PRIMARY && rhs->v.primary.kind == PRIMARY_VALUE)
func(rhs->v.primary.v.value);
else if (rhs->kind == RHS_OP)
{
int i;
for (i = 0; i < rhs->v.op.op->num_args; ++i)
{
primary_t *arg = &rhs->v.op.args[i];
if (arg->kind == PRIMARY_VALUE)
func(arg->v.value);
}
}
}
/*
static int
rhs_contains (rhs_t *rhs, value_t *value)
{
int contained = 0;
void func (value_t *val)
{ if (val == value) contained = 1; }
for_each_value_in_rhs(rhs, &func);
return contained;
}
*/
static void
for_each_assign_statement (statement_t *stmts, void (*func) (statement_t *stmt))
{
while (stmts != 0)
{
switch (stmts->kind)
{
case STMT_NIL :
break;
case STMT_ASSIGN :
case STMT_PHI_ASSIGN :
func(stmts);
break;
case STMT_IF_COND :
for_each_assign_statement(stmts->v.if_cond.consequent, func);
for_each_assign_statement(stmts->v.if_cond.alternative, func);
for_each_assign_statement(stmts->v.if_cond.exit, func);
break;
case STMT_WHILE_LOOP :
for_each_assign_statement(stmts->v.while_loop.entry, func);
for_each_assign_statement(stmts->v.while_loop.body, func);
break;
default :
assert(0);
}
stmts = stmts->next;
}
}
static void
for_each_value_in_statements (statement_t *stmt, void (*func) (value_t *value, statement_t *stmt))
{
void call_func (value_t *value)
{ func(value, stmt); }
while (stmt != 0)
{
switch (stmt->kind)
{
case STMT_NIL :
break;
case STMT_PHI_ASSIGN :
for_each_value_in_rhs(stmt->v.assign.rhs2, &call_func);
case STMT_ASSIGN :
for_each_value_in_rhs(stmt->v.assign.rhs, &call_func);
func(stmt->v.assign.lhs, stmt);
break;
case STMT_IF_COND :
for_each_value_in_rhs(stmt->v.if_cond.condition, &call_func);
for_each_value_in_statements(stmt->v.if_cond.consequent, func);
for_each_value_in_statements(stmt->v.if_cond.alternative, func);
for_each_value_in_statements(stmt->v.if_cond.exit, func);
break;
case STMT_WHILE_LOOP :
for_each_value_in_rhs(stmt->v.while_loop.invariant, &call_func);
for_each_value_in_statements(stmt->v.while_loop.entry, func);
for_each_value_in_statements(stmt->v.while_loop.body, func);
break;
default :
assert(0);
}
stmt = stmt->next;
}
}
/* checks whether stmt is a direct or indirect child of limit. if stmt ==
* limit, it does not count as a child */
static int
stmt_is_within_limit (statement_t *stmt, statement_t *limit)
{
if (limit == 0)
return 1;
do
{
stmt = stmt->parent;
} while (stmt != 0 && stmt != limit);
if (stmt == 0)
return 0;
return 1;
}
/* assumes that old is used at least once in stmt */
static void
rewrite_use (statement_t *stmt, value_t *old, primary_t new)
{
primary_t *primary;
statement_list_t **lst;
int found_one = 0;
if (new.kind == PRIMARY_VALUE)
assert(old != new.v.value);
restart:
/* remove stmt from the uses list of old */
lst = &old->uses;
for (;;)
{
statement_list_t *elem = *lst;
if (elem == 0)
{
/* if we don't find it now we must have done at least one
iteration before */
assert(found_one);
return;
}
if (elem->stmt == stmt)
{
*lst = elem->next;
found_one = 1;
break;
}
lst = &elem->next;
}
/* now find out where the use is */
switch (stmt->kind)
{
case STMT_ASSIGN :
primary = find_value_in_rhs(old, stmt->v.assign.rhs);
break;
case STMT_PHI_ASSIGN :
primary = find_value_in_rhs(old, stmt->v.assign.rhs);
if (primary == 0)
primary = find_value_in_rhs(old, stmt->v.assign.rhs2);
break;
case STMT_IF_COND :
primary = find_value_in_rhs(old, stmt->v.if_cond.condition);
break;
case STMT_WHILE_LOOP :
primary = find_value_in_rhs(old, stmt->v.while_loop.invariant);
break;
default :
assert(0);
}
assert(primary != 0 && primary->v.value == old);
/* rewrite */
*primary = new;
/* add to new use list */
if (new.kind == PRIMARY_VALUE)
add_use(new.v.value, stmt);
/* old might be used more than once in stmt */
goto restart;
}
static void
rewrite_uses (value_t *old, primary_t new, statement_t *limit)
{
statement_list_t *lst;
if (new.kind == PRIMARY_VALUE)
assert(old != new.v.value);
lst = old->uses;
while (lst != 0)
{
statement_t *stmt = lst->stmt;
/* we do not rewrite phis in the loop we're currently working on */
if (stmt_is_within_limit(stmt, limit)
&& !(stmt->kind == STMT_PHI_ASSIGN && stmt->parent == limit))
{
rewrite_use(stmt, old, new);
/* rewrite_use changes the list, so we have to restart -
very inefficient */
lst = old->uses;
}
else
lst = lst->next;
}
}
static void
rewrite_uses_to_value (value_t *old, value_t *new, statement_t *limit)
{
primary_t primary;
if (old == new)
return;
primary.kind = PRIMARY_VALUE;
primary.v.value = new;
rewrite_uses(old, primary, limit);
}
void
commit_assign (statement_t *stmt)
{
statement_t *tos;
if (stmt_stackp > 0)
{
tos = stmt_stack[stmt_stackp - 1];
switch (tos->kind)
{
case STMT_IF_COND :
{
statement_t *phi_assign = find_phi_assign(tos->v.if_cond.exit,
stmt->v.assign.lhs->compvar);
if (phi_assign == 0)
{
phi_assign = alloc_stmt();
phi_assign->kind = STMT_PHI_ASSIGN;
phi_assign->v.assign.lhs = make_value_copy(stmt->v.assign.lhs);
phi_assign->v.assign.rhs = make_value_rhs(current_value(stmt->v.assign.lhs->compvar));
add_use(current_value(stmt->v.assign.lhs->compvar), phi_assign);
phi_assign->v.assign.rhs2 = make_value_rhs(current_value(stmt->v.assign.lhs->compvar));
add_use(current_value(stmt->v.assign.lhs->compvar), phi_assign);
phi_assign->v.assign.old_value = current_value(stmt->v.assign.lhs->compvar);
phi_assign->v.assign.lhs->def = phi_assign;
UNSAFE_EMIT_STMT(phi_assign, tos->v.if_cond.exit);
}
if (tos->v.if_cond.alternative == 0)
{
assert(phi_assign->v.assign.rhs->kind == RHS_PRIMARY
&& phi_assign->v.assign.rhs->v.primary.kind == PRIMARY_VALUE);
remove_use(phi_assign->v.assign.rhs->v.primary.v.value, phi_assign);
phi_assign->v.assign.rhs = make_value_rhs(stmt->v.assign.lhs);
add_use(stmt->v.assign.lhs, phi_assign);
}
else
{
assert(phi_assign->v.assign.rhs2->kind == RHS_PRIMARY
&& phi_assign->v.assign.rhs2->v.primary.kind == PRIMARY_VALUE);
remove_use(phi_assign->v.assign.rhs2->v.primary.v.value, phi_assign);
phi_assign->v.assign.rhs2 = make_value_rhs(stmt->v.assign.lhs);
add_use(stmt->v.assign.lhs, phi_assign);
}
}
break;
case STMT_WHILE_LOOP :
{
statement_t *phi_assign = find_phi_assign(tos->v.while_loop.entry, stmt->v.assign.lhs->compvar);
if (phi_assign == 0)
{
phi_assign = alloc_stmt();
phi_assign->kind = STMT_PHI_ASSIGN;
phi_assign->v.assign.lhs = make_value_copy(stmt->v.assign.lhs);
phi_assign->v.assign.rhs = make_value_rhs(current_value(stmt->v.assign.lhs->compvar));
add_use(current_value(stmt->v.assign.lhs->compvar), phi_assign);
phi_assign->v.assign.lhs->def = phi_assign;
UNSAFE_EMIT_STMT(phi_assign, tos->v.while_loop.entry);
phi_assign->v.assign.rhs2 = make_value_rhs(stmt->v.assign.lhs);
add_use(stmt->v.assign.lhs, phi_assign);
phi_assign->v.assign.old_value = current_value(stmt->v.assign.lhs->compvar);