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 1.34 1997/07/01 08:30:31 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 */
17 #include "botch_free.h"
21 #include <flt_arith.h>
49 label datlab_count = 1;
52 extern arith NewLocal(); /* util.c */
54 /* global function info */
56 struct type *func_type;
66 extern char options[];
67 extern char *symbol2str();
74 /* init_code() initialises the output file on which the
75 compact EM code is written
77 C_init(word_size, pointer_size); /* initialise EM module */
78 if (C_open(dst_file) == 0)
79 fatal("cannot write to %s\n", dst_file);
81 C_ms_emx(word_size, pointer_size);
84 C_ms_std(source, N_SO, 0);
85 stb_typedef(int_type, "int");
86 stb_typedef(schar_type, "char");
87 stb_typedef(long_type, "long");
88 stb_typedef(short_type, "short");
89 stb_typedef(uchar_type, "unsigned char");
90 stb_typedef(ushort_type, "unsigned short");
91 stb_typedef(ulong_type, "unsigned long");
92 stb_typedef(uint_type, "unsigned int");
93 stb_typedef(float_type, "float");
94 stb_typedef(double_type, "double");
95 stb_typedef(lngdbl_type, "long double");
96 stb_typedef(void_type, "void");
100 #ifdef PREPEND_SCOPES
101 C_insertpart(tmp_id = C_getid());
102 #endif /* PREPEND_SCOPES */
107 struct string_cst *str_list = 0;
110 code_string(val, len)
114 register struct string_cst *sc = new_string_cst();
115 label dlb = data_label();
127 register struct string_cst *sc;
130 struct string_cst *sc1 = sc;
132 C_df_dlb(sc->sc_dlb);
133 str_cst(sc->sc_value, sc->sc_len, 1); /* string in rom */
136 free_string_cst(sc1);
140 /* flush_strings() is called from program.g after each external definition */
143 def_strings(str_list);
151 /* end_code() performs the actions to be taken when closing
155 /* floating point used */
158 C_ms_src((int)(LineNumber - 2), source);
163 #ifdef PREPEND_SCOPES
166 /* prepend_scopes() runs down the list of global idf's
167 and generates those exa's, exp's, ina's and inp's
168 that superior hindsight has provided.
170 register struct stack_entry *se = local_level->sl_entry;
176 register struct def *df = se->se_idf->id_def;
178 if (df && (df->df_initialized || df->df_used || df->df_alloc)) {
179 code_scope(se->se_idf->id_text, df);
187 #endif /* PREPEND_SCOPES */
189 code_scope(text, def)
191 register struct def *def;
193 /* generates code for one name, text, of the storage class
194 as given by def, if meaningful.
196 int fund = def->df_type->tp_fund;
198 switch (def->df_sc) {
201 if (fund == FUNCTION)
207 if (fund == FUNCTION)
215 static label return_label, return2_label;
216 static char return_expr_occurred;
217 static arith func_size;
218 static int struct_return;
219 static char *last_fn_given = (char *)0;
220 static label file_name_label;
222 begin_proc(ds, idf) /* to be called when entering a procedure */
226 /* begin_proc() is called at the entrance of a new function
227 and performs the necessary code generation:
228 - a scope indicator (if needed) exp/inp
229 - the procedure entry pro $name
230 - reserves some space if the result of the function
231 does not fit in the return area
232 - a fil pseudo instruction
234 register char *name = idf->id_text;
235 register struct def *def = idf->id_def;
237 /* idf->id_def does not indicate the right def structure
238 * when the function being defined has a parameter of the
239 * same name in an old-style function definition.
241 while (def->df_level != L_GLOBAL) def = def->next;
243 /* When we have a new-style function definition, the parameters
244 * are defined first, which means that it may look as if they have
245 * the greatest scope. Correct this.
247 if (idf->id_def == def && def->next && def->next->df_level == L_PROTO) {
248 struct def *tmpdef = def->next;
250 def->next = tmpdef->next;
252 idf->id_def = tmpdef;
254 #ifndef PREPEND_SCOPES
255 code_scope(name, def);
256 #endif /* PREPEND_SCOPES */
259 DfaStartFunction(name);
260 #endif /* DATAFLOW */
263 /* set global function info */
265 if (def->df_type->tp_fund != FUNCTION) {
266 error("making function body for non-function");
267 def->df_type = error_type;
269 func_type = def->df_type->tp_up;
271 func_notypegiven = ds->ds_notypegiven;
273 func_size = ATW(func_type->tp_size);
274 sp_occurred[SP_SETJMP] = 0;
279 C_insertpart(pro_id = C_getid());
281 if (is_struct_or_union(func_type->tp_fund)) {
282 if (func_size <= 0) {
283 error("unknown return type for function %s", name);
290 /* Special arrangements if the function result doesn't fit in
291 the function return area of the EM machine. The size of
292 the function return area is implementation dependent.
294 lab_count = (label) 1;
295 return_label = text_label();
296 return2_label = text_label();
297 return_expr_occurred = 0;
300 if (! options['L']) { /* profiling */
301 if (!last_fn_given || strcmp(last_fn_given, FileName) != 0) {
302 /* previous function came from other file */
303 C_df_dlb(file_name_label = data_label());
304 C_con_scon(last_fn_given = FileName,
305 (arith)(strlen(FileName) + 1));
307 /* enable debug trace of EM source */
308 C_fil_dlb(file_name_label, (arith)0);
309 C_lin((arith)LineNumber);
313 stb_string(def, FUNCTION, name);
314 if (! strcmp(name, "main")) {
315 C_ms_stb_cst(name, N_MAIN, 0, (arith) 0);
324 /* end_proc() deals with the code to be generated at the end of
325 a function, as there is:
326 - the EM ret instruction: "ret 0"
327 - loading of the function result in the function
328 result area if there has been a return <expr>
329 in the function body (see do_return_expr())
330 - indication of the use of floating points
331 - indication of the number of bytes used for
333 - use of special identifiers such as "__setjmp"
334 - "end" + number of bytes used for local variables
337 char optionsn = options['n'];
342 #endif /* DATAFLOW */
343 C_df_ilb(return2_label);
344 if (return_expr_occurred && struct_return == 0) {
347 C_df_ilb(return_label);
349 if (return_expr_occurred) {
350 if (struct_return != 0) {
351 LoadLocal((arith) 0, pointer_size);
357 else C_ret((arith) 0);
359 /* getting the number of "local" bytes is posponed until here,
360 because copying the function result may need temporaries!
361 However, local_level is now L_FORMAL2, because
362 L_LOCAL is already unstacked. Therefore, "unstack_level" must
363 also pass "sl_max_block" to the level above L_LOCAL.
365 nbytes = ATW(- local_level->sl_max_block);
368 C_pro(func_name, nbytes);
370 if (fbytes > max_int) {
371 error("%s has more than %ld parameter bytes",
372 func_name, (long) max_int);
374 C_ms_par(fbytes); /* # bytes for formals */
375 if (sp_occurred[SP_SETJMP]) { /* indicate use of "__setjmp" */
378 sp_occurred[SP_SETJMP] = 0;
385 if (nbytes > max_int) {
386 error("%s has more than %ld bytes of local variables",
387 func_name, (long) max_int);
389 options['n'] = optionsn;
394 /* do_return handles the case of a return without expression.
395 This version branches to the return label, which is
396 probably smarter than generating a direct return.
397 Return sequences may be expensive.
400 if (options['g']) db_line(dot.tk_file, dot.tk_line);
401 #endif /* DBSYMTAB */
402 C_bra(return2_label);
408 /* do_return_expr() generates the expression and the jump for
409 a return statement with an expression.
411 ch3cast(&expr, RETURN, func_type);
412 code_expr(expr, RVAL, TRUE, NO_LABEL, NO_LABEL);
413 if (struct_return != 0) {
414 LoadLocal((arith) 0, pointer_size);
415 store_block(func_type->tp_size, func_type->tp_align);
418 return_expr_occurred = 1;
421 code_declaration(idf, expr, lvl, sc)
422 register struct idf *idf; /* idf to be declared */
423 struct expr *expr; /* initialisation; NULL if absent */
424 int lvl; /* declaration level */
425 int sc; /* storage class, as in the declaration */
427 /* code_declaration() does the actual declaration of the
428 variable indicated by "idf" on declaration level "lvl".
429 If the variable is initialised, the expression is given
430 in "expr", but for global and static initialisations it
431 is just non-zero, as the expression is not parsed yet.
432 There are some cases to be considered:
433 - filter out typedefs, they don't correspond to code;
434 - global variables, coded only if initialized;
435 - local static variables;
436 - local automatic variables;
437 Since the expression may be modified in the process,
438 code_declaration() frees it after use, as the caller can
441 If there is a storage class indication (EXTERN/STATIC),
442 code_declaration() will generate an exa or ina.
443 The sc is the actual storage class, as given in the
446 register struct def *def = idf->id_def;
447 register arith size = def->df_type->tp_size;
448 int fund = def->df_type->tp_fund;
449 int def_sc = def->df_sc;
451 if (def_sc == TYPEDEF) { /* no code for typedefs */
454 stb_typedef(def->df_type, idf->id_text);
456 #endif /* DBSYMTAB */
459 if (lvl == L_GLOBAL) { /* global variable */
460 /* is this an allocating declaration? */
461 if ( (sc == 0 || sc == STATIC)
464 def->df_alloc = ALLOC_SEEN;
465 if (expr && def_sc == STATIC && sc == EXTERN) {
466 warning("%s has internal linkage", idf->id_text);
468 if (expr) { /* code only if initialized */
469 #ifndef PREPEND_SCOPES
470 code_scope(idf->id_text, def);
471 #endif /* PREPEND_SCOPES */
472 def->df_alloc = ALLOC_DONE;
473 C_df_dnam(idf->id_text);
477 if (lvl >= L_LOCAL) { /* local variable */
478 /* STATIC, EXTERN, GLOBAL, AUTO or REGISTER */
481 if (fund == FUNCTION) {
482 /* should produce "inp $function" ??? */
485 /* they are handled on the spot and get an
489 if (options['g'] && ! expr) {
490 stb_string(def, sc, idf->id_text);
492 #endif /* DBSYMTAB */
493 C_df_dlb((label)def->df_address);
494 if (expr) { /* there is an initialisation */
496 else { /* produce blank space */
498 error("size of %s unknown", idf->id_text);
501 C_bss_cst(ATW(size), (arith)0, 1);
505 if (expr && !is_anon_idf(idf) && level != L_GLOBAL)
506 error("cannot initialize extern %s in block"
509 /* we are sure there is no expression */
515 stb_string(def, sc, idf->id_text);
517 #endif /* DBSYMTAB */
520 else if ((fund == ARRAY)
521 && (def->df_type->tp_size == (arith)-1)) {
522 error("size for local %s unknown"
527 crash("bad local storage class");
537 /* loc_init() generates code for the assignment of
538 expression expr to the local variable described by id.
539 It frees the expression afterwards.
541 register struct expr *e = expr;
542 register struct def *df = id->id_def;
543 register struct type *tp = df->df_type;
544 static arith tmpoffset = 0;
545 static arith unknownsize = 0;
547 ASSERT(df->df_sc != STATIC);
548 switch (tp->tp_fund) {
550 if (tp->tp_size == (arith) -1)
554 if (e != (struct expr *) 0) {
556 } else if (!tmpoffset) {/* first time for this variable */
557 tmpoffset = df->df_address;
558 if (unknownsize) tmpoffset = -1;
559 df->df_address = data_label();
560 C_df_dlb((label)df->df_address);
562 C_lae_dlb((label)df->df_address, (arith)0);
563 load_block(tp->tp_size, word_align);
565 /* tmpoffset += tp->tp_size; */
568 tmpoffset = NewLocal(tp->tp_size
574 store_block(tp->tp_size, tmpoffset % word_align ? 1 : word_align);
575 df->df_address = tmpoffset;
580 stb_string(df, AUTO, id->id_text);
583 #endif /* DBSYMTAB */
586 if (ISCOMMA(e)) { /* embraced: int i = {12}; */
588 loc_init(e->OP_LEFT, id);
592 else { /* not embraced */
593 ch3cast(&expr, '=', tp); /* may modify expr */
598 EVAL(expr, RVAL, TRUE, NO_LABEL, NO_LABEL);
600 vl.vl_data.vl_idf = id;
601 vl.vl_value = (arith)0;
605 id->id_def->df_set = 1;
607 free_expression(expr);
612 register struct idf *idf;
614 /* bss() allocates bss space for the global idf.
616 register struct def *df = idf->id_def;
618 #ifndef PREPEND_SCOPES
619 code_scope(idf->id_text, df);
620 #endif /* PREPEND_SCOPES */
623 stb_string(df, df->df_sc, idf->id_text);
625 #endif /* DBSYMTAB */
626 if (df->df_type->tp_size <= 0) {
627 if (df->df_sc != STATIC &&
628 df->df_type->tp_fund == ARRAY &&
629 df->df_type->tp_up &&
630 df->df_type->tp_up->tp_size >= 0) {
631 C_df_dnam(idf->id_text);
632 C_bss_cst(ATW(df->df_type->tp_up->tp_size), (arith)0, 1);
634 else error("size of %s unknown (\"%s\", line %d)"
635 , idf->id_text, df->df_file, df->df_line);
637 C_df_dnam(idf->id_text);
638 C_bss_cst(ATW(df->df_type->tp_size), (arith)0, 1);
642 formal_cvt(hasproto,df)
644 register struct def *df;
646 /* formal_cvt() converts a formal parameter of type char or
647 short from int to that type. It also converts a formal
648 parameter of type float from a double to a float.
650 register struct type *tp = df->df_type;
652 if (tp->tp_size != int_size &&
653 (tp->tp_fund == CHAR || tp->tp_fund == SHORT)
655 LoadLocal(df->df_address, int_size);
656 /* conversion(int_type, df->df_type); ???
657 No, you can't do this on the stack! (CJ)
659 StoreLocal(df->df_address, tp->tp_size);
660 } else if (tp->tp_size != double_size
661 && tp->tp_fund == FLOAT
663 LoadLocal(df->df_address, double_size);
665 conversion(double_type, float_type);
667 StoreLocal(df->df_address, tp->tp_size);
674 code_expr(expr, val, code, tlbl, flbl)
678 /* code_expr() is the parser's interface to the expression code
679 generator. If line number trace is wanted, it generates a
680 lin instruction. EVAL() is called directly.
683 if (! options['L']) /* profiling */
684 C_lin((arith)(expr->ex_line));
686 if (options['g']) db_line(expr->ex_file, (unsigned int)expr->ex_line);
688 EVAL(expr, val, code, tlbl, flbl);
690 lint_expr(expr, code ? USED : IGNORED);
694 /* The FOR/WHILE/DO/SWITCH stacking mechanism:
695 stack_stmt() has to be called at the entrance of a
696 for, while, do or switch statement to indicate the
697 EM labels where a subsequent break or continue causes
698 the program to jump to.
700 static struct stmt_block *stmt_stack; /* top of statement stack */
702 /* code_break() generates EM code needed at the occurrence of "break":
703 it generates a branch instruction to the break label of the
704 innermost statement in which break has a meaning.
705 As "break" is legal in any of 'while', 'do', 'for' or 'switch',
706 which are the only ones that are stacked, only the top of
707 the stack is interesting.
711 register struct stmt_block *stmt_block = stmt_stack;
714 if (options['g']) db_line(dot.tk_file, dot.tk_line);
715 #endif /* DBSYMTAB */
717 C_bra(stmt_block->st_break);
719 error("break not inside for, while, do or switch");
722 /* code_continue() generates EM code needed at the occurrence of
724 it generates a branch instruction to the continue label of the
725 innermost statement in which continue has a meaning.
729 register struct stmt_block *stmt_block = stmt_stack;
732 if (stmt_block->st_continue) {
734 if (options['g']) db_line(dot.tk_file, dot.tk_line);
735 #endif /* DBSYMTAB */
736 C_bra(stmt_block->st_continue);
739 stmt_block = stmt_block->next;
741 error("continue not inside for, while or do");
744 stack_stmt(break_label, cont_label)
745 label break_label, cont_label;
747 register struct stmt_block *stmt_block = new_stmt_block();
749 stmt_block->next = stmt_stack;
750 stmt_block->st_break = break_label;
751 stmt_block->st_continue = cont_label;
752 stmt_stack = stmt_block;
757 /* unstack_stmt() unstacks the data of a statement
758 which may contain break or continue
760 register struct stmt_block *sbp = stmt_stack;
761 stmt_stack = sbp->next;
762 free_stmt_block(sbp);
765 static label prc_name;
771 C_df_dlb(prc_name = data_label());
772 C_rom_scon(name, (arith) (strlen(name) + 1));
773 C_lae_dlb(prc_name, (arith) 0);
782 C_lae_dlb(prc_name, (arith) 0);
793 static unsigned oldline;
794 static char *oldfile;
796 if (file != oldfile || line != oldline) {
797 C_ms_std((char *) 0, N_SLINE, (int) line);
802 #endif /* DBSYMTAB */