1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1 |
// Copyright 2009 The Go Authors. All rights reserved.
|
2 |
// Use of this source code is governed by a BSD-style
|
|
3 |
// license that can be found in the LICENSE file.
|
|
4 |
||
5 |
#undef EXTERN
|
|
6 |
#define EXTERN
|
|
7 |
#include "gg.h" |
|
8 |
#include "opt.h" |
|
9 |
||
10 |
void
|
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
11 |
defframe(Prog *ptxt) |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
12 |
{
|
13 |
// fill in argument size
|
|
14 |
ptxt->to.offset = rnd(curfn->type->argwid, widthptr); |
|
15 |
||
16 |
// fill in final stack size
|
|
17 |
ptxt->to.offset <<= 32; |
|
18 |
ptxt->to.offset |= rnd(stksize+maxarg, widthptr); |
|
19 |
}
|
|
20 |
||
1.1.8
by Ondřej Surý
Import upstream version 59 |
21 |
// Sweep the prog list to mark any used nodes.
|
22 |
void
|
|
23 |
markautoused(Prog* p) |
|
24 |
{
|
|
25 |
for (; p; p = p->link) { |
|
26 |
if (p->from.type == D_AUTO && p->from.node) |
|
27 |
p->from.node->used++; |
|
28 |
||
29 |
if (p->to.type == D_AUTO && p->to.node) |
|
30 |
p->to.node->used++; |
|
31 |
}
|
|
32 |
}
|
|
33 |
||
34 |
// Fixup instructions after compactframe has moved all autos around.
|
|
35 |
void
|
|
36 |
fixautoused(Prog* p) |
|
37 |
{
|
|
38 |
for (; p; p = p->link) { |
|
39 |
if (p->from.type == D_AUTO && p->from.node) |
|
40 |
p->from.offset += p->from.node->stkdelta; |
|
41 |
||
42 |
if (p->to.type == D_AUTO && p->to.node) |
|
43 |
p->to.offset += p->to.node->stkdelta; |
|
44 |
}
|
|
45 |
}
|
|
46 |
||
1.1.6
by Ondřej Surý
Import upstream version 58 |
47 |
|
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
48 |
/*
|
49 |
* generate:
|
|
50 |
* call f
|
|
51 |
* proc=0 normal call
|
|
52 |
* proc=1 goroutine run in new proc
|
|
53 |
* proc=2 defer call save away stack
|
|
54 |
*/
|
|
55 |
void
|
|
56 |
ginscall(Node *f, int proc) |
|
57 |
{
|
|
58 |
Prog *p; |
|
59 |
Node reg, con; |
|
60 |
||
61 |
switch(proc) { |
|
62 |
default: |
|
63 |
fatal("ginscall: bad proc %d", proc); |
|
64 |
break; |
|
65 |
||
66 |
case 0: // normal call |
|
67 |
p = gins(ACALL, N, f); |
|
68 |
afunclit(&p->to); |
|
69 |
break; |
|
70 |
||
71 |
case 1: // call in new proc (go) |
|
72 |
case 2: // deferred call (defer) |
|
73 |
nodreg(®, types[TINT64], D_CX); |
|
74 |
gins(APUSHQ, f, N); |
|
75 |
nodconst(&con, types[TINT32], argsize(f->type)); |
|
76 |
gins(APUSHQ, &con, N); |
|
77 |
if(proc == 1) |
|
78 |
ginscall(newproc, 0); |
|
79 |
else { |
|
80 |
if(!hasdefer) |
|
81 |
fatal("hasdefer=0 but has defer"); |
|
82 |
ginscall(deferproc, 0); |
|
83 |
}
|
|
84 |
gins(APOPQ, N, ®); |
|
85 |
gins(APOPQ, N, ®); |
|
86 |
if(proc == 2) { |
|
87 |
nodreg(®, types[TINT64], D_AX); |
|
88 |
gins(ATESTQ, ®, ®); |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
89 |
patch(gbranch(AJNE, T), retpc); |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
90 |
}
|
91 |
break; |
|
92 |
}
|
|
93 |
}
|
|
94 |
||
95 |
/*
|
|
96 |
* n is call to interface method.
|
|
97 |
* generate res = n.
|
|
98 |
*/
|
|
99 |
void
|
|
100 |
cgen_callinter(Node *n, Node *res, int proc) |
|
101 |
{
|
|
102 |
Node *i, *f; |
|
103 |
Node tmpi, nodo, nodr, nodsp; |
|
104 |
||
105 |
i = n->left; |
|
106 |
if(i->op != ODOTINTER) |
|
107 |
fatal("cgen_callinter: not ODOTINTER %O", i->op); |
|
108 |
||
109 |
f = i->right; // field |
|
110 |
if(f->op != ONAME) |
|
111 |
fatal("cgen_callinter: not ONAME %O", f->op); |
|
112 |
||
113 |
i = i->left; // interface |
|
114 |
||
115 |
if(!i->addable) { |
|
116 |
tempname(&tmpi, i->type); |
|
117 |
cgen(i, &tmpi); |
|
118 |
i = &tmpi; |
|
119 |
}
|
|
120 |
||
121 |
genlist(n->list); // assign the args |
|
122 |
||
123 |
regalloc(&nodr, types[tptr], res); |
|
124 |
regalloc(&nodo, types[tptr], &nodr); |
|
125 |
nodo.op = OINDREG; |
|
126 |
||
1.1.6
by Ondřej Surý
Import upstream version 58 |
127 |
agen(i, &nodr); // REG = &inter |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
128 |
|
129 |
nodindreg(&nodsp, types[tptr], D_SP); |
|
130 |
nodo.xoffset += widthptr; |
|
1.1.8
by Ondřej Surý
Import upstream version 59 |
131 |
cgen(&nodo, &nodsp); // 0(SP) = 8(REG) -- i.data |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
132 |
|
133 |
nodo.xoffset -= widthptr; |
|
1.1.8
by Ondřej Surý
Import upstream version 59 |
134 |
cgen(&nodo, &nodr); // REG = 0(REG) -- i.tab |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
135 |
|
136 |
nodo.xoffset = n->left->xoffset + 3*widthptr + 8; |
|
1.1.8
by Ondřej Surý
Import upstream version 59 |
137 |
cgen(&nodo, &nodr); // REG = 32+offset(REG) -- i.tab->fun[f] |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
138 |
|
139 |
// BOTCH nodr.type = fntype;
|
|
140 |
nodr.type = n->left->type; |
|
141 |
ginscall(&nodr, proc); |
|
142 |
||
143 |
regfree(&nodr); |
|
144 |
regfree(&nodo); |
|
145 |
||
146 |
setmaxarg(n->left->type); |
|
147 |
}
|
|
148 |
||
149 |
/*
|
|
150 |
* generate function call;
|
|
151 |
* proc=0 normal call
|
|
152 |
* proc=1 goroutine run in new proc
|
|
153 |
* proc=2 defer call save away stack
|
|
154 |
*/
|
|
155 |
void
|
|
156 |
cgen_call(Node *n, int proc) |
|
157 |
{
|
|
158 |
Type *t; |
|
159 |
Node nod, afun; |
|
160 |
||
161 |
if(n == N) |
|
162 |
return; |
|
163 |
||
164 |
if(n->left->ullman >= UINF) { |
|
165 |
// if name involves a fn call
|
|
166 |
// precompute the address of the fn
|
|
167 |
tempname(&afun, types[tptr]); |
|
168 |
cgen(n->left, &afun); |
|
169 |
}
|
|
170 |
||
171 |
genlist(n->list); // assign the args |
|
172 |
t = n->left->type; |
|
173 |
||
174 |
setmaxarg(t); |
|
175 |
||
176 |
// call tempname pointer
|
|
177 |
if(n->left->ullman >= UINF) { |
|
178 |
regalloc(&nod, types[tptr], N); |
|
179 |
cgen_as(&nod, &afun); |
|
180 |
nod.type = t; |
|
181 |
ginscall(&nod, proc); |
|
182 |
regfree(&nod); |
|
183 |
goto ret; |
|
184 |
}
|
|
185 |
||
186 |
// call pointer
|
|
187 |
if(n->left->op != ONAME || n->left->class != PFUNC) { |
|
188 |
regalloc(&nod, types[tptr], N); |
|
189 |
cgen_as(&nod, n->left); |
|
190 |
nod.type = t; |
|
191 |
ginscall(&nod, proc); |
|
192 |
regfree(&nod); |
|
193 |
goto ret; |
|
194 |
}
|
|
195 |
||
196 |
// call direct
|
|
197 |
n->left->method = 1; |
|
198 |
ginscall(n->left, proc); |
|
199 |
||
200 |
||
201 |
ret: |
|
202 |
;
|
|
203 |
}
|
|
204 |
||
205 |
/*
|
|
206 |
* call to n has already been generated.
|
|
207 |
* generate:
|
|
208 |
* res = return value from call.
|
|
209 |
*/
|
|
210 |
void
|
|
211 |
cgen_callret(Node *n, Node *res) |
|
212 |
{
|
|
213 |
Node nod; |
|
214 |
Type *fp, *t; |
|
215 |
Iter flist; |
|
216 |
||
217 |
t = n->left->type; |
|
218 |
if(t->etype == TPTR32 || t->etype == TPTR64) |
|
219 |
t = t->type; |
|
220 |
||
221 |
fp = structfirst(&flist, getoutarg(t)); |
|
222 |
if(fp == T) |
|
223 |
fatal("cgen_callret: nil"); |
|
224 |
||
225 |
memset(&nod, 0, sizeof(nod)); |
|
226 |
nod.op = OINDREG; |
|
227 |
nod.val.u.reg = D_SP; |
|
228 |
nod.addable = 1; |
|
229 |
||
230 |
nod.xoffset = fp->width; |
|
231 |
nod.type = fp->type; |
|
232 |
cgen_as(res, &nod); |
|
233 |
}
|
|
234 |
||
235 |
/*
|
|
236 |
* call to n has already been generated.
|
|
237 |
* generate:
|
|
238 |
* res = &return value from call.
|
|
239 |
*/
|
|
240 |
void
|
|
241 |
cgen_aret(Node *n, Node *res) |
|
242 |
{
|
|
243 |
Node nod1, nod2; |
|
244 |
Type *fp, *t; |
|
245 |
Iter flist; |
|
246 |
||
247 |
t = n->left->type; |
|
248 |
if(isptr[t->etype]) |
|
249 |
t = t->type; |
|
250 |
||
251 |
fp = structfirst(&flist, getoutarg(t)); |
|
252 |
if(fp == T) |
|
253 |
fatal("cgen_aret: nil"); |
|
254 |
||
255 |
memset(&nod1, 0, sizeof(nod1)); |
|
256 |
nod1.op = OINDREG; |
|
257 |
nod1.val.u.reg = D_SP; |
|
258 |
nod1.addable = 1; |
|
259 |
||
260 |
nod1.xoffset = fp->width; |
|
261 |
nod1.type = fp->type; |
|
262 |
||
263 |
if(res->op != OREGISTER) { |
|
264 |
regalloc(&nod2, types[tptr], res); |
|
265 |
gins(ALEAQ, &nod1, &nod2); |
|
266 |
gins(AMOVQ, &nod2, res); |
|
267 |
regfree(&nod2); |
|
268 |
} else |
|
269 |
gins(ALEAQ, &nod1, res); |
|
270 |
}
|
|
271 |
||
272 |
/*
|
|
273 |
* generate return.
|
|
274 |
* n->left is assignments to return values.
|
|
275 |
*/
|
|
276 |
void
|
|
277 |
cgen_ret(Node *n) |
|
278 |
{
|
|
279 |
genlist(n->list); // copy out args |
|
280 |
if(hasdefer || curfn->exit) |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
281 |
gjmp(retpc); |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
282 |
else
|
283 |
gins(ARET, N, N); |
|
284 |
}
|
|
285 |
||
286 |
/*
|
|
287 |
* generate += *= etc.
|
|
288 |
*/
|
|
289 |
void
|
|
290 |
cgen_asop(Node *n) |
|
291 |
{
|
|
292 |
Node n1, n2, n3, n4; |
|
293 |
Node *nl, *nr; |
|
294 |
Prog *p1; |
|
295 |
Addr addr; |
|
296 |
int a; |
|
297 |
||
298 |
nl = n->left; |
|
299 |
nr = n->right; |
|
300 |
||
301 |
if(nr->ullman >= UINF && nl->ullman >= UINF) { |
|
302 |
tempname(&n1, nr->type); |
|
303 |
cgen(nr, &n1); |
|
304 |
n2 = *n; |
|
305 |
n2.right = &n1; |
|
306 |
cgen_asop(&n2); |
|
307 |
goto ret; |
|
308 |
}
|
|
309 |
||
310 |
if(!isint[nl->type->etype]) |
|
311 |
goto hard; |
|
312 |
if(!isint[nr->type->etype]) |
|
313 |
goto hard; |
|
314 |
||
315 |
switch(n->etype) { |
|
316 |
case OADD: |
|
317 |
if(smallintconst(nr)) |
|
318 |
if(mpgetfix(nr->val.u.xval) == 1) { |
|
319 |
a = optoas(OINC, nl->type); |
|
320 |
if(nl->addable) { |
|
321 |
gins(a, N, nl); |
|
322 |
goto ret; |
|
323 |
}
|
|
324 |
if(sudoaddable(a, nl, &addr)) { |
|
325 |
p1 = gins(a, N, N); |
|
326 |
p1->to = addr; |
|
327 |
sudoclean(); |
|
328 |
goto ret; |
|
329 |
}
|
|
330 |
}
|
|
331 |
break; |
|
332 |
||
333 |
case OSUB: |
|
334 |
if(smallintconst(nr)) |
|
335 |
if(mpgetfix(nr->val.u.xval) == 1) { |
|
336 |
a = optoas(ODEC, nl->type); |
|
337 |
if(nl->addable) { |
|
338 |
gins(a, N, nl); |
|
339 |
goto ret; |
|
340 |
}
|
|
341 |
if(sudoaddable(a, nl, &addr)) { |
|
342 |
p1 = gins(a, N, N); |
|
343 |
p1->to = addr; |
|
344 |
sudoclean(); |
|
345 |
goto ret; |
|
346 |
}
|
|
347 |
}
|
|
348 |
break; |
|
349 |
}
|
|
350 |
||
351 |
switch(n->etype) { |
|
352 |
case OADD: |
|
353 |
case OSUB: |
|
354 |
case OXOR: |
|
355 |
case OAND: |
|
356 |
case OOR: |
|
357 |
a = optoas(n->etype, nl->type); |
|
358 |
if(nl->addable) { |
|
359 |
if(smallintconst(nr)) { |
|
360 |
gins(a, nr, nl); |
|
361 |
goto ret; |
|
362 |
}
|
|
363 |
regalloc(&n2, nr->type, N); |
|
364 |
cgen(nr, &n2); |
|
365 |
gins(a, &n2, nl); |
|
366 |
regfree(&n2); |
|
367 |
goto ret; |
|
368 |
}
|
|
369 |
if(nr->ullman < UINF) |
|
370 |
if(sudoaddable(a, nl, &addr)) { |
|
371 |
if(smallintconst(nr)) { |
|
372 |
p1 = gins(a, nr, N); |
|
373 |
p1->to = addr; |
|
374 |
sudoclean(); |
|
375 |
goto ret; |
|
376 |
}
|
|
377 |
regalloc(&n2, nr->type, N); |
|
378 |
cgen(nr, &n2); |
|
379 |
p1 = gins(a, &n2, N); |
|
380 |
p1->to = addr; |
|
381 |
regfree(&n2); |
|
382 |
sudoclean(); |
|
383 |
goto ret; |
|
384 |
}
|
|
385 |
}
|
|
386 |
||
387 |
hard: |
|
388 |
n2.op = 0; |
|
389 |
n1.op = 0; |
|
390 |
if(nr->ullman >= nl->ullman || nl->addable) { |
|
391 |
regalloc(&n2, nr->type, N); |
|
392 |
cgen(nr, &n2); |
|
393 |
nr = &n2; |
|
394 |
} else { |
|
395 |
tempname(&n2, nr->type); |
|
396 |
cgen(nr, &n2); |
|
397 |
nr = &n2; |
|
398 |
}
|
|
399 |
if(!nl->addable) { |
|
400 |
igen(nl, &n1, N); |
|
401 |
nl = &n1; |
|
402 |
}
|
|
403 |
||
404 |
n3 = *n; |
|
405 |
n3.left = nl; |
|
406 |
n3.right = nr; |
|
407 |
n3.op = n->etype; |
|
408 |
||
409 |
regalloc(&n4, nl->type, N); |
|
410 |
cgen(&n3, &n4); |
|
411 |
gmove(&n4, nl); |
|
412 |
||
413 |
if(n1.op) |
|
414 |
regfree(&n1); |
|
415 |
if(n2.op == OREGISTER) |
|
416 |
regfree(&n2); |
|
417 |
regfree(&n4); |
|
418 |
||
419 |
ret: |
|
420 |
;
|
|
421 |
}
|
|
422 |
||
423 |
int
|
|
424 |
samereg(Node *a, Node *b) |
|
425 |
{
|
|
426 |
if(a == N || b == N) |
|
427 |
return 0; |
|
428 |
if(a->op != OREGISTER) |
|
429 |
return 0; |
|
430 |
if(b->op != OREGISTER) |
|
431 |
return 0; |
|
432 |
if(a->val.u.reg != b->val.u.reg) |
|
433 |
return 0; |
|
434 |
return 1; |
|
435 |
}
|
|
436 |
||
437 |
/*
|
|
438 |
* generate division.
|
|
439 |
* caller must set:
|
|
440 |
* ax = allocated AX register
|
|
441 |
* dx = allocated DX register
|
|
442 |
* generates one of:
|
|
443 |
* res = nl / nr
|
|
444 |
* res = nl % nr
|
|
445 |
* according to op.
|
|
446 |
*/
|
|
447 |
void
|
|
448 |
dodiv(int op, Node *nl, Node *nr, Node *res) |
|
449 |
{
|
|
450 |
int a; |
|
451 |
Node n3, n4; |
|
452 |
Type *t; |
|
453 |
Node ax, dx, oldax, olddx; |
|
454 |
||
455 |
t = nl->type; |
|
456 |
if(t->width == 1) { |
|
457 |
if(issigned[t->etype]) |
|
458 |
t = types[TINT32]; |
|
459 |
else
|
|
460 |
t = types[TUINT32]; |
|
461 |
}
|
|
462 |
a = optoas(op, t); |
|
463 |
||
464 |
regalloc(&n3, t, N); |
|
465 |
if(nl->ullman >= nr->ullman) { |
|
466 |
savex(D_AX, &ax, &oldax, res, t); |
|
467 |
cgen(nl, &ax); |
|
468 |
regalloc(&ax, t, &ax); // mark ax live during cgen |
|
469 |
cgen(nr, &n3); |
|
470 |
regfree(&ax); |
|
471 |
} else { |
|
472 |
cgen(nr, &n3); |
|
473 |
savex(D_AX, &ax, &oldax, res, t); |
|
474 |
cgen(nl, &ax); |
|
475 |
}
|
|
476 |
savex(D_DX, &dx, &olddx, res, t); |
|
477 |
if(!issigned[t->etype]) { |
|
478 |
nodconst(&n4, t, 0); |
|
479 |
gmove(&n4, &dx); |
|
480 |
} else |
|
481 |
gins(optoas(OEXTEND, t), N, N); |
|
482 |
gins(a, &n3, N); |
|
483 |
regfree(&n3); |
|
484 |
||
485 |
if(op == ODIV) |
|
486 |
gmove(&ax, res); |
|
487 |
else
|
|
488 |
gmove(&dx, res); |
|
489 |
restx(&ax, &oldax); |
|
490 |
restx(&dx, &olddx); |
|
491 |
}
|
|
492 |
||
493 |
/*
|
|
494 |
* register dr is one of the special ones (AX, CX, DI, SI, etc.).
|
|
495 |
* we need to use it. if it is already allocated as a temporary
|
|
496 |
* (r > 1; can only happen if a routine like sgen passed a
|
|
497 |
* special as cgen's res and then cgen used regalloc to reuse
|
|
498 |
* it as its own temporary), then move it for now to another
|
|
499 |
* register. caller must call restx to move it back.
|
|
500 |
* the move is not necessary if dr == res, because res is
|
|
501 |
* known to be dead.
|
|
502 |
*/
|
|
503 |
void
|
|
504 |
savex(int dr, Node *x, Node *oldx, Node *res, Type *t) |
|
505 |
{
|
|
506 |
int r; |
|
507 |
||
508 |
r = reg[dr]; |
|
509 |
||
510 |
// save current ax and dx if they are live
|
|
511 |
// and not the destination
|
|
512 |
memset(oldx, 0, sizeof *oldx); |
|
513 |
nodreg(x, t, dr); |
|
514 |
if(r > 1 && !samereg(x, res)) { |
|
515 |
regalloc(oldx, types[TINT64], N); |
|
516 |
x->type = types[TINT64]; |
|
517 |
gmove(x, oldx); |
|
518 |
x->type = t; |
|
519 |
oldx->ostk = r; // squirrel away old r value |
|
520 |
reg[dr] = 1; |
|
521 |
}
|
|
522 |
}
|
|
523 |
||
524 |
void
|
|
525 |
restx(Node *x, Node *oldx) |
|
526 |
{
|
|
527 |
if(oldx->op != 0) { |
|
528 |
x->type = types[TINT64]; |
|
529 |
reg[x->val.u.reg] = oldx->ostk; |
|
530 |
gmove(oldx, x); |
|
531 |
regfree(oldx); |
|
532 |
}
|
|
533 |
}
|
|
534 |
||
535 |
/*
|
|
536 |
* generate division according to op, one of:
|
|
537 |
* res = nl / nr
|
|
538 |
* res = nl % nr
|
|
539 |
*/
|
|
540 |
void
|
|
541 |
cgen_div(int op, Node *nl, Node *nr, Node *res) |
|
542 |
{
|
|
543 |
Node n1, n2, n3, savl, savr; |
|
544 |
Node ax, dx, oldax, olddx; |
|
545 |
int n, w, s, a; |
|
546 |
Magic m; |
|
547 |
||
548 |
if(nl->ullman >= UINF) { |
|
549 |
tempname(&savl, nl->type); |
|
550 |
cgen(nl, &savl); |
|
551 |
nl = &savl; |
|
552 |
}
|
|
553 |
if(nr->ullman >= UINF) { |
|
554 |
tempname(&savr, nr->type); |
|
555 |
cgen(nr, &savr); |
|
556 |
nr = &savr; |
|
557 |
}
|
|
558 |
||
559 |
if(nr->op != OLITERAL) |
|
560 |
goto longdiv; |
|
561 |
||
562 |
// special cases of mod/div
|
|
563 |
// by a constant
|
|
564 |
w = nl->type->width*8; |
|
565 |
s = 0; |
|
566 |
n = powtwo(nr); |
|
567 |
if(n >= 1000) { |
|
568 |
// negative power of 2
|
|
569 |
s = 1; |
|
570 |
n -= 1000; |
|
571 |
}
|
|
572 |
||
573 |
if(n+1 >= w) { |
|
574 |
// just sign bit
|
|
575 |
goto longdiv; |
|
576 |
}
|
|
577 |
||
578 |
if(n < 0) |
|
579 |
goto divbymul; |
|
580 |
switch(n) { |
|
581 |
case 0: |
|
582 |
// divide by 1
|
|
583 |
regalloc(&n1, nl->type, res); |
|
584 |
cgen(nl, &n1); |
|
585 |
if(op == OMOD) { |
|
586 |
gins(optoas(OXOR, nl->type), &n1, &n1); |
|
587 |
} else |
|
588 |
if(s) |
|
589 |
gins(optoas(OMINUS, nl->type), N, &n1); |
|
590 |
gmove(&n1, res); |
|
591 |
regfree(&n1); |
|
592 |
return; |
|
593 |
case 1: |
|
594 |
// divide by 2
|
|
595 |
if(op == OMOD) { |
|
596 |
if(issigned[nl->type->etype]) |
|
597 |
goto longmod; |
|
598 |
regalloc(&n1, nl->type, res); |
|
599 |
cgen(nl, &n1); |
|
600 |
nodconst(&n2, nl->type, 1); |
|
601 |
gins(optoas(OAND, nl->type), &n2, &n1); |
|
602 |
gmove(&n1, res); |
|
603 |
regfree(&n1); |
|
604 |
return; |
|
605 |
}
|
|
606 |
regalloc(&n1, nl->type, res); |
|
607 |
cgen(nl, &n1); |
|
608 |
if(!issigned[nl->type->etype]) |
|
609 |
break; |
|
610 |
||
611 |
// develop -1 iff nl is negative
|
|
612 |
regalloc(&n2, nl->type, N); |
|
613 |
gmove(&n1, &n2); |
|
614 |
nodconst(&n3, nl->type, w-1); |
|
615 |
gins(optoas(ORSH, nl->type), &n3, &n2); |
|
616 |
gins(optoas(OSUB, nl->type), &n2, &n1); |
|
617 |
regfree(&n2); |
|
618 |
break; |
|
619 |
default: |
|
620 |
if(op == OMOD) { |
|
621 |
if(issigned[nl->type->etype]) |
|
622 |
goto longmod; |
|
623 |
regalloc(&n1, nl->type, res); |
|
624 |
cgen(nl, &n1); |
|
625 |
nodconst(&n2, nl->type, mpgetfix(nr->val.u.xval)-1); |
|
626 |
if(!smallintconst(&n2)) { |
|
627 |
regalloc(&n3, nl->type, N); |
|
628 |
gmove(&n2, &n3); |
|
629 |
gins(optoas(OAND, nl->type), &n3, &n1); |
|
630 |
regfree(&n3); |
|
631 |
} else |
|
632 |
gins(optoas(OAND, nl->type), &n2, &n1); |
|
633 |
gmove(&n1, res); |
|
634 |
regfree(&n1); |
|
635 |
return; |
|
636 |
}
|
|
637 |
regalloc(&n1, nl->type, res); |
|
638 |
cgen(nl, &n1); |
|
639 |
if(!issigned[nl->type->etype]) |
|
640 |
break; |
|
641 |
||
642 |
// develop (2^k)-1 iff nl is negative
|
|
643 |
regalloc(&n2, nl->type, N); |
|
644 |
gmove(&n1, &n2); |
|
645 |
nodconst(&n3, nl->type, w-1); |
|
646 |
gins(optoas(ORSH, nl->type), &n3, &n2); |
|
647 |
nodconst(&n3, nl->type, w-n); |
|
648 |
gins(optoas(ORSH, tounsigned(nl->type)), &n3, &n2); |
|
649 |
gins(optoas(OADD, nl->type), &n2, &n1); |
|
650 |
regfree(&n2); |
|
651 |
break; |
|
652 |
}
|
|
653 |
nodconst(&n2, nl->type, n); |
|
654 |
gins(optoas(ORSH, nl->type), &n2, &n1); |
|
655 |
if(s) |
|
656 |
gins(optoas(OMINUS, nl->type), N, &n1); |
|
657 |
gmove(&n1, res); |
|
658 |
regfree(&n1); |
|
659 |
return; |
|
660 |
||
661 |
divbymul: |
|
662 |
// try to do division by multiply by (2^w)/d
|
|
663 |
// see hacker's delight chapter 10
|
|
664 |
switch(simtype[nl->type->etype]) { |
|
665 |
default: |
|
666 |
goto longdiv; |
|
667 |
||
668 |
case TUINT8: |
|
669 |
case TUINT16: |
|
670 |
case TUINT32: |
|
671 |
case TUINT64: |
|
672 |
m.w = w; |
|
673 |
m.ud = mpgetfix(nr->val.u.xval); |
|
674 |
umagic(&m); |
|
675 |
if(m.bad) |
|
676 |
break; |
|
677 |
if(op == OMOD) |
|
678 |
goto longmod; |
|
679 |
||
680 |
regalloc(&n1, nl->type, N); |
|
681 |
cgen(nl, &n1); // num -> reg(n1) |
|
682 |
||
683 |
savex(D_AX, &ax, &oldax, res, nl->type); |
|
684 |
savex(D_DX, &dx, &olddx, res, nl->type); |
|
685 |
||
686 |
nodconst(&n2, nl->type, m.um); |
|
687 |
gmove(&n2, &ax); // const->ax |
|
688 |
||
689 |
gins(optoas(OHMUL, nl->type), &n1, N); // imul reg |
|
690 |
if(w == 8) { |
|
691 |
// fix up 8-bit multiply
|
|
692 |
Node ah, dl; |
|
693 |
nodreg(&ah, types[TUINT8], D_AH); |
|
694 |
nodreg(&dl, types[TUINT8], D_DL); |
|
695 |
gins(AMOVB, &ah, &dl); |
|
696 |
}
|
|
697 |
||
698 |
if(m.ua) { |
|
699 |
// need to add numerator accounting for overflow
|
|
700 |
gins(optoas(OADD, nl->type), &n1, &dx); |
|
701 |
nodconst(&n2, nl->type, 1); |
|
702 |
gins(optoas(ORRC, nl->type), &n2, &dx); |
|
703 |
nodconst(&n2, nl->type, m.s-1); |
|
704 |
gins(optoas(ORSH, nl->type), &n2, &dx); |
|
705 |
} else { |
|
706 |
nodconst(&n2, nl->type, m.s); |
|
707 |
gins(optoas(ORSH, nl->type), &n2, &dx); // shift dx |
|
708 |
}
|
|
709 |
||
710 |
||
711 |
regfree(&n1); |
|
712 |
gmove(&dx, res); |
|
713 |
||
714 |
restx(&ax, &oldax); |
|
715 |
restx(&dx, &olddx); |
|
716 |
return; |
|
717 |
||
718 |
case TINT8: |
|
719 |
case TINT16: |
|
720 |
case TINT32: |
|
721 |
case TINT64: |
|
722 |
m.w = w; |
|
723 |
m.sd = mpgetfix(nr->val.u.xval); |
|
724 |
smagic(&m); |
|
725 |
if(m.bad) |
|
726 |
break; |
|
727 |
if(op == OMOD) |
|
728 |
goto longmod; |
|
729 |
||
730 |
regalloc(&n1, nl->type, N); |
|
731 |
cgen(nl, &n1); // num -> reg(n1) |
|
732 |
||
733 |
savex(D_AX, &ax, &oldax, res, nl->type); |
|
734 |
savex(D_DX, &dx, &olddx, res, nl->type); |
|
735 |
||
736 |
nodconst(&n2, nl->type, m.sm); |
|
737 |
gmove(&n2, &ax); // const->ax |
|
738 |
||
739 |
gins(optoas(OHMUL, nl->type), &n1, N); // imul reg |
|
740 |
if(w == 8) { |
|
741 |
// fix up 8-bit multiply
|
|
742 |
Node ah, dl; |
|
743 |
nodreg(&ah, types[TUINT8], D_AH); |
|
744 |
nodreg(&dl, types[TUINT8], D_DL); |
|
745 |
gins(AMOVB, &ah, &dl); |
|
746 |
}
|
|
747 |
||
748 |
if(m.sm < 0) { |
|
749 |
// need to add numerator
|
|
750 |
gins(optoas(OADD, nl->type), &n1, &dx); |
|
751 |
}
|
|
752 |
||
753 |
nodconst(&n2, nl->type, m.s); |
|
754 |
gins(optoas(ORSH, nl->type), &n2, &dx); // shift dx |
|
755 |
||
756 |
nodconst(&n2, nl->type, w-1); |
|
757 |
gins(optoas(ORSH, nl->type), &n2, &n1); // -1 iff num is neg |
|
758 |
gins(optoas(OSUB, nl->type), &n1, &dx); // added |
|
759 |
||
760 |
if(m.sd < 0) { |
|
761 |
// this could probably be removed
|
|
762 |
// by factoring it into the multiplier
|
|
763 |
gins(optoas(OMINUS, nl->type), N, &dx); |
|
764 |
}
|
|
765 |
||
766 |
regfree(&n1); |
|
767 |
gmove(&dx, res); |
|
768 |
||
769 |
restx(&ax, &oldax); |
|
770 |
restx(&dx, &olddx); |
|
771 |
return; |
|
772 |
}
|
|
773 |
goto longdiv; |
|
774 |
||
775 |
longdiv: |
|
776 |
// division and mod using (slow) hardware instruction
|
|
777 |
dodiv(op, nl, nr, res); |
|
778 |
return; |
|
779 |
||
780 |
longmod: |
|
781 |
// mod using formula A%B = A-(A/B*B) but
|
|
782 |
// we know that there is a fast algorithm for A/B
|
|
783 |
regalloc(&n1, nl->type, res); |
|
784 |
cgen(nl, &n1); |
|
785 |
regalloc(&n2, nl->type, N); |
|
786 |
cgen_div(ODIV, &n1, nr, &n2); |
|
787 |
a = optoas(OMUL, nl->type); |
|
788 |
if(w == 8) { |
|
789 |
// use 2-operand 16-bit multiply
|
|
790 |
// because there is no 2-operand 8-bit multiply
|
|
791 |
a = AIMULW; |
|
792 |
}
|
|
793 |
if(!smallintconst(nr)) { |
|
794 |
regalloc(&n3, nl->type, N); |
|
795 |
cgen(nr, &n3); |
|
796 |
gins(a, &n3, &n2); |
|
797 |
regfree(&n3); |
|
798 |
} else |
|
799 |
gins(a, nr, &n2); |
|
800 |
gins(optoas(OSUB, nl->type), &n2, &n1); |
|
801 |
gmove(&n1, res); |
|
802 |
regfree(&n1); |
|
803 |
regfree(&n2); |
|
804 |
}
|
|
805 |
||
806 |
/*
|
|
807 |
* generate shift according to op, one of:
|
|
808 |
* res = nl << nr
|
|
809 |
* res = nl >> nr
|
|
810 |
*/
|
|
811 |
void
|
|
812 |
cgen_shift(int op, Node *nl, Node *nr, Node *res) |
|
813 |
{
|
|
814 |
Node n1, n2, n3, n4, n5, cx, oldcx; |
|
815 |
int a, rcx; |
|
816 |
Prog *p1; |
|
817 |
uvlong sc; |
|
818 |
Type *tcount; |
|
819 |
||
820 |
a = optoas(op, nl->type); |
|
821 |
||
822 |
if(nr->op == OLITERAL) { |
|
823 |
regalloc(&n1, nl->type, res); |
|
824 |
cgen(nl, &n1); |
|
825 |
sc = mpgetfix(nr->val.u.xval); |
|
826 |
if(sc >= nl->type->width*8) { |
|
827 |
// large shift gets 2 shifts by width
|
|
828 |
nodconst(&n3, types[TUINT32], nl->type->width*8-1); |
|
829 |
gins(a, &n3, &n1); |
|
830 |
gins(a, &n3, &n1); |
|
831 |
} else |
|
832 |
gins(a, nr, &n1); |
|
833 |
gmove(&n1, res); |
|
834 |
regfree(&n1); |
|
835 |
goto ret; |
|
836 |
}
|
|
837 |
||
838 |
if(nl->ullman >= UINF) { |
|
839 |
tempname(&n4, nl->type); |
|
840 |
cgen(nl, &n4); |
|
841 |
nl = &n4; |
|
842 |
}
|
|
843 |
if(nr->ullman >= UINF) { |
|
844 |
tempname(&n5, nr->type); |
|
845 |
cgen(nr, &n5); |
|
846 |
nr = &n5; |
|
847 |
}
|
|
848 |
||
849 |
rcx = reg[D_CX]; |
|
850 |
nodreg(&n1, types[TUINT32], D_CX); |
|
851 |
||
852 |
// Allow either uint32 or uint64 as shift type,
|
|
853 |
// to avoid unnecessary conversion from uint32 to uint64
|
|
854 |
// just to do the comparison.
|
|
855 |
tcount = types[simtype[nr->type->etype]]; |
|
856 |
if(tcount->etype < TUINT32) |
|
857 |
tcount = types[TUINT32]; |
|
858 |
||
859 |
regalloc(&n1, nr->type, &n1); // to hold the shift type in CX |
|
860 |
regalloc(&n3, tcount, &n1); // to clear high bits of CX |
|
861 |
||
862 |
nodreg(&cx, types[TUINT64], D_CX); |
|
863 |
memset(&oldcx, 0, sizeof oldcx); |
|
864 |
if(rcx > 0 && !samereg(&cx, res)) { |
|
865 |
regalloc(&oldcx, types[TUINT64], N); |
|
866 |
gmove(&cx, &oldcx); |
|
867 |
}
|
|
868 |
cx.type = tcount; |
|
869 |
||
870 |
if(samereg(&cx, res)) |
|
871 |
regalloc(&n2, nl->type, N); |
|
872 |
else
|
|
873 |
regalloc(&n2, nl->type, res); |
|
874 |
if(nl->ullman >= nr->ullman) { |
|
875 |
cgen(nl, &n2); |
|
876 |
cgen(nr, &n1); |
|
877 |
gmove(&n1, &n3); |
|
878 |
} else { |
|
879 |
cgen(nr, &n1); |
|
880 |
gmove(&n1, &n3); |
|
881 |
cgen(nl, &n2); |
|
882 |
}
|
|
883 |
regfree(&n3); |
|
884 |
||
885 |
// test and fix up large shifts
|
|
886 |
nodconst(&n3, tcount, nl->type->width*8); |
|
887 |
gins(optoas(OCMP, tcount), &n1, &n3); |
|
888 |
p1 = gbranch(optoas(OLT, tcount), T); |
|
889 |
if(op == ORSH && issigned[nl->type->etype]) { |
|
890 |
nodconst(&n3, types[TUINT32], nl->type->width*8-1); |
|
891 |
gins(a, &n3, &n2); |
|
892 |
} else { |
|
893 |
nodconst(&n3, nl->type, 0); |
|
894 |
gmove(&n3, &n2); |
|
895 |
}
|
|
896 |
patch(p1, pc); |
|
897 |
gins(a, &n1, &n2); |
|
898 |
||
899 |
if(oldcx.op != 0) { |
|
900 |
cx.type = types[TUINT64]; |
|
901 |
gmove(&oldcx, &cx); |
|
902 |
regfree(&oldcx); |
|
903 |
}
|
|
904 |
||
905 |
gmove(&n2, res); |
|
906 |
||
907 |
regfree(&n1); |
|
908 |
regfree(&n2); |
|
909 |
||
910 |
ret: |
|
911 |
;
|
|
912 |
}
|
|
913 |
||
914 |
/*
|
|
915 |
* generate byte multiply:
|
|
916 |
* res = nl * nr
|
|
917 |
* no 2-operand byte multiply instruction so have to do
|
|
918 |
* 16-bit multiply and take bottom half.
|
|
919 |
*/
|
|
920 |
void
|
|
921 |
cgen_bmul(int op, Node *nl, Node *nr, Node *res) |
|
922 |
{
|
|
923 |
Node n1b, n2b, n1w, n2w; |
|
924 |
Type *t; |
|
925 |
int a; |
|
926 |
||
927 |
if(nl->ullman >= nr->ullman) { |
|
928 |
regalloc(&n1b, nl->type, res); |
|
929 |
cgen(nl, &n1b); |
|
930 |
regalloc(&n2b, nr->type, N); |
|
931 |
cgen(nr, &n2b); |
|
932 |
} else { |
|
933 |
regalloc(&n2b, nr->type, N); |
|
934 |
cgen(nr, &n2b); |
|
935 |
regalloc(&n1b, nl->type, res); |
|
936 |
cgen(nl, &n1b); |
|
937 |
}
|
|
938 |
||
939 |
// copy from byte to short registers
|
|
940 |
t = types[TUINT16]; |
|
941 |
if(issigned[nl->type->etype]) |
|
942 |
t = types[TINT16]; |
|
943 |
||
944 |
regalloc(&n2w, t, &n2b); |
|
945 |
cgen(&n2b, &n2w); |
|
946 |
||
947 |
regalloc(&n1w, t, &n1b); |
|
948 |
cgen(&n1b, &n1w); |
|
949 |
||
950 |
a = optoas(op, t); |
|
951 |
gins(a, &n2w, &n1w); |
|
952 |
cgen(&n1w, &n1b); |
|
953 |
cgen(&n1b, res); |
|
954 |
||
955 |
regfree(&n1w); |
|
956 |
regfree(&n2w); |
|
957 |
regfree(&n1b); |
|
958 |
regfree(&n2b); |
|
959 |
}
|
|
960 |
||
961 |
void
|
|
962 |
clearfat(Node *nl) |
|
963 |
{
|
|
964 |
uint32 w, c, q; |
|
965 |
Node n1, oldn1, ax, oldax; |
|
966 |
||
967 |
/* clear a fat object */
|
|
968 |
if(debug['g']) |
|
969 |
dump("\nclearfat", nl); |
|
970 |
||
971 |
||
972 |
w = nl->type->width; |
|
973 |
if(w == 16) |
|
974 |
if(componentgen(N, nl)) |
|
975 |
return; |
|
976 |
||
977 |
c = w % 8; // bytes |
|
978 |
q = w / 8; // quads |
|
979 |
||
980 |
savex(D_DI, &n1, &oldn1, N, types[tptr]); |
|
981 |
agen(nl, &n1); |
|
982 |
||
983 |
savex(D_AX, &ax, &oldax, N, types[tptr]); |
|
984 |
gconreg(AMOVQ, 0, D_AX); |
|
985 |
||
986 |
if(q >= 4) { |
|
987 |
gconreg(AMOVQ, q, D_CX); |
|
988 |
gins(AREP, N, N); // repeat |
|
989 |
gins(ASTOSQ, N, N); // STOQ AL,*(DI)+ |
|
990 |
} else |
|
991 |
while(q > 0) { |
|
992 |
gins(ASTOSQ, N, N); // STOQ AL,*(DI)+ |
|
993 |
q--; |
|
994 |
}
|
|
995 |
||
996 |
if(c >= 4) { |
|
997 |
gconreg(AMOVQ, c, D_CX); |
|
998 |
gins(AREP, N, N); // repeat |
|
999 |
gins(ASTOSB, N, N); // STOB AL,*(DI)+ |
|
1000 |
} else |
|
1001 |
while(c > 0) { |
|
1002 |
gins(ASTOSB, N, N); // STOB AL,*(DI)+ |
|
1003 |
c--; |
|
1004 |
}
|
|
1005 |
||
1006 |
restx(&n1, &oldn1); |
|
1007 |
restx(&ax, &oldax); |
|
1008 |
}
|
|
1009 |
||
1010 |
static int |
|
1011 |
regcmp(const void *va, const void *vb) |
|
1012 |
{
|
|
1013 |
Node *ra, *rb; |
|
1014 |
||
1015 |
ra = (Node*)va; |
|
1016 |
rb = (Node*)vb; |
|
1017 |
return ra->local - rb->local; |
|
1018 |
}
|
|
1019 |
||
1020 |
static Prog* throwpc; |
|
1021 |
||
1022 |
void
|
|
1023 |
getargs(NodeList *nn, Node *reg, int n) |
|
1024 |
{
|
|
1025 |
NodeList *l; |
|
1026 |
int i; |
|
1027 |
||
1028 |
throwpc = nil; |
|
1029 |
||
1030 |
l = nn; |
|
1031 |
for(i=0; i<n; i++) { |
|
1032 |
if(!smallintconst(l->n->right) && !isslice(l->n->right->type)) { |
|
1033 |
regalloc(reg+i, l->n->right->type, N); |
|
1034 |
cgen(l->n->right, reg+i); |
|
1035 |
} else |
|
1036 |
reg[i] = *l->n->right; |
|
1037 |
if(reg[i].local != 0) |
|
1038 |
yyerror("local used"); |
|
1039 |
reg[i].local = l->n->left->xoffset; |
|
1040 |
l = l->next; |
|
1041 |
}
|
|
1042 |
qsort((void*)reg, n, sizeof(*reg), regcmp); |
|
1043 |
for(i=0; i<n; i++) |
|
1044 |
reg[i].local = 0; |
|
1045 |
}
|
|
1046 |
||
1047 |
void
|
|
1048 |
cmpandthrow(Node *nl, Node *nr) |
|
1049 |
{
|
|
1050 |
vlong cl; |
|
1051 |
Prog *p1; |
|
1052 |
int op; |
|
1053 |
Node *c; |
|
1054 |
Type *t; |
|
1055 |
Node n1; |
|
1056 |
||
1057 |
if(nl->op == OCONV && is64(nl->type)) |
|
1058 |
nl = nl->left; |
|
1059 |
if(nr->op == OCONV && is64(nr->type)) |
|
1060 |
nr = nr->left; |
|
1061 |
||
1062 |
op = OLE; |
|
1063 |
if(smallintconst(nl)) { |
|
1064 |
cl = mpgetfix(nl->val.u.xval); |
|
1065 |
if(cl == 0) |
|
1066 |
return; |
|
1067 |
if(smallintconst(nr)) |
|
1068 |
return; |
|
1069 |
// put the constant on the right
|
|
1070 |
op = brrev(op); |
|
1071 |
c = nl; |
|
1072 |
nl = nr; |
|
1073 |
nr = c; |
|
1074 |
}
|
|
1075 |
if(is64(nr->type) && smallintconst(nr)) |
|
1076 |
nr->type = types[TUINT32]; |
|
1077 |
||
1078 |
n1.op = OXXX; |
|
1079 |
t = types[TUINT32]; |
|
1080 |
if(nl->type->width != t->width || nr->type->width != t->width) { |
|
1081 |
if((is64(nl->type) && nl->op != OLITERAL) || (is64(nr->type) && nr->op != OLITERAL)) |
|
1082 |
t = types[TUINT64]; |
|
1083 |
||
1084 |
// Check if we need to use a temporary.
|
|
1085 |
// At least one of the arguments is 32 bits
|
|
1086 |
// (the len or cap) so one temporary suffices.
|
|
1087 |
if(nl->type->width != t->width && nl->op != OLITERAL) { |
|
1088 |
regalloc(&n1, t, nl); |
|
1089 |
gmove(nl, &n1); |
|
1090 |
nl = &n1; |
|
1091 |
} else if(nr->type->width != t->width && nr->op != OLITERAL) { |
|
1092 |
regalloc(&n1, t, nr); |
|
1093 |
gmove(nr, &n1); |
|
1094 |
nr = &n1; |
|
1095 |
}
|
|
1096 |
}
|
|
1097 |
gins(optoas(OCMP, t), nl, nr); |
|
1098 |
if(n1.op != OXXX) |
|
1099 |
regfree(&n1); |
|
1100 |
if(throwpc == nil) { |
|
1101 |
p1 = gbranch(optoas(op, t), T); |
|
1102 |
throwpc = pc; |
|
1103 |
ginscall(panicslice, 0); |
|
1104 |
patch(p1, pc); |
|
1105 |
} else { |
|
1106 |
op = brcom(op); |
|
1107 |
p1 = gbranch(optoas(op, t), T); |
|
1108 |
patch(p1, throwpc); |
|
1109 |
}
|
|
1110 |
}
|
|
1111 |
||
1112 |
int
|
|
1113 |
sleasy(Node *n) |
|
1114 |
{
|
|
1115 |
if(n->op != ONAME) |
|
1116 |
return 0; |
|
1117 |
if(!n->addable) |
|
1118 |
return 0; |
|
1119 |
return 1; |
|
1120 |
}
|
|
1121 |
||
1122 |
// generate inline code for
|
|
1123 |
// slicearray
|
|
1124 |
// sliceslice
|
|
1125 |
// arraytoslice
|
|
1126 |
int
|
|
1127 |
cgen_inline(Node *n, Node *res) |
|
1128 |
{
|
|
1129 |
Node nodes[5]; |
|
1130 |
Node n1, n2, nres, ntemp; |
|
1131 |
vlong v; |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1132 |
int i, narg, nochk; |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1133 |
|
1134 |
if(n->op != OCALLFUNC) |
|
1135 |
goto no; |
|
1136 |
if(!n->left->addable) |
|
1137 |
goto no; |
|
1138 |
if(n->left->sym == S) |
|
1139 |
goto no; |
|
1140 |
if(n->left->sym->pkg != runtimepkg) |
|
1141 |
goto no; |
|
1142 |
if(strcmp(n->left->sym->name, "slicearray") == 0) |
|
1143 |
goto slicearray; |
|
1144 |
if(strcmp(n->left->sym->name, "sliceslice") == 0) { |
|
1145 |
narg = 4; |
|
1146 |
goto sliceslice; |
|
1147 |
}
|
|
1148 |
if(strcmp(n->left->sym->name, "sliceslice1") == 0) { |
|
1149 |
narg = 3; |
|
1150 |
goto sliceslice; |
|
1151 |
}
|
|
1152 |
goto no; |
|
1153 |
||
1154 |
slicearray: |
|
1155 |
if(!sleasy(res)) |
|
1156 |
goto no; |
|
1157 |
getargs(n->list, nodes, 5); |
|
1158 |
||
1159 |
// if(hb[3] > nel[1]) goto throw
|
|
1160 |
cmpandthrow(&nodes[3], &nodes[1]); |
|
1161 |
||
1162 |
// if(lb[2] > hb[3]) goto throw
|
|
1163 |
cmpandthrow(&nodes[2], &nodes[3]); |
|
1164 |
||
1165 |
// len = hb[3] - lb[2] (destroys hb)
|
|
1166 |
n2 = *res; |
|
1167 |
n2.xoffset += Array_nel; |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1168 |
n2.type = types[TUINT32]; |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1169 |
|
1170 |
if(smallintconst(&nodes[3]) && smallintconst(&nodes[2])) { |
|
1171 |
v = mpgetfix(nodes[3].val.u.xval) - |
|
1172 |
mpgetfix(nodes[2].val.u.xval); |
|
1173 |
nodconst(&n1, types[TUINT32], v); |
|
1174 |
gins(optoas(OAS, types[TUINT32]), &n1, &n2); |
|
1175 |
} else { |
|
1176 |
regalloc(&n1, types[TUINT32], &nodes[3]); |
|
1177 |
gmove(&nodes[3], &n1); |
|
1178 |
if(!smallintconst(&nodes[2]) || mpgetfix(nodes[2].val.u.xval) != 0) |
|
1179 |
gins(optoas(OSUB, types[TUINT32]), &nodes[2], &n1); |
|
1180 |
gins(optoas(OAS, types[TUINT32]), &n1, &n2); |
|
1181 |
regfree(&n1); |
|
1182 |
}
|
|
1183 |
||
1184 |
// cap = nel[1] - lb[2] (destroys nel)
|
|
1185 |
n2 = *res; |
|
1186 |
n2.xoffset += Array_cap; |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1187 |
n2.type = types[TUINT32]; |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1188 |
|
1189 |
if(smallintconst(&nodes[1]) && smallintconst(&nodes[2])) { |
|
1190 |
v = mpgetfix(nodes[1].val.u.xval) - |
|
1191 |
mpgetfix(nodes[2].val.u.xval); |
|
1192 |
nodconst(&n1, types[TUINT32], v); |
|
1193 |
gins(optoas(OAS, types[TUINT32]), &n1, &n2); |
|
1194 |
} else { |
|
1195 |
regalloc(&n1, types[TUINT32], &nodes[1]); |
|
1196 |
gmove(&nodes[1], &n1); |
|
1197 |
if(!smallintconst(&nodes[2]) || mpgetfix(nodes[2].val.u.xval) != 0) |
|
1198 |
gins(optoas(OSUB, types[TUINT32]), &nodes[2], &n1); |
|
1199 |
gins(optoas(OAS, types[TUINT32]), &n1, &n2); |
|
1200 |
regfree(&n1); |
|
1201 |
}
|
|
1202 |
||
1203 |
// if slice could be too big, dereference to
|
|
1204 |
// catch nil array pointer.
|
|
1205 |
if(nodes[0].op == OREGISTER && nodes[0].type->type->width >= unmappedzero) { |
|
1206 |
n2 = nodes[0]; |
|
1207 |
n2.xoffset = 0; |
|
1208 |
n2.op = OINDREG; |
|
1209 |
n2.type = types[TUINT8]; |
|
1210 |
gins(ATESTB, nodintconst(0), &n2); |
|
1211 |
}
|
|
1212 |
||
1213 |
// ary = old[0] + (lb[2] * width[4]) (destroys old)
|
|
1214 |
n2 = *res; |
|
1215 |
n2.xoffset += Array_array; |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1216 |
n2.type = types[tptr]; |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1217 |
|
1218 |
if(smallintconst(&nodes[2]) && smallintconst(&nodes[4])) { |
|
1219 |
v = mpgetfix(nodes[2].val.u.xval) * |
|
1220 |
mpgetfix(nodes[4].val.u.xval); |
|
1221 |
if(v != 0) |
|
1222 |
ginscon(optoas(OADD, types[tptr]), v, &nodes[0]); |
|
1223 |
} else { |
|
1224 |
regalloc(&n1, types[tptr], &nodes[2]); |
|
1225 |
gmove(&nodes[2], &n1); |
|
1226 |
if(!smallintconst(&nodes[4]) || mpgetfix(nodes[4].val.u.xval) != 1) |
|
1227 |
gins(optoas(OMUL, types[tptr]), &nodes[4], &n1); |
|
1228 |
gins(optoas(OADD, types[tptr]), &n1, &nodes[0]); |
|
1229 |
regfree(&n1); |
|
1230 |
}
|
|
1231 |
gins(optoas(OAS, types[tptr]), &nodes[0], &n2); |
|
1232 |
||
1233 |
for(i=0; i<5; i++) { |
|
1234 |
if(nodes[i].op == OREGISTER) |
|
1235 |
regfree(&nodes[i]); |
|
1236 |
}
|
|
1237 |
return 1; |
|
1238 |
||
1239 |
sliceslice: |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1240 |
nochk = n->etype; // skip bounds checking |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1241 |
ntemp.op = OXXX; |
1242 |
if(!sleasy(n->list->n->right)) { |
|
1243 |
Node *n0; |
|
1244 |
||
1245 |
n0 = n->list->n->right; |
|
1246 |
tempname(&ntemp, res->type); |
|
1247 |
cgen(n0, &ntemp); |
|
1248 |
n->list->n->right = &ntemp; |
|
1249 |
getargs(n->list, nodes, narg); |
|
1250 |
n->list->n->right = n0; |
|
1251 |
} else |
|
1252 |
getargs(n->list, nodes, narg); |
|
1253 |
||
1254 |
nres = *res; // result |
|
1255 |
if(!sleasy(res)) { |
|
1256 |
if(ntemp.op == OXXX) |
|
1257 |
tempname(&ntemp, res->type); |
|
1258 |
nres = ntemp; |
|
1259 |
}
|
|
1260 |
||
1261 |
if(narg == 3) { // old[lb:] |
|
1262 |
// move width to where it would be for old[lb:hb]
|
|
1263 |
nodes[3] = nodes[2]; |
|
1264 |
nodes[2].op = OXXX; |
|
1265 |
||
1266 |
// if(lb[1] > old.nel[0]) goto throw;
|
|
1267 |
n2 = nodes[0]; |
|
1268 |
n2.xoffset += Array_nel; |
|
1269 |
n2.type = types[TUINT32]; |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1270 |
if(!nochk) |
1271 |
cmpandthrow(&nodes[1], &n2); |
|
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1272 |
|
1273 |
// ret.nel = old.nel[0]-lb[1];
|
|
1274 |
n2 = nodes[0]; |
|
1275 |
n2.xoffset += Array_nel; |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1276 |
n2.type = types[TUINT32]; |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1277 |
|
1278 |
regalloc(&n1, types[TUINT32], N); |
|
1279 |
gins(optoas(OAS, types[TUINT32]), &n2, &n1); |
|
1280 |
if(!smallintconst(&nodes[1]) || mpgetfix(nodes[1].val.u.xval) != 0) |
|
1281 |
gins(optoas(OSUB, types[TUINT32]), &nodes[1], &n1); |
|
1282 |
||
1283 |
n2 = nres; |
|
1284 |
n2.xoffset += Array_nel; |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1285 |
n2.type = types[TUINT32]; |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1286 |
gins(optoas(OAS, types[TUINT32]), &n1, &n2); |
1287 |
regfree(&n1); |
|
1288 |
} else { // old[lb:hb] |
|
1289 |
n2 = nodes[0]; |
|
1290 |
n2.xoffset += Array_cap; |
|
1291 |
n2.type = types[TUINT32]; |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1292 |
if(!nochk) { |
1293 |
// if(hb[2] > old.cap[0]) goto throw;
|
|
1294 |
cmpandthrow(&nodes[2], &n2); |
|
1295 |
// if(lb[1] > hb[2]) goto throw;
|
|
1296 |
cmpandthrow(&nodes[1], &nodes[2]); |
|
1297 |
}
|
|
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1298 |
// ret.len = hb[2]-lb[1]; (destroys hb[2])
|
1299 |
n2 = nres; |
|
1300 |
n2.xoffset += Array_nel; |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1301 |
n2.type = types[TUINT32]; |
1302 |
||
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1303 |
if(smallintconst(&nodes[2]) && smallintconst(&nodes[1])) { |
1304 |
v = mpgetfix(nodes[2].val.u.xval) - |
|
1305 |
mpgetfix(nodes[1].val.u.xval); |
|
1306 |
nodconst(&n1, types[TUINT32], v); |
|
1307 |
gins(optoas(OAS, types[TUINT32]), &n1, &n2); |
|
1308 |
} else { |
|
1309 |
regalloc(&n1, types[TUINT32], &nodes[2]); |
|
1310 |
gmove(&nodes[2], &n1); |
|
1311 |
if(!smallintconst(&nodes[1]) || mpgetfix(nodes[1].val.u.xval) != 0) |
|
1312 |
gins(optoas(OSUB, types[TUINT32]), &nodes[1], &n1); |
|
1313 |
gins(optoas(OAS, types[TUINT32]), &n1, &n2); |
|
1314 |
regfree(&n1); |
|
1315 |
}
|
|
1316 |
}
|
|
1317 |
||
1318 |
// ret.cap = old.cap[0]-lb[1]; (uses hb[2])
|
|
1319 |
n2 = nodes[0]; |
|
1320 |
n2.xoffset += Array_cap; |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1321 |
n2.type = types[TUINT32]; |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1322 |
|
1323 |
regalloc(&n1, types[TUINT32], &nodes[2]); |
|
1324 |
gins(optoas(OAS, types[TUINT32]), &n2, &n1); |
|
1325 |
if(!smallintconst(&nodes[1]) || mpgetfix(nodes[1].val.u.xval) != 0) |
|
1326 |
gins(optoas(OSUB, types[TUINT32]), &nodes[1], &n1); |
|
1327 |
||
1328 |
n2 = nres; |
|
1329 |
n2.xoffset += Array_cap; |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1330 |
n2.type = types[TUINT32]; |
1331 |
||
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1332 |
gins(optoas(OAS, types[TUINT32]), &n1, &n2); |
1333 |
regfree(&n1); |
|
1334 |
||
1335 |
// ret.array = old.array[0]+lb[1]*width[3]; (uses lb[1])
|
|
1336 |
n2 = nodes[0]; |
|
1337 |
n2.xoffset += Array_array; |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1338 |
n2.type = types[tptr]; |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1339 |
regalloc(&n1, types[tptr], &nodes[1]); |
1340 |
if(smallintconst(&nodes[1]) && smallintconst(&nodes[3])) { |
|
1341 |
gins(optoas(OAS, types[tptr]), &n2, &n1); |
|
1342 |
v = mpgetfix(nodes[1].val.u.xval) * |
|
1343 |
mpgetfix(nodes[3].val.u.xval); |
|
1344 |
if(v != 0) |
|
1345 |
ginscon(optoas(OADD, types[tptr]), v, &n1); |
|
1346 |
} else { |
|
1347 |
gmove(&nodes[1], &n1); |
|
1348 |
if(!smallintconst(&nodes[3]) || mpgetfix(nodes[3].val.u.xval) != 1) |
|
1349 |
gins(optoas(OMUL, types[tptr]), &nodes[3], &n1); |
|
1350 |
gins(optoas(OADD, types[tptr]), &n2, &n1); |
|
1351 |
}
|
|
1352 |
||
1353 |
n2 = nres; |
|
1354 |
n2.xoffset += Array_array; |
|
1.1.6
by Ondřej Surý
Import upstream version 58 |
1355 |
n2.type = types[tptr]; |
1
by Ondřej Surý
Import upstream version 2011.03.07.1 |
1356 |
gins(optoas(OAS, types[tptr]), &n1, &n2); |
1357 |
regfree(&n1); |
|
1358 |
||
1359 |
for(i=0; i<4; i++) { |
|
1360 |
if(nodes[i].op == OREGISTER) |
|
1361 |
regfree(&nodes[i]); |
|
1362 |
}
|
|
1363 |
||
1364 |
if(!sleasy(res)) { |
|
1365 |
cgen(&nres, res); |
|
1366 |
}
|
|
1367 |
return 1; |
|
1368 |
||
1369 |
no: |
|
1370 |
return 0; |
|
1371 |
}
|