int InitializeStructure(TYP *tp) { SYM *sp; int nbytes; needpunc(begin,25); nbytes = 0; sp = sp->GetPtr(tp->lst.GetHead()); /* start at top of symbol table */ while(sp != 0) { while(nbytes < sp->value.i) { /* align properly */ // nbytes += GenerateByte(0); GenerateByte(0); // nbytes++; } nbytes += InitializeType(sp->tp); if( lastst == comma) NextToken(); else if(lastst == end) break; else error(ERR_PUNCT); sp = sp->GetNextPtr(); } if( nbytes < tp->size) genstorage( tp->size - nbytes); needpunc(end,26); return tp->size; }
word32 RandomNumberGenerator::GenerateWord32(word32 min, word32 max) { word32 range = max-min; const int maxBytes = BytePrecision(range); const int maxBits = BitPrecision(range); word32 value; do { value = 0; for (int i=0; i<maxBytes; i++) value = (value << 8) | GenerateByte(); value = Crop(value, maxBits); } while (value > range); return value+min; }
void RandomNumberGenerator::DiscardBytes(unsigned int n) { while (n--) GenerateByte(); }
void RandomNumberGenerator::GenerateBlock(byte *output, unsigned int size) { while (size--) *output++ = GenerateByte(); }
unsigned int RandomNumberGenerator::GenerateBit() { return Parity(GenerateByte()); }
unsigned int RandomNumberGenerator::GenerateBit() { return GenerateByte() & 1; }
/* * process ParseSpecifierarations of the form: * * <type> <ParseSpecifier>, <ParseSpecifier>...; * * leaves the ParseSpecifierarations in the symbol table pointed to by * table and returns the number of bytes declared. al is the * allocation type to assign, ilc is the initial location * counter. if al is sc_member then no initialization will * be processed. ztype should be bt_struct for normal and in * structure ParseSpecifierarations and sc_union for in union ParseSpecifierarations. */ int declare(TABLE *table,int al,int ilc,int ztype) { SYM *sp, *sp1, *sp2; TYP *dhead; char stnm[200]; static long old_nbytes; int nbytes; nbytes = 0; ParseSpecifier(table); dhead = head; for(;;) { declid = 0; bit_width = -1; ParseDeclarationPrefix(ztype==bt_union); if( declid != 0) { /* otherwise just struct tag... */ sp = allocSYM(); sp->name = declid; sp->storage_class = al; if (bit_width > 0 && bit_offset > 0) { // share the storage word with the previously defined field nbytes = old_nbytes - ilc; } old_nbytes = ilc + nbytes; if (al != sc_member) { // sp->isTypedef = isTypedef; if (isTypedef) sp->storage_class = sc_typedef; isTypedef = FALSE; } while( (ilc + nbytes) % alignment(head)) { if( al != sc_member && al != sc_external && al != sc_auto) { dseg(); GenerateByte(0); } ++nbytes; } if( al == sc_static) { sp->value.i = nextlabel++; } else if( ztype == bt_union) sp->value.i = ilc; else if( al != sc_auto ) sp->value.i = ilc + nbytes; else sp->value.i = -(ilc + nbytes + head->size); if (bit_width == -1) sp->tp = head; else { sp->tp = allocTYP(); *(sp->tp) = *head; sp->tp->type = bt_bitfield; sp->tp->size = head->size;//tp_int.size; sp->tp->bit_width = bit_width; sp->tp->bit_offset = bit_offset; } if( (sp->tp->type == bt_func) && sp->storage_class == sc_global ) sp->storage_class = sc_external; if(ztype == bt_union) nbytes = imax(nbytes,sp->tp->size); else if(al != sc_external) nbytes += sp->tp->size; if( sp->tp->type == bt_ifunc && (sp1 = search(sp->name,table)) != 0 && sp1->tp->type == bt_func ) { sp1->tp = sp->tp; sp1->storage_class = sp->storage_class; // sp1->value.i = sp->value.i; sp1->IsPrototype = sp->IsPrototype; sp = sp1; } else { sp2 = search(sp->name,table); if (sp2 == NULL) insert(sp,table); else { if (funcdecl==2) sp2->tp = sp->tp; //else if (!sp2->IsPrototype) // insert(sp,table); } } if( sp->tp->type == bt_ifunc) { /* function body follows */ ParseFunction(sp); return nbytes; } if( (al == sc_global || al == sc_static) && sp->tp->type != bt_func && sp->storage_class!=sc_typedef) doinit(sp); } if (funcdecl==TRUE) { if (lastst==comma || lastst==semicolon) break; if (lastst==closepa) goto xit1; } else if (catchdecl==TRUE) { if (lastst==closepa) goto xit1; } else if (lastst == semicolon) break; needpunc(comma); if(declbegin(lastst) == 0) break; head = dhead; } NextToken(); xit1: return nbytes; }
void RandomNumberGenerator::DiscardBytes(size_t n) { while (n--) GenerateByte(); }
void dumplits() { char *cp; int64_t nn; slit *lit; dfs.printf("<Dumplits>\n"); roseg(); if (casetab) { nl(); align(8); nl(); } while (casetab != nullptr) { nl(); if (casetab->pass == 2) put_label(casetab->label, "", casetab->nmspace, 'D'); for (nn = 0; nn < casetab->num; nn++) { if (casetab->cases[nn].pass==2) GenerateLabelReference(casetab->cases[nn].label, 0); } casetab = casetab->next; } if (quadtab) { nl(); align(8); nl(); } // Dumping to ro segment - no need for GC skip /* nn = GetQuadtabLen(); if (nn) { sprintf_s(buf, sizeof(buf), "\tdw\t$%I64X ; GC_skip\n", nn | 0xFFF0200000000000LL); ofs.printf("%s", buf); } */ while(quadtab != nullptr) { nl(); if (DataLabels[quadtab->label]) { put_label(quadtab->label, "", quadtab->nmspace, 'D'); ofs.printf("\tdh\t"); quadtab->Pack(64); ofs.printf("%s", quadtab->ToString(64)); outcol += 35; } quadtab = quadtab->next; } if (strtab) { nl(); align(8); nl(); } //nn = GetStrtabLen(); //if (nn) { // sprintf_s(buf, sizeof(buf), "\tdw\t$%I64X ; GC_skip\n", nn | 0xFFF0200000000000LL); // ofs.printf("%s", buf); //} for (lit = strtab; lit; lit = lit->next) { ENODE *ep; agr = ep = (ENODE *)lit->str; dfs.printf("."); nl(); if (!lit->isString) { if (DataLabels[lit->label]) put_label(lit->label, strip_crlf(&lit->str[1]), lit->nmspace, 'D'); } else put_label(lit->label,strip_crlf(&lit->str[1]),lit->nmspace,'D'); if (lit->isString) { cp = lit->str; switch (*cp) { case 'B': cp++; while (*cp) GenerateByte(*cp++); GenerateByte(0); break; case 'C': cp++; while (*cp) GenerateChar(*cp++); GenerateChar(0); break; case 'H': cp++; while (*cp) GenerateHalf(*cp++); GenerateHalf(0); break; case 'W': cp++; while (*cp) GenerateWord(*cp++); GenerateWord(0); break; } } else { if (DataLabels[lit->label]) { ep->PutStructConst(ofs); } } } strtab = nullptr; nl(); dfs.printf("</Dumplits>\n"); }
void doinit(SYM *sp) { char lbl[200]; int algn; enum e_sg oseg; oseg = noseg; lbl[0] = 0; // Initialize constants into read-only data segment. Constants may be placed // in ROM along with code. if (sp->isConst) { oseg = rodataseg; } if (sp->storage_class == sc_thread) { if (sp->tp->type==bt_struct || sp->tp->type==bt_union) algn = imax(sp->tp->alignment,2); else if (sp->tp->type==bt_pointer && sp->tp->val_flag) algn = imax(sp->tp->GetBtp()->alignment,2); else algn = 2; seg(oseg==noseg ? tlsseg : oseg,algn); nl(); } else if (sp->storage_class == sc_static || lastst==assign) { if (sp->tp->type==bt_struct || sp->tp->type==bt_union) algn = imax(sp->tp->alignment,2); else if (sp->tp->type==bt_pointer && sp->tp->val_flag) algn = imax(sp->tp->GetBtp()->alignment,2); else algn = 2; seg(oseg==noseg ? dataseg : oseg,algn); /* initialize into data segment */ nl(); /* start a new line in object */ } else { if (sp->tp->type==bt_struct || sp->tp->type==bt_union) algn = imax(sp->tp->alignment,2); else if (sp->tp->type==bt_pointer && sp->tp->val_flag) algn = imax(sp->tp->GetBtp()->alignment,2); else algn = 2; seg(oseg==noseg ? bssseg : oseg,algn); /* initialize into data segment */ nl(); /* start a new line in object */ } if(sp->storage_class == sc_static || sp->storage_class == sc_thread) { sp->realname = my_strdup(put_label(sp->value.i, (char *)sp->name->c_str(), GetNamespace(), 'D')); } else { if (sp->storage_class == sc_global) { strcpy_s(lbl, sizeof(lbl), "public "); if (curseg==dataseg) strcat_s(lbl, sizeof(lbl), "data "); else if (curseg==bssseg) strcat_s(lbl, sizeof(lbl), "bss "); else if (curseg==tlsseg) strcat_s(lbl, sizeof(lbl), "tls "); } strcat_s(lbl, sizeof(lbl), sp->name->c_str()); gen_strlab(lbl); } if (lastst == kw_firstcall) { GenerateByte(1); return; } else if( lastst != assign) { genstorage(sp->tp->size); } else { NextToken(); InitializeType(sp->tp); } endinit(); if (sp->storage_class == sc_global) ofs.printf("\nendpublic\n"); }
int initbyte() { GenerateByte(GetIntegerExpression((ENODE **)NULL)); return 1; }
int initbyte() { GenerateByte(GetIntegerExpression()); return 1; }
/* * process declarations of the form: * * <type> <specifier>, <specifier>...; * * leaves the declarations in the symbol table pointed to by * table and returns the number of bytes declared. al is the * allocation type to assign, ilc is the initial location * counter. if al is sc_member then no initialization will * be processed. ztype should be bt_struct for normal and in * structure ParseSpecifierarations and sc_union for in union ParseSpecifierarations. */ int declare(TABLE *table,int al,int ilc,int ztype) { SYM *sp, *sp1, *sp2; TYP *dhead, *tp1, *tp2; ENODE *ep1, *ep2; char stnm[200]; int op; int fd; int fn_doneinit = 0; static long old_nbytes; int nbytes; nbytes = 0; if (ParseSpecifier(table)) return nbytes; dhead = head; for(;;) { declid = (char *)NULL; bit_width = -1; ParseDeclarationPrefix(ztype==bt_union); // If a function declaration is taking place and just the type is // specified without a parameter name, assign an internal compiler // generated name. if (funcdecl>0 && funcdecl != 10 && declid==(char *)NULL) { sprintf(lastid, "_p%d", nparms); declid = litlate(lastid); names[nparms++] = declid; missingArgumentName = TRUE; } if( declid != NULL) { /* otherwise just struct tag... */ sp = allocSYM(); sp->name = declid; sp->storage_class = al; if (bit_width > 0 && bit_offset > 0) { // share the storage word with the previously defined field nbytes = old_nbytes - ilc; } old_nbytes = ilc + nbytes; if (al != sc_member) { // sp->isTypedef = isTypedef; if (isTypedef) sp->storage_class = sc_typedef; isTypedef = FALSE; } while( (ilc + nbytes) % roundAlignment(head)) { if( al != sc_member && al != sc_external && al != sc_auto) { if (al==sc_thread) tseg(); else dseg(); GenerateByte(0); } ++nbytes; } // Set the struct member storage offset. if( al == sc_static || al==sc_thread) { sp->value.i = nextlabel++; } else if( ztype == bt_union) sp->value.i = ilc; else if( al != sc_auto ) sp->value.i = ilc + nbytes; // Auto variables are referenced negative to the base pointer // Structs need to be aligned on the boundary of the largest // struct element. If a struct is all chars this will be 2. // If a struct contains a pointer this will be 8. It has to // be the worst case alignment. else { sp->value.i = -(ilc + nbytes + roundSize(head)); } if (bit_width == -1) sp->tp = head; else { sp->tp = allocTYP(); *(sp->tp) = *head; sp->tp->type = bt_bitfield; sp->tp->size = head->size;//tp_int.size; sp->tp->bit_width = bit_width; sp->tp->bit_offset = bit_offset; } if (isConst) sp->tp->isConst = TRUE; if((sp->tp->type == bt_func) && sp->storage_class == sc_global ) sp->storage_class = sc_external; // Increase the storage allocation by the type size. if(ztype == bt_union) nbytes = imax(nbytes,sp->tp->size); else if(al != sc_external) { // If a pointer to a function is defined in a struct. if (isStructDecl && (sp->tp->type==bt_func || sp->tp->type==bt_ifunc)) nbytes += 8; else nbytes += roundSize(sp->tp); } if (sp->tp->type == bt_ifunc && (sp1 = search(sp->name,table)) != 0 && sp1->tp->type == bt_func ) { sp1->tp = sp->tp; sp1->storage_class = sp->storage_class; sp1->value.i = sp->value.i; sp1->IsPrototype = sp->IsPrototype; sp = sp1; } else { sp2 = search(sp->name,table); if (sp2 == NULL) insert(sp,table); else { if (funcdecl==2) sp2->tp = sp->tp; //else if (!sp2->IsPrototype) // insert(sp,table); } } if (needParseFunction) { needParseFunction = FALSE; fn_doneinit = ParseFunction(sp); if (sp->tp->type != bt_pointer) return nbytes; } // if(sp->tp->type == bt_ifunc) { /* function body follows */ // ParseFunction(sp); // return nbytes; // } if( (al == sc_global || al == sc_static || al==sc_thread) && !fn_doneinit && sp->tp->type != bt_func && sp->tp->type != bt_ifunc && sp->storage_class!=sc_typedef) doinit(sp); } if (funcdecl>0) { if (lastst==comma || lastst==semicolon) break; if (lastst==closepa) goto xit1; } else if (catchdecl==TRUE) { if (lastst==closepa) goto xit1; } else if (lastst == semicolon) break; else if (lastst == assign) { tp1 = nameref(&ep1); op = en_assign; // NextToken(); tp2 = asnop(&ep2); if( tp2 == 0 || !IsLValue(ep1) ) error(ERR_LVALUE); else { tp1 = forcefit(&ep1,tp1,&ep2,tp2); ep1 = makenode(op,ep1,ep2); } sp->initexp = ep1; if (lastst==semicolon) break; } needpunc(comma); if(declbegin(lastst) == 0) break; head = dhead; } NextToken(); xit1: return nbytes; }
void doinit(SYM *sp) { static bool first = true; char lbl[200]; int algn; enum e_sg oseg; char buf[500]; std::streampos endpoint; TYP *tp; hasPointer = false; if (first) { firstPrim = true; first = false; } oseg = noseg; lbl[0] = 0; // Initialize constants into read-only data segment. Constants may be placed // in ROM along with code. if (sp->isConst) { oseg = rodataseg; } if (sp->storage_class == sc_thread) { if (sp->tp->type==bt_struct || sp->tp->type==bt_union) algn = imax(sp->tp->alignment,8); else if (sp->tp->type==bt_pointer)// && sp->tp->val_flag) algn = imax(sp->tp->GetBtp()->alignment,8); else algn = 2; seg(oseg==noseg ? tlsseg : oseg,algn); nl(); } else if (sp->storage_class == sc_static || lastst==assign) { if (sp->tp->type==bt_struct || sp->tp->type==bt_union) algn = imax(sp->tp->alignment,8); else if (sp->tp->type==bt_pointer)// && sp->tp->val_flag) algn = imax(sp->tp->GetBtp()->alignment,8); else algn = 2; seg(oseg==noseg ? dataseg : oseg,algn); /* initialize into data segment */ nl(); /* start a new line in object */ } else { if (sp->tp->type==bt_struct || sp->tp->type==bt_union) algn = imax(sp->tp->alignment,8); else if (sp->tp->type==bt_pointer)// && sp->tp->val_flag) algn = imax(sp->tp->GetBtp()->alignment,8); else algn = 2; seg(oseg==noseg ? (lastst==assign ? dataseg : bssseg) : oseg,algn); /* initialize into data segment */ nl(); /* start a new line in object */ } if (sp->storage_class == sc_static || sp->storage_class == sc_thread) { //strcpy_s(glbl, sizeof(glbl), gen_label((int)sp->value.i, (char *)sp->name->c_str(), GetNamespace(), 'D')); if (sp->tp->IsSkippable()) { patchpoint = ofs.tellp(); sprintf_s(buf, sizeof(buf), "\talign\t8\n\tdw\t$FFF0200000000001\n"); ofs.printf(buf); } sp->realname = my_strdup(put_label((int)sp->value.i, (char *)sp->name->c_str(), GetNamespace(), 'D')); strcpy_s(glbl2, sizeof(glbl2), gen_label((int)sp->value.i, (char *)sp->name->c_str(), GetNamespace(), 'D')); } else { if (sp->storage_class == sc_global) { strcpy_s(lbl, sizeof(lbl), "public "); if (curseg==dataseg) strcat_s(lbl, sizeof(lbl), "data "); else if (curseg==bssseg) strcat_s(lbl, sizeof(lbl), "bss "); else if (curseg==tlsseg) strcat_s(lbl, sizeof(lbl), "tls "); } strcat_s(lbl, sizeof(lbl), sp->name->c_str()); if (sp->tp->IsSkippable()) { patchpoint = ofs.tellp(); sprintf_s(buf, sizeof(buf), "\talign\t8\n\tdw\t$FFF0200000000001\n"); ofs.printf(buf); } strcpy_s(glbl2, sizeof(glbl2), sp->name->c_str()); gen_strlab(lbl); } if (lastst == kw_firstcall) { GenerateByte(1); return; } else if( lastst != assign) { hasPointer = sp->tp->FindPointer(); genstorage(sp->tp->size); } else { NextToken(); hasPointer = sp->tp->FindPointer(); typ_sp = 0; tp = sp->tp; push_typ(tp); while (tp = tp->GetBtp()) { push_typ(tp); } brace_level = 0; sp->tp->Initialize(nullptr); if (sp->tp->numele == 0) { if (sp->tp->GetBtp()) { if (sp->tp->GetBtp()->type == bt_char || sp->tp->GetBtp()->type == bt_uchar || sp->tp->GetBtp()->type == bt_ichar || sp->tp->GetBtp()->type == bt_iuchar ) { sp->tp->numele = laststrlen; sp->tp->size = laststrlen; } } } } if (!hasPointer && sp->tp->IsSkippable()) { endpoint = ofs.tellp(); ofs.seekp(patchpoint); sprintf_s(buf, sizeof(buf), "\talign\t8\n\tdw\t$%I64X\n", ((genst_cumulative + 7LL) >> 3LL) | 0xFFF0200000000000LL); ofs.printf(buf); ofs.seekp(endpoint); genst_cumulative = 0; }