1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
#include "rc.h"
#include "io.h"
#include "exec.h"
#include "fns.h"
#include "getflags.h"
#define c0 t->child[0]
#define c1 t->child[1]
#define c2 t->child[2]
int codep, ncode;
#define emitf(x) ((void)(codep!=ncode || morecode()), codebuf[codep].f=(x), codep++)
#define emiti(x) ((void)(codep!=ncode || morecode()), codebuf[codep].i=(x), codep++)
#define emits(x) ((void)(codep!=ncode || morecode()), codebuf[codep].s=(x), codep++)
void stuffdot(int);
char *fnstr(tree*);
void outcode(tree*, int);
void codeswitch(tree*, int);
int iscase(tree*);
code *codecopy(code*);
void codefree(code*);
int morecode(void){
ncode+=100;
codebuf=(code *)realloc((char *)codebuf, ncode*sizeof codebuf[0]);
if(codebuf==0) panic("Can't realloc %d bytes in morecode!",
ncode*sizeof codebuf[0]);
return 0;
}
void stuffdot(int a){
if(a<0 || codep<=a) panic("Bad address %d in stuffdot", a);
codebuf[a].i=codep;
}
int compile(tree *t)
{
ncode=100;
codebuf=(code *)emalloc(ncode*sizeof codebuf[0]);
codep=0;
emiti(0); /* reference count */
outcode(t, flag['e']?1:0);
if(nerror){
efree((char *)codebuf);
return 0;
}
readhere();
emitf(Xreturn);
emitf(0);
return 1;
}
void cleanhere(char *f)
{
emitf(Xdelhere);
emits(strdup(f));
}
char *fnstr(tree *t)
{
io *f=openstr();
char *v;
extern char nl;
char svnl=nl;
nl=';';
pfmt(f, "%t", t);
nl=svnl;
v=f->strp;
f->strp=0;
closeio(f);
return v;
}
void outcode(tree *t, int eflag)
{
int p, q;
tree *tt;
if(t==0) return;
if(t->type!=NOT && t->type!=';') runq->iflast=0;
switch(t->type){
default:
pfmt(err, "bad type %d in outcode\n", t->type);
break;
case '$':
emitf(Xmark);
outcode(c0, eflag);
emitf(Xdol);
break;
case '"':
emitf(Xmark);
outcode(c0, eflag);
emitf(Xqdol);
break;
case SUB:
emitf(Xmark);
outcode(c0, eflag);
emitf(Xmark);
outcode(c1, eflag);
emitf(Xsub);
break;
case '&':
emitf(Xasync);
p=emiti(0);
outcode(c0, eflag);
emitf(Xexit);
stuffdot(p);
break;
case ';':
outcode(c0, eflag);
outcode(c1, eflag);
break;
case '^':
emitf(Xmark);
outcode(c1, eflag);
emitf(Xmark);
outcode(c0, eflag);
emitf(Xconc);
break;
case '`':
emitf(Xbackq);
p=emiti(0);
outcode(c0, 0);
emitf(Xexit);
stuffdot(p);
break;
case ANDAND:
outcode(c0, 0);
emitf(Xtrue);
p=emiti(0);
outcode(c1, eflag);
stuffdot(p);
break;
case ARGLIST:
outcode(c1, eflag);
outcode(c0, eflag);
break;
case BANG:
outcode(c0, eflag);
emitf(Xbang);
break;
case PCMD:
case BRACE:
outcode(c0, eflag);
break;
case COUNT:
emitf(Xmark);
outcode(c0, eflag);
emitf(Xcount);
break;
case FN:
emitf(Xmark);
outcode(c0, eflag);
if(c1){
emitf(Xfn);
p=emiti(0);
emits(fnstr(c1));
outcode(c1, eflag);
emitf(Xunlocal); /* get rid of $* */
emitf(Xreturn);
stuffdot(p);
}
else
emitf(Xdelfn);
break;
case IF:
outcode(c0, 0);
emitf(Xif);
p=emiti(0);
outcode(c1, eflag);
emitf(Xwastrue);
stuffdot(p);
break;
case NOT:
if(!runq->iflast) yyerror("`if not' does not follow `if(...)'");
emitf(Xifnot);
p=emiti(0);
outcode(c0, eflag);
stuffdot(p);
break;
case OROR:
outcode(c0, 0);
emitf(Xfalse);
p=emiti(0);
outcode(c1, eflag);
stuffdot(p);
break;
case PAREN:
outcode(c0, eflag);
break;
case SIMPLE:
emitf(Xmark);
outcode(c0, eflag);
emitf(Xsimple);
if(eflag) emitf(Xeflag);
break;
case SUBSHELL:
emitf(Xsubshell);
p=emiti(0);
outcode(c0, eflag);
emitf(Xexit);
stuffdot(p);
if(eflag) emitf(Xeflag);
break;
case SWITCH:
codeswitch(t, eflag);
break;
case TWIDDLE:
emitf(Xmark);
outcode(c1, eflag);
emitf(Xmark);
outcode(c0, eflag);
emitf(Xmatch);
if(eflag) emitf(Xeflag);
break;
case WHILE:
q=codep;
outcode(c0, 0);
if(q==codep) emitf(Xsettrue); /* empty condition == while(true) */
emitf(Xtrue);
p=emiti(0);
outcode(c1, eflag);
emitf(Xjump);
emiti(q);
stuffdot(p);
break;
case WORDS:
outcode(c1, eflag);
outcode(c0, eflag);
break;
case FOR:
emitf(Xmark);
if(c1){
outcode(c1, eflag);
emitf(Xglob);
}
else{
emitf(Xmark);
emitf(Xword);
emits(strdup("*"));
emitf(Xdol);
}
emitf(Xmark); /* dummy value for Xlocal */
emitf(Xmark);
outcode(c0, eflag);
emitf(Xlocal);
p=emitf(Xfor);
q=emiti(0);
outcode(c2, eflag);
emitf(Xjump);
emiti(p);
stuffdot(q);
emitf(Xunlocal);
break;
case WORD:
emitf(Xword);
emits(strdup(t->str));
break;
case DUP:
if(t->rtype==DUPFD){
emitf(Xdup);
emiti(t->fd0);
emiti(t->fd1);
}
else{
emitf(Xclose);
emiti(t->fd0);
}
outcode(c1, eflag);
emitf(Xpopredir);
break;
case PIPEFD:
emitf(Xpipefd);
emiti(t->rtype);
p=emiti(0);
outcode(c0, eflag);
emitf(Xexit);
stuffdot(p);
break;
case REDIR:
emitf(Xmark);
outcode(c0, eflag);
emitf(Xglob);
switch(t->rtype){
case APPEND:
emitf(Xappend);
break;
case WRITE:
emitf(Xwrite);
break;
case READ:
case HERE:
emitf(Xread);
break;
}
emiti(t->fd0);
outcode(c1, eflag);
emitf(Xpopredir);
break;
case '=':
tt=t;
for(;t && t->type=='=';t=c2);
if(t){
for(t=tt;t->type=='=';t=c2){
emitf(Xmark);
outcode(c1, eflag);
emitf(Xmark);
outcode(c0, eflag);
emitf(Xlocal);
}
t=tt;
outcode(c2, eflag);
for(;t->type=='=';t=c2) emitf(Xunlocal);
}
else{
for(t=tt;t;t=c2){
emitf(Xmark);
outcode(c1, eflag);
emitf(Xmark);
outcode(c0, eflag);
emitf(Xassign);
}
}
t=tt; /* so tests below will work */
break;
case PIPE:
emitf(Xpipe);
emiti(t->fd0);
emiti(t->fd1);
p=emiti(0);
q=emiti(0);
outcode(c0, eflag);
emitf(Xexit);
stuffdot(p);
outcode(c1, eflag);
emitf(Xreturn);
stuffdot(q);
emitf(Xpipewait);
break;
}
if(t->type!=NOT && t->type!=';')
runq->iflast=t->type==IF;
else if(c0) runq->iflast=c0->type==IF;
}
/*
* switch code looks like this:
* Xmark
* (get switch value)
* Xjump 1f
* out: Xjump leave
* 1: Xmark
* (get case values)
* Xcase 1f
* (commands)
* Xjump out
* 1: Xmark
* (get case values)
* Xcase 1f
* (commands)
* Xjump out
* 1:
* leave:
* Xpopm
*/
void codeswitch(tree *t, int eflag)
{
int leave; /* patch jump address to leave switch */
int out; /* jump here to leave switch */
int nextcase; /* patch jump address to next case */
tree *tt;
if(c1->child[0]==nil
|| c1->child[0]->type!=';'
|| !iscase(c1->child[0]->child[0])){
yyerror("case missing in switch");
return;
}
emitf(Xmark);
outcode(c0, eflag);
emitf(Xjump);
nextcase=emiti(0);
out=emitf(Xjump);
leave=emiti(0);
stuffdot(nextcase);
t=c1->child[0];
while(t->type==';'){
tt=c1;
emitf(Xmark);
for(t=c0->child[0];t->type==ARGLIST;t=c0) outcode(c1, eflag);
emitf(Xcase);
nextcase=emiti(0);
t=tt;
for(;;){
if(t->type==';'){
if(iscase(c0)) break;
outcode(c0, eflag);
t=c1;
}
else{
if(!iscase(t)) outcode(t, eflag);
break;
}
}
emitf(Xjump);
emiti(out);
stuffdot(nextcase);
}
stuffdot(leave);
emitf(Xpopm);
}
int iscase(tree *t)
{
if(t->type!=SIMPLE) return 0;
do t=c0; while(t->type==ARGLIST);
return t->type==WORD && !t->quoted && strcmp(t->str, "case")==0;
}
code *codecopy(code *cp)
{
cp[0].i++;
return cp;
}
void codefree(code *cp)
{
code *p;
if(--cp[0].i!=0) return;
for(p=cp+1;p->f;p++){
if(p->f==Xappend || p->f==Xclose || p->f==Xread || p->f==Xwrite
|| p->f==Xasync || p->f==Xbackq || p->f==Xcase || p->f==Xfalse
|| p->f==Xfor || p->f==Xjump
|| p->f==Xsubshell || p->f==Xtrue) p++;
else if(p->f==Xdup || p->f==Xpipefd) p+=2;
else if(p->f==Xpipe) p+=4;
else if(p->f==Xword || p->f==Xdelhere) efree((++p)->s);
else if(p->f==Xfn){
efree(p[2].s);
p+=2;
}
}
efree((char *)cp);
}
|