+ Oops; forgot to check for trailing whitespace.
[parrot.git] / compilers / imcc / imc.c
blob3d4c5f17ee3451decb130df2875ccbc2582075b5
1 /*
2 * $Id$
3 * Copyright (C) 2002-2007, The Perl Foundation.
4 */
6 /*
8 =head1 NAME
10 compilers/imcc/imc.c
12 =head1 DESCRIPTION
14 Routines for handling imc_units, which represent subs.
16 Moved all register allocation and spill code to reg_alloc.c
18 =head2 Functions
20 =over 4
22 =cut
26 #include <string.h>
27 #include "imc.h"
28 #include "optimizer.h"
30 /* HEADERIZER HFILE: compilers/imcc/imc.h */
32 /* HEADERIZER BEGIN: static */
34 static void imc_free_unit(PARROT_INTERP, ARGMOD(IMC_Unit *unit))
35 __attribute__nonnull__(1)
36 __attribute__nonnull__(2)
37 FUNC_MODIFIES(*unit);
39 PARROT_CANNOT_RETURN_NULL
40 PARROT_MALLOC
41 static IMC_Unit * imc_new_unit(IMC_Unit_Type t);
43 /* HEADERIZER END: static */
45 #define COMPILE_IMMEDIATE 1
47 extern FILE* yyin;
53 =item C<void imc_compile_all_units>
55 Compile all imc_units, and free all memory of instructions
56 and structures afterwards.
58 =cut
62 PARROT_API
63 void
64 imc_compile_all_units(PARROT_INTERP)
66 /* compile all units created during the parse */
67 IMC_Unit *unit;
68 #if ! COMPILE_IMMEDIATE
69 for (unit = IMCC_INFO(interp)->imc_units; unit;) {
70 IMC_Unit * const unit_next = unit->next;
71 imc_compile_unit(interp, unit);
72 unit = unit_next;
74 #endif
75 emit_close(interp, NULL);
77 /* All done with compilation, now free all memory allocated
78 * for instructions and other structures.
80 for (unit = IMCC_INFO(interp)->imc_units; unit;) {
81 IMC_Unit * const unit_next = unit->next;
82 Instruction *ins;
84 for (ins = unit->instructions; ins;) {
85 Instruction * const ins_next = ins->next;
86 free_ins(ins);
87 ins = ins_next;
89 imc_free_unit(interp, unit);
90 unit = unit_next;
93 IMCC_INFO(interp)->imc_units = NULL;
94 IMCC_INFO(interp)->last_unit = NULL;
99 =item C<void imc_compile_unit>
101 imc_compile_unit is the main loop of the IMC compiler for each unit. It
102 operates on a single compilation unit at a time.
104 =cut
108 PARROT_API
109 void
110 imc_compile_unit(PARROT_INTERP, ARGIN(IMC_Unit *unit))
112 /* Not much here for now except the allocator */
113 IMCC_INFO(interp)->cur_unit = unit;
115 imc_reg_alloc(interp, unit);
116 emit_flush(interp, NULL, unit);
121 =item C<void imc_cleanup>
123 Any activity required to cleanup the compiler state and be
124 ready for a new compiler invocation goes here.
126 =cut
130 PARROT_API
131 void
132 imc_cleanup(PARROT_INTERP, ARGIN_NULLOK(void *yyscanner))
134 IMCC_pop_parser_state(interp, yyscanner);
135 clear_globals(interp);
136 mem_sys_free(IMCC_INFO(interp)->ghash.data);
137 IMCC_INFO(interp)->ghash.data = NULL;
142 =item C<static IMC_Unit * imc_new_unit>
144 Create a new IMC_Unit.
146 =cut
150 PARROT_CANNOT_RETURN_NULL
151 PARROT_MALLOC
152 static IMC_Unit *
153 imc_new_unit(IMC_Unit_Type t)
155 IMC_Unit * const unit = mem_allocate_zeroed_typed(IMC_Unit);
156 create_symhash(&unit->hash);
157 unit->type = t;
158 return unit;
163 =item C<IMC_Unit * imc_open_unit>
165 Create a new IMC_Unit and "open" it for construction.
166 This sets the current state of the parser. The unit
167 can be closed later retaining all the current state.
169 =cut
173 PARROT_CANNOT_RETURN_NULL
174 IMC_Unit *
175 imc_open_unit(PARROT_INTERP, IMC_Unit_Type t)
177 IMC_Unit * const unit = imc_new_unit(t);
178 imc_info_t * const imc_info = IMCC_INFO(interp);
180 if (!imc_info->imc_units)
181 imc_info->imc_units = unit;
182 if (!imc_info->ghash.data)
183 create_symhash(&imc_info->ghash);
184 unit->prev = imc_info->last_unit;
185 if (imc_info->last_unit)
186 imc_info->last_unit->next = unit;
187 imc_info->last_unit = unit;
188 imc_info->n_comp_units++;
189 unit->file = imc_info->state->file;
190 unit->pasm_file = imc_info->state->pasm_file;
192 return unit;
197 =item C<void imc_close_unit>
199 Close a unit from compilation.
200 Does not destroy the unit, leaves it on the list.
202 =cut
206 void
207 imc_close_unit(PARROT_INTERP, ARGIN_NULLOK(IMC_Unit *unit))
209 #if COMPILE_IMMEDIATE
210 if (unit) {
211 imc_compile_unit(interp, unit);
213 #endif
215 IMCC_INFO(interp)->cur_unit = NULL;
220 =item C<static void imc_free_unit>
222 Free all memory allocated of an IMC_Unit structure.
224 =cut
228 static void
229 imc_free_unit(PARROT_INTERP, ARGMOD(IMC_Unit *unit))
231 imc_info_t * const imc = IMCC_INFO(interp);
233 #if IMC_TRACE_HIGH
234 fprintf(stderr, "imc_free_unit()\n");
235 #endif
237 free_reglist(unit);
239 clear_basic_blocks(unit); /* and cfg ... */
240 if (!imc->n_comp_units)
241 IMCC_fatal(interp, 1, "imc_free_unit: non existent unit\n");
242 imc->n_comp_units--;
244 clear_locals(unit);
245 free(unit->hash.data);
247 free(unit);
252 =back
254 =cut
259 * Local variables:
260 * c-file-style: "parrot"
261 * End:
262 * vim: expandtab shiftwidth=4: