void outcode(void) { int f; Biobuf b; f = open(outfile, OWRITE); if(f < 0) { diag(Z, "cannot open %s", outfile); return; } Binit(&b, f, OWRITE); Bprint(&b, "go object %s %s %s\n", getgoos(), getgoarch(), getgoversion()); if(pragcgobuf.to > pragcgobuf.start) { Bprint(&b, "\n"); Bprint(&b, "$$ // exports\n\n"); Bprint(&b, "$$ // local types\n\n"); Bprint(&b, "$$ // cgo\n"); Bprint(&b, "%s", fmtstrflush(&pragcgobuf)); Bprint(&b, "\n$$\n\n"); } Bprint(&b, "!\n"); writeobj(ctxt, &b); Bterm(&b); close(f); lastp = P; }
int assemble(char *file) { char *ofile, *p; int i, of; ofile = alloc(strlen(file)+3); // +3 for .x\0 (x=thechar) strcpy(ofile, file); p = utfrrune(ofile, pathchar()); if(p) { include[0] = ofile; *p++ = 0; } else p = ofile; if(outfile == 0) { outfile = p; if(outfile){ p = utfrrune(outfile, '.'); if(p) if(p[1] == 's' && p[2] == 0) p[0] = 0; p = utfrune(outfile, 0); p[0] = '.'; p[1] = thechar; p[2] = 0; } else outfile = "/dev/null"; } of = create(outfile, OWRITE, 0664); if(of < 0) { yyerror("%ca: cannot create %s", thechar, outfile); errorexit(); } Binit(&obuf, of, OWRITE); Bprint(&obuf, "go object %s %s %s\n", getgoos(), getgoarch(), getgoversion()); Bprint(&obuf, "!\n"); for(pass = 1; pass <= 2; pass++) { pinit(file); for(i=0; i<nDlist; i++) dodefine(Dlist[i]); yyparse(); cclean(); if(nerrors) return nerrors; } writeobj(ctxt, &obuf); Bflush(&obuf); return 0; }
void dumpobj(void) { NodeList *externs, *tmp; char arhdr[ArhdrSize]; vlong startobj, size; Sym *zero; bout = Bopen(outfile, OWRITE); if(bout == nil) { flusherrors(); print("can't create %s: %r\n", outfile); errorexit(); } startobj = 0; if(writearchive) { Bwrite(bout, "!<arch>\n", 8); memset(arhdr, 0, sizeof arhdr); Bwrite(bout, arhdr, sizeof arhdr); startobj = Boffset(bout); } Bprint(bout, "go object %s %s %s %s\n", getgoos(), getgoarch(), getgoversion(), expstring()); dumpexport(); if(writearchive) { Bflush(bout); size = Boffset(bout) - startobj; if(size&1) Bputc(bout, 0); Bseek(bout, startobj - ArhdrSize, 0); formathdr(arhdr, "__.PKGDEF", size); Bwrite(bout, arhdr, ArhdrSize); Bflush(bout); Bseek(bout, startobj + size + (size&1), 0); memset(arhdr, 0, ArhdrSize); Bwrite(bout, arhdr, ArhdrSize); startobj = Boffset(bout); Bprint(bout, "go object %s %s %s %s\n", getgoos(), getgoarch(), getgoversion(), expstring()); } Bprint(bout, "\n!\n"); externs = nil; if(externdcl != nil) externs = externdcl->end; dumpglobls(); dumptypestructs(); // Dump extra globals. tmp = externdcl; if(externs != nil) externdcl = externs->next; dumpglobls(); externdcl = tmp; zero = pkglookup("zerovalue", runtimepkg); ggloblsym(zero, zerosize, 1, 1); dumpdata(); writeobj(ctxt, bout); if(writearchive) { Bflush(bout); size = Boffset(bout) - startobj; if(size&1) Bputc(bout, 0); Bseek(bout, startobj - ArhdrSize, 0); snprint(namebuf, sizeof namebuf, "_go_.%c", thechar); formathdr(arhdr, namebuf, size); Bwrite(bout, arhdr, ArhdrSize); } Bterm(bout); }
void genobj( void ) { short int *symbol, *target; short int *p, *q, *r; short int action; set_size *mp; a_sym *sym; a_pro *pro; an_item *item; a_state *x; a_shift_action *tx; a_reduce_action *rx; int i, j, savings; for( i = nterm; i < nsym; ++i ) symtab[i]->token = i - nterm; label = OPTENTRY( nstate - 1 ); emitins( JMP, TOKENTRY( startstate->sidx ) ); target = CALLOC( nsym, short int ); for( i = 0; i < nsym; ++i ) target[i] = DEFAULT; symbol = CALLOC( nsym, short int ); for( i = 0; i < nstate; ++i ) { x = statetab[i]; q = symbol; for( tx = x->trans; (sym = tx->sym) != NULL; ++tx ) { if( sym == eofsym ) { action = ACCEPT; } else if( sym->idx < nterm ) { action = TOKENTRY(tx->state->sidx); } else { action = OPTENTRY(tx->state->sidx); } *q++ = sym->idx; target[sym->idx] = action; } savings = 0; for( rx = x->redun; (pro = rx->pro) != NULL; ++rx ) { action = PROENTRY( pro->pidx ); mp = Members( rx->follow ); if( mp - setmembers > savings ) { savings = mp - setmembers; r = q; } while( --mp >= setmembers ) { *q++ = *mp; target[*mp] = action; } } action = DEFAULT; if( savings ) { action = target[*r]; p = r; while( --savings >= 0 ) { target[*p++] = DEFAULT; } } emitins( LBL, TOKENTRY( x->sidx ) ); emitins( SCAN, 0 ); emitins( LBL, OPTENTRY( x->sidx ) ); emitins( CALL, VBLENTRY( x->sidx ) ); q = symbol; for( j = nterm; j < nsym; ++j ) { if( target[j] != DEFAULT ) { *q++ = j; } } if( q != symbol ) { emitv( symbol, target, q - symbol ); for( p = symbol; p < q; ++p ) { target[*p] = DEFAULT; } } emitins( LBL, VBLENTRY( x->sidx ) ); q = symbol; for( j = 0; j < nterm; ++j ) { if( target[j] != DEFAULT ) { *q++ = j; } } emitt( symbol, target, q - symbol, action ); for( p = symbol; p < q; ++p ) { target[*p] = DEFAULT; } } FREE( target ); FREE( symbol ); for( i = 0; i < npro; ++i ) { pro = protab[i]; if( pro != startpro ) { for( item = pro->item; item->p.sym != NULL; ) { ++item; } emitins( LBL, PROENTRY( pro->pidx ) ); emitins( ACTION, PROPACK( item - pro->item, i ) ); emitins( REDUCE, PROPACK( item - pro->item, pro->sym->token ) ); } } writeobj( label + 1 ); }