2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
5 /* $Id: code.c,v 3.51 1997/07/01 08:33:13 ceriel Exp $ */
6 /* C O D E - G E N E R A T I N G R O U T I N E S */
16 #include "botch_free.h"
37 #include "noRoption.h"
44 label datlab_count = 1;
50 /* global function info */
52 struct type *func_type;
60 extern char options[];
61 extern char *symbol2str();
67 /* init_code() initialises the output file on which the
68 compact EM code is written
70 C_init(word_size, pointer_size); /* initialise EM module */
71 if (C_open(dst_file) == 0)
72 fatal("cannot write to %s\n", dst_file);
74 C_ms_emx(word_size, pointer_size);
79 C_ms_std(source, N_SO, 0);
80 stb_typedef(int_type, "int");
81 stb_typedef(char_type, "char");
82 stb_typedef(long_type, "long");
83 stb_typedef(short_type, "short");
84 stb_typedef(uchar_type, "unsigned char");
85 stb_typedef(ushort_type, "unsigned short");
86 stb_typedef(ulong_type, "unsigned long");
87 stb_typedef(uint_type, "unsigned int");
88 stb_typedef(float_type, "float");
89 stb_typedef(double_type, "double");
90 stb_typedef(void_type, "void");
95 C_insertpart(tmp_id = C_getid());
97 #endif /* PREPEND_SCOPES */
101 struct string_cst *str_list = 0;
103 code_string(val, len, dlb)
108 register struct string_cst *sc = new_string_cst();
119 register struct string_cst *sc;
122 struct string_cst *sc1 = sc;
124 C_df_dlb(sc->sc_dlb);
125 str_cst(sc->sc_value, sc->sc_len);
127 free_string_cst(sc1);
134 /* end_code() performs the actions to be taken when closing
139 /* floating point used */
143 def_strings(str_list);
145 C_ms_src((int)(LineNumber - 2), FileName);
150 #ifdef PREPEND_SCOPES
153 /* prepend_scopes() runs down the list of global idf's
154 and generates those exa's, exp's, ina's and inp's
155 that superior hindsight has provided.
157 register struct stack_entry *se = local_level->sl_entry;
163 register struct idf *id = se->se_idf;
164 register struct def *df = id->id_def;
166 if (df && (df->df_initialized || df->df_used || df->df_alloc))
167 code_scope(id->id_text, df);
174 #endif /* PREPEND_SCOPES */
176 code_scope(text, def)
178 register struct def *def;
180 /* generates code for one name, text, of the storage class
181 as given by def, if meaningful.
183 int fund = def->df_type->tp_fund;
185 switch (def->df_sc) {
189 if (fund == FUNCTION)
195 if (fund == FUNCTION)
203 static label return_label, return2_label;
204 static char return_expr_occurred;
205 static arith func_size;
206 static label func_res_label;
207 static char *last_fn_given = "";
208 static label file_name_label;
210 begin_proc(ds, idf) /* to be called when entering a procedure */
214 /* begin_proc() is called at the entrance of a new function
215 and performs the necessary code generation:
216 - a scope indicator (if needed) exp/inp
217 - the procedure entry pro $name
218 - reserves some space if the result of the function
219 does not fit in the return area
220 - a fil pseudo instruction
222 register char *name = idf->id_text;
223 register struct def *def = idf->id_def;
225 while (def->df_level > L_GLOBAL) def = def->next;
226 /* idf->id_def does not indicate the right def structure
227 when the function being defined has a parameter of the
230 #ifndef PREPEND_SCOPES
231 code_scope(name, def);
232 #endif /* PREPEND_SCOPES */
235 DfaStartFunction(name);
236 #endif /* DATAFLOW */
238 /* set global function info */
240 if (def->df_type->tp_fund != FUNCTION) {
241 error("making function body for non-function");
242 func_type = error_type;
245 func_type = def->df_type->tp_up;
247 func_notypegiven = ds->ds_notypegiven;
248 func_size = ATW(func_type->tp_size);
253 C_insertpart(pro_id = C_getid());
255 if (is_struct_or_union(func_type->tp_fund)) {
256 C_df_dlb(func_res_label = data_label());
257 C_bss_cst(func_size, (arith)0, 1);
261 /* Special arrangements if the function result doesn't fit in
262 the function return area of the EM machine. The size of
263 the function return area is implementation dependent.
265 lab_count = (label) 1;
266 return_label = text_label();
267 return2_label = text_label();
268 return_expr_occurred = 0;
271 if (! options['L']) { /* profiling */
272 if (strcmp(last_fn_given, FileName) != 0) {
273 /* previous function came from other file */
274 C_df_dlb(file_name_label = data_label());
275 C_con_scon(last_fn_given = FileName,
276 (arith)(strlen(FileName) + 1));
278 /* enable debug trace of EM source */
279 C_fil_dlb(file_name_label, (arith)0);
280 C_lin((arith)LineNumber);
284 stb_string(def, FUNCTION, name);
285 if (! strcmp(name, "main")) {
286 C_ms_stb_cst(name, N_MAIN, 0, (arith) 0);
295 /* end_proc() deals with the code to be generated at the end of
296 a function, as there is:
297 - the EM ret instruction: "ret 0"
298 - loading of the function result in the function
299 result area if there has been a return <expr>
300 in the function body (see do_return_expr())
301 - indication of the use of floating points
302 - indication of the number of bytes used for
304 - use of special identifiers such as "setjmp"
305 - "end" + number of bytes used for local variables
308 char optionsn = options['n'];
313 #endif /* DATAFLOW */
314 C_df_ilb(return2_label);
315 if (return_expr_occurred) C_asp(-func_size);
316 C_df_ilb(return_label);
319 if (return_expr_occurred) {
320 if (func_res_label != 0) {
321 C_lae_dlb(func_res_label, (arith)0);
322 store_block(func_type->tp_size, func_type->tp_align);
323 C_lae_dlb(func_res_label, (arith)0);
329 else C_ret((arith) 0);
332 /* getting the number of "local" bytes is posponed until here,
333 because copying the function result in "func_res_label" may
334 need temporaries! However, local_level is now L_FORMAL2, because
335 L_LOCAL is already unstacked. Therefore, "unstack_level" must
336 also pass "sl_max_block" to the level above L_LOCAL.
338 nbytes = ATW(- local_level->sl_max_block);
341 C_pro(func_name, nbytes);
343 if (fbytes > max_int) {
344 error("%s has more than %ld parameter bytes",
345 func_name, (long) max_int);
347 C_ms_par(fbytes); /* # bytes for formals */
348 if (sp_occurred[SP_SETJMP]) { /* indicate use of "setjmp" */
351 sp_occurred[SP_SETJMP] = 0;
358 if (nbytes > max_int) {
359 error("%s has more than %ld bytes of local variables",
360 func_name, (long) max_int);
362 options['n'] = optionsn;
367 /* do_return handles the case of a return without expression.
368 This version branches to the return label, which is
369 probably smarter than generating a direct return.
370 Return sequences may be expensive.
373 if (options['g']) db_line(dot.tk_file, dot.tk_line);
374 #endif /* DBSYMTAB */
375 C_bra(return2_label);
381 /* do_return_expr() generates the expression and the jump for
382 a return statement with an expression.
384 ch7cast(&expr, RETURN, func_type);
385 code_expr(expr, RVAL, TRUE, NO_LABEL, NO_LABEL);
387 return_expr_occurred = 1;
390 code_declaration(idf, expr, lvl, sc)
391 register struct idf *idf; /* idf to be declared */
392 struct expr *expr; /* initialisation; NULL if absent */
393 int lvl; /* declaration level */
394 int sc; /* storage class, as in the declaration */
396 /* code_declaration() does the actual declaration of the
397 variable indicated by "idf" on declaration level "lvl".
398 If the variable is initialised, the expression is given
399 in "expr", but for global and static initialisations it
400 is just non-zero, as the expression is not parsed yet.
401 There are some cases to be considered:
402 - filter out typedefs, they don't correspond to code;
403 - global variables, coded only if initialized;
404 - local static variables;
405 - local automatic variables;
406 Since the expression may be modified in the process,
407 code_declaration() frees it after use, as the caller can
410 If there is a storage class indication (EXTERN/STATIC),
411 code_declaration() will generate an exa or ina.
412 The sc is the actual storage class, as given in the
413 declaration. This is to allow:
416 while at the same time forbidding
419 register struct def *def = idf->id_def;
420 register arith size = def->df_type->tp_size;
421 int def_sc = def->df_sc;
423 if (def_sc == TYPEDEF) { /* no code for typedefs */
426 stb_typedef(def->df_type, idf->id_text);
428 #endif /* DBSYMTAB */
431 if (sc == EXTERN && expr && !is_anon_idf(idf))
432 error("%s is extern; cannot initialize", idf->id_text);
433 if (lvl == L_GLOBAL) { /* global variable */
434 /* is this an allocating declaration? */
435 if ( (sc == 0 || sc == STATIC)
436 && def->df_type->tp_fund != FUNCTION
439 def->df_alloc = ALLOC_SEEN;
440 if (expr) { /* code only if initialized */
441 #ifndef PREPEND_SCOPES
442 code_scope(idf->id_text, def);
443 #endif /* PREPEND_SCOPES */
444 def->df_alloc = ALLOC_DONE;
445 C_df_dnam(idf->id_text);
449 if (lvl >= L_LOCAL) { /* local variable */
450 /* STATIC, EXTERN, GLOBAL, IMPLICIT, AUTO or REGISTER */
453 if (def->df_type->tp_fund == FUNCTION) {
454 /* should produce "inp $function" ??? */
457 /* they are handled on the spot and get an
461 if (options['g'] && ! expr) {
462 stb_string(def, sc, idf->id_text);
464 #endif /* DBSYMTAB */
465 C_df_dlb((label)def->df_address);
466 if (expr) { /* there is an initialisation */
468 else { /* produce blank space */
470 error("size of %s unknown", idf->id_text);
473 C_bss_cst(ATW(size), (arith)0, 1);
479 /* we are sure there is no expression */
485 stb_string(def, sc, idf->id_text);
487 #endif /* DBSYMTAB */
492 crash("bad local storage class");
500 register struct idf *id;
502 /* loc_init() generates code for the assignment of
503 expression expr to the local variable described by id.
504 It frees the expression afterwards.
506 register struct expr *e = expr;
507 register struct type *tp = id->id_def->df_type;
509 ASSERT(id->id_def->df_sc != STATIC);
510 switch (tp->tp_fund) {
514 error("automatic %s cannot be initialized in declaration",
515 symbol2str(tp->tp_fund));
519 if (ISCOMMA(e)) { /* embraced: int i = {12}; */
522 if (ISCOMMA(e->OP_LEFT)) /* int i = {{1}} */
523 expr_error(e, "extra braces not allowed");
525 if (e->OP_RIGHT != 0) /* int i = {1 , 2} */
526 expr_error(e, "too many initializers");
528 #endif /* NOROPTION */
530 loc_init(e->OP_LEFT, id);
534 else { /* not embraced */
535 ch7cast(&expr, '=', tp); /* may modify expr */
540 EVAL(expr, RVAL, TRUE, NO_LABEL, NO_LABEL);
542 vl.vl_data.vl_idf = id;
543 vl.vl_value = (arith)0;
547 id->id_def->df_set = 1;
549 free_expression(expr);
554 register struct idf *idf;
556 /* bss() allocates bss space for the global idf.
558 arith size = idf->id_def->df_type->tp_size;
560 #ifndef PREPEND_SCOPES
561 code_scope(idf->id_text, idf->id_def);
562 #endif /* PREPEND_SCOPES */
565 stb_string(idf->id_def, idf->id_def->df_sc, idf->id_text);
567 #endif /* DBSYMTAB */
568 /* Since bss() is only called if df_alloc is non-zero, and
569 since df_alloc is only non-zero if size >= 0, we have:
571 /* but we already gave a warning at the declaration of the
572 array. Besides, the message given here does not apply to
575 if (options['R'] && size == 0)
576 warning("actual array of size 0");
578 C_df_dnam(idf->id_text);
579 C_bss_cst(ATW(size), (arith)0, 1);
583 register struct def *df;
585 /* formal_cvt() converts a formal parameter of type char or
586 short from int to that type.
588 register struct type *tp = df->df_type;
590 if (tp->tp_size != int_size &&
591 (tp->tp_fund == CHAR || tp->tp_fund == SHORT)
593 LoadLocal(df->df_address, int_size);
594 /* conversion(int_type, df->df_type); ???
595 No, you can't do this on the stack! (CJ)
597 StoreLocal(df->df_address, tp->tp_size);
604 code_expr(expr, val, code, tlbl, flbl)
608 /* code_expr() is the parser's interface to the expression code
609 generator. If line number trace is wanted, it generates a
610 lin instruction. EVAL() is called directly.
613 if (! options['L']) /* profiling */
614 C_lin((arith)(expr->ex_line));
616 if (options['g']) db_line(expr->ex_file, (unsigned int)expr->ex_line);
618 EVAL(expr, val, code, tlbl, flbl);
620 lint_expr(expr, code ? USED : IGNORED);
624 /* The FOR/WHILE/DO/SWITCH stacking mechanism:
625 stack_stmt() has to be called at the entrance of a
626 for, while, do or switch statement to indicate the
627 EM labels where a subsequent break or continue causes
628 the program to jump to.
630 static struct stmt_block *stmt_stack; /* top of statement stack */
632 /* code_break() generates EM code needed at the occurrence of "break":
633 it generates a branch instruction to the break label of the
634 innermost statement in which break has a meaning.
635 As "break" is legal in any of 'while', 'do', 'for' or 'switch',
636 which are the only ones that are stacked, only the top of
637 the stack is interesting.
641 register struct stmt_block *stmt_block = stmt_stack;
644 if (options['g']) db_line(dot.tk_file, dot.tk_line);
645 #endif /* DBSYMTAB */
647 C_bra(stmt_block->st_break);
649 error("break not inside for, while, do or switch");
652 /* code_continue() generates EM code needed at the occurrence of
654 it generates a branch instruction to the continue label of the
655 innermost statement in which continue has a meaning.
659 register struct stmt_block *stmt_block = stmt_stack;
662 if (stmt_block->st_continue) {
664 if (options['g']) db_line(dot.tk_file, dot.tk_line);
665 #endif /* DBSYMTAB */
666 C_bra(stmt_block->st_continue);
669 stmt_block = stmt_block->next;
671 error("continue not inside for, while or do");
674 stack_stmt(break_label, cont_label)
675 label break_label, cont_label;
677 register struct stmt_block *stmt_block = new_stmt_block();
679 stmt_block->next = stmt_stack;
680 stmt_block->st_break = break_label;
681 stmt_block->st_continue = cont_label;
682 stmt_stack = stmt_block;
687 /* unstack_stmt() unstacks the data of a statement
688 which may contain break or continue
690 register struct stmt_block *sbp = stmt_stack;
691 stmt_stack = sbp->next;
692 free_stmt_block(sbp);
695 static label prc_name;
701 C_df_dlb(prc_name = data_label());
702 C_rom_scon(name, (arith) (strlen(name) + 1));
703 C_lae_dlb(prc_name, (arith) 0);
712 C_lae_dlb(prc_name, (arith) 0);
723 static unsigned oldline;
724 static char *oldfile;
726 if (file != oldfile || line != oldline) {
727 C_ms_std((char *) 0, N_SLINE, (int) line);
732 #endif /* DBSYMTAB */