Debian Codegraph @

arch/nova/local.c

raw symbols SCIP
1 /* $Id: local.c,v 1.21 2022/01/11 08:22:37 ragge Exp $ */
2 /*
3 * Copyright (c) 2003 Anders Magnusson ([email protected]).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27
28 # include "pass1.h"
29 # include "unicode.h"
30
31 #ifdef LANG_CXX
32 #define P1ND NODE
33 #define p1alloc talloc
34 #define p1nfree nfree
35 #define p1fwalk fwalk
36 #define p1tcopy ccopy
37 #endif
38
39 /* this file contains code which is dependent on the target machine */
40
41 P1ND *
42 clocal(P1ND *p)
43 {
44 struct symtab *q;
45 P1ND *r, *l;
46 TWORD t;
47 int o;
48
49 #ifdef PCC_DEBUG
50 if (xdebug) {
51 printf("clocal\n");
52 p1fwalk(p, eprint, 0);
53 }
54 #endif
55
56 switch( o = p->n_op ){
57 case NAME:
58 /* handle variables */
59 if ((q = p->n_sp) == NULL)
60 return p; /* Nothing to care about */
61 switch (q->sclass) {
62 case PARAM:
63 case AUTO:
64 /* fake up a structure reference */
65 r = block(REG, NULL, NULL, PTR+STRTY, 0, 0);
66 slval(r, 0);
67 r->n_rval = FPREG;
68 p = stref(block(STREF, r, p, 0, 0, 0));
69 break;
70 default:
71 break;
72 }
73 break;
74
75 case PMCONV:
76 case PVCONV:
77 if( p->n_right->n_op != ICON ) cerror( "bad conversion", 0);
78 r = (buildtree(o==PMCONV?MUL:DIV, p->n_left, p->n_right));
79 p1nfree(p);
80 p = r;
81 break;
82
83 case PCONV:
84 t = p->n_type;
85 if (t == INCREF(CHAR) || t == INCREF(UCHAR) ||
86 t == INCREF(BOOL) || t == INCREF(VOID))
87 break;
88 l = p->n_left;
89 t = l->n_type;
90 if (t == INCREF(CHAR) || t == INCREF(UCHAR) ||
91 t == INCREF(BOOL) || t == INCREF(VOID))
92 break;
93 if (p->n_type <= UCHAR || l->n_type <= UCHAR)
94 break; /* must do runtime ptr conv */
95 /* if conversion to another pointer type, just remove */
96 if (p->n_type > BTMASK && l->n_type > BTMASK)
97 goto delp;
98 if (l->n_op == ICON && l->n_sp == NULL)
99 goto delp;
100 break;
101
102 delp: l->n_type = p->n_type;
103 l->n_qual = p->n_qual;
104 l->n_df = p->n_df;
105 l->n_ap = p->n_ap;
106 p = p1nfree(p);
107 break;
108
109 case FORCE:
110 /* put return value in return reg */
111 p->n_op = ASSIGN;
112 p->n_right = p->n_left;
113 p->n_left = block(REG, NULL, NULL, p->n_type, 0, 0);
114 p->n_left->n_rval = RETREG(p->n_type);
115 break;
116
117 }
118
119 #ifdef PCC_DEBUG
120 if (xdebug) {
121 printf("clocal end\n");
122 p1fwalk(p, eprint, 0);
123 }
124 #endif
125 return(p);
126 }
127
128 void
129 myp2tree(P1ND *p)
130 {
131 struct symtab *sp;
132 P1ND *l, *r;
133 int o = p->n_op;
134
135 switch (o) {
136 case NAME: /* reading from a name must be done with a subroutine */
137 if (p->n_type != CHAR && p->n_type != UCHAR)
138 break;
139 l = buildtree(ADDROF, p1tcopy(p), NULL);
140 r = block(NAME, NULL, NULL, INT, 0, 0);
141
142 r->n_sp = lookup(addname("__nova_rbyte"), SNORMAL);
143 if (r->n_sp->sclass == SNULL) {
144 r->n_sp->sclass = EXTERN;
145 r->n_sp->stype = INCREF(p->n_type)+(FTN-PTR);
146 }
147 r->n_type = r->n_sp->stype;
148 r = clocal(r);
149 r = optim(buildtree(CALL, r, l));
150 *p = *r;
151 p1nfree(r);
152 break;
153
154 case FCON:
155 sp = tmpalloc(sizeof(struct symtab));
156 sp->sclass = STATIC;
157 sp->sap = 0;
158 sp->slevel = 1; /* fake numeric label */
159 sp->soffset = getlab();
160 sp->sflags = 0;
161 sp->stype = p->n_type;
162 sp->squal = (CON >> TSHIFT);
163
164 defloc(sp);
165 ninval(0, tsize(sp->stype, sp->sdf, sp->sap), p);
166
167 p->n_op = NAME;
168 slval(p, 0);
169 p->n_sp = sp;
170 }
171 }
172
173 /*ARGSUSED*/
174 int
175 andable(P1ND *p)
176 {
177 return(1); /* all names can have & taken on them */
178 }
179
180 /*
181 * Return 1 if a variable of type type is OK to put in register.
182 */
183 int
184 cisreg(TWORD t)
185 {
186 return 1; /* try to put anything in a register */
187 }
188
189 /*
190 * return a node, for structure references, which is suitable for
191 * being added to a pointer of type t, in order to be off bits offset
192 * into a structure
193 * t, d, and s are the type, dimension offset, and sizeoffset
194 * For nova, return the type-specific index number which calculation
195 * is based on its size. For example, char a[3] would return 3.
196 * Be careful about only handling first-level pointers, the following
197 * indirections must be fullword.
198 */
199 P1ND *
200 offcon(OFFSZ off, TWORD t, union dimfun *d, struct attr *ap)
201 {
202 register P1ND *p;
203
204 if (xdebug)
205 printf("offcon: OFFSZ %ld type %x dim %p siz %ld\n",
206 off, t, d, tsize(t, d, ap));
207
208 p = bcon(off/SZINT);
209 if (t == INCREF(CHAR) || t == INCREF(UCHAR) || t == INCREF(VOID))
210 slval(p, off/SZCHAR); /* pointer to char */
211 return(p);
212 }
213
214 /*
215 * Allocate off bits on the stack. p is a tree that when evaluated
216 * is the multiply count for off, t is a NAME node where to write
217 * the allocated address.
218 */
219 void
220 spalloc(P1ND *t, P1ND *p, OFFSZ off)
221 {
222 P1ND *sp;
223
224 cerror("spalloc");
225 if ((off % SZINT) == 0)
226 p = buildtree(MUL, p, bcon(off/SZINT));
227 else if ((off % SZSHORT) == 0) {
228 p = buildtree(MUL, p, bcon(off/SZSHORT));
229 p = buildtree(PLUS, p, bcon(1));
230 p = buildtree(RS, p, bcon(1));
231 } else if ((off % SZCHAR) == 0) {
232 p = buildtree(MUL, p, bcon(off/SZCHAR));
233 p = buildtree(PLUS, p, bcon(3));
234 p = buildtree(RS, p, bcon(2));
235 } else
236 cerror("roundsp");
237
238 /* save the address of sp */
239 sp = block(REG, NULL, NULL, PTR+INT, t->n_df, t->n_ap);
240 slval(sp, 0);
241 sp->n_rval = STKREG;
242 t->n_type = sp->n_type;
243 ecomp(buildtree(ASSIGN, t, sp)); /* Emit! */
244
245 /* add the size to sp */
246 sp = block(REG, NULL, NULL, p->n_type, 0, 0);
247 slval(sp, 0);
248 sp->n_rval = STKREG;
249 ecomp(buildtree(PLUSEQ, sp, p));
250 }
251
252 #ifdef os_none
253 /*
254 * Print out a string of characters.
255 * Assume that the assembler understands C-style escape
256 * sequences.
257 */
258 void
259 instring(struct symtab *sp)
260 {
261 unsigned short sh[2];
262 char *s;
263 TWORD t;
264 P1ND *p;
265
266 locctr(STRNG, sp);
267 defloc(sp);
268
269 t = BTYPE(sp->stype);
270 s = sp->sname;
271 if (t == ctype(USHORT)) {
272 /* convert to UTF-16 */
273 p = xbcon(0, NULL, t);
274 while (*s) {
275 cp2u16(u82cp(&s), sh);
276 if ((glval(p) = sh[0]))
277 inval(0, SZSHORT, p);
278 if ((glval(p) = sh[1]))
279 inval(0, SZSHORT, p);
280 }
281 slval(p, 0);
282 inval(0, SZSHORT, p);
283 p1nfree(p);
284 } else if (t == ctype(SZINT < 32 ? ULONG : UNSIGNED) ||
285 t == ctype(SZINT < 32 ? LONG : INT)) {
286 /* convert to UTF-32 */
287 p = xbcon(0, NULL, t);
288 while (*s) {
289 slval(p, u82cp(&s));
290 inval(0, SZINT < 32 ? SZLONG : SZINT, p);
291 }
292 slval(p, 0);
293 inval(0, SZINT < 32 ? SZLONG : SZINT, p);
294 p1nfree(p);
295 } else if (t == CHAR || t == UCHAR) {
296 int chno = 0;
297 printf(PRTPREF "\t.TXT \"");
298 for (; *s; s++) {
299 if (*s == '\\') {
300 printf("<%o>", esccon(&s));
301 } else
302 printf("%c", *s);
303
304 if (chno++ >= 60) {
305 printf("\r\n" PRTPREF);
306 chno = 0;
307 }
308 }
309
310 printf("\"\n");
311 } else
312 cerror("instring %ld", t);
313 }
314 #endif
315
316 /*
317 * print out a constant node
318 * mat be associated with a label
319 */
320 int
321 ninval(CONSZ off, int fsz, P1ND *p)
322 {
323 switch (p->n_type) {
324 case FLOAT:
325 case DOUBLE:
326 case LDOUBLE:
327 cerror("ninval");
328 }
329 return 1;
330 }
331
332 /* make a name look like an external name in the local machine */
333 char *
334 exname(char *p)
335 {
336 #define NCHNAM 256
337 static char text[NCHNAM+1];
338 int i;
339
340 if (p == NULL)
341 return "";
342
343 text[0] = '_';
344 for (i=1; *p && i<NCHNAM; ++i)
345 text[i] = *p++;
346
347 text[i] = '\0';
348 text[NCHNAM] = '\0'; /* truncate */
349
350 return (text);
351 }
352
353 /*
354 * map types which are not defined on the local machine
355 */
356 TWORD
357 ctype(TWORD type)
358 {
359 switch (BTYPE(type)) {
360 case LONGLONG:
361 MODTYPE(type,LONG);
362 break;
363
364 case ULONGLONG:
365 MODTYPE(type,ULONG);
366 break;
367 case SHORT:
368 MODTYPE(type,INT);
369 break;
370 case USHORT:
371 MODTYPE(type,UNSIGNED);
372 break;
373 }
374 return (type);
375 }
376
377 #if 0
378 /* curid is a variable which is defined but
379 * is not initialized (and not a function );
380 * This routine returns the storage class for an uninitialized declaration
381 */
382 int
383 noinit()
384 {
385 return(EXTERN);
386 }
387 #endif
388
389 void
390 calldec(P1ND *p, P1ND *q)
391 {
392 }
393
394 void
395 extdec(struct symtab *q)
396 {
397 }
398
399 #if 0
400 /* make a common declaration for id, if reasonable */
401 void
402 commdec(struct symtab *q)
403 {
404 int off;
405
406 off = tsize(q->stype, q->sdf, q->ssue);
407 off = (off+(SZCHAR-1))/SZCHAR;
408 printf(" .comm %s,0%o\n", exname(q->soname), off);
409 }
410
411 /* make a local common declaration for id, if reasonable */
412 void
413 lcommdec(struct symtab *q)
414 {
415 int off;
416
417 off = tsize(q->stype, q->sdf, q->ssue);
418 off = (off+(SZCHAR-1))/SZCHAR;
419 if (q->slevel == 0)
420 printf(" .lcomm %s,0%o\n", exname(q->soname), off);
421 else
422 printf(" .lcomm " LABFMT ",0%o\n", q->soffset, off);
423 }
424
425 /*
426 * print a (non-prog) label.
427 */
428 void
429 deflab1(int label)
430 {
431 printf(LABFMT ":\n", label);
432 }
433 #endif
434
435 /*
436 * Give target the opportunity of handling pragmas.
437 */
438 int
439 mypragma(char *str)
440 {
441 return 0;
442 }
443
444 /*
445 * Called when a identifier has been declared, to give target last word.
446 */
447 void
448 fixdef(struct symtab *sp)
449 {
450 }
451
452 void
453 pass1_lastchance(struct interpass *ip)
454 {
455 }