void kref(char *name, FILE *fp, Table_T identifiers) { char buf[512]; const char *filename = ""; int linenum; if (name) filename = name; for (linenum = 1; fgets(buf, sizeof buf, fp) != NULL; linenum++) { int i, j; for (i = 0; (j = getword(buf, &i, first, rest)) > 0; i = j) { char *id = Str_sub(buf, j, i); const char *ln = Atom_int(linenum); Seq_T seq; Table_T files; files = Table_get(identifiers, id); if (files == NULL) { files = Table_new(0, (int (*)(const void *, const void *))strcmp, strhash); Table_put(identifiers, id, files); } else FREE(id); seq = Table_get(files, filename); if (seq == NULL) { seq = Seq_new(0); Table_put(files, Str_dup(filename, 1, 0, 1), seq); Seq_addlo(seq, (void *)ln); } else if (Seq_get(seq, 0) != ln) Seq_addlo(seq, (void *)ln); } } }
/* * Initializes the program counter and returns the number of instructions. */ void mapProgram(FILE* program) { Seq_T words = Seq_new(PROGRAM_HINT); int c = getc(program); while(c != EOF) { UM_Word temp = 0; temp = Bitpack_newu(temp, 8, 24, c); for(int bit = 16; bit >=0; bit -=8){ int b = getc(program); temp = Bitpack_newu(temp, 8, bit, b); } UM_Word* instr; NEW(instr); *instr = temp; Seq_addhi(words, instr); c = getc(program); } mapSegment(memorySegments, 0); mapInstructions(Seq_length(words)); int length = instructionLength(); for(int locToLoad = 0; locToLoad < length; locToLoad++){ UM_Word* value = (UM_Word*)Seq_get(words, locToLoad); loadInstruction(locToLoad, *value); FREE(value); } Seq_free(&words); }
void test_add(Seq_T segments) { Segment s; NEW (s); s->length = SEG_LENGTH; s->data = Seq_new(SEG_LENGTH); for (unsigned i = 0; i < s->length; i++) { uint32_t *word = malloc(sizeof(uint32_t)); *word = i; Seq_addhi(s->data, word); } add_seg(SEG_LENGTH, s); Segment test = Seq_get(segments, SEG_LENGTH); for (unsigned i = 0; i < test->length; i++) { uint32_t *word = Seq_get(test->data, i); if (*word != i) printf("ADD IS A FAIL!!!!!\n"); } printf("ADD IS A SUCCESS!!!\n"); }
/* * Input: Mem_T structure, unsigned integer * Output: returns the index at which the segment was stored. * Purpose: Adds a segment to memory with numWords words. */ int map_segment(Mem_T memory, unsigned numWords) { int segIndex; int reusedTest = 0; if(Seq_length(memory->reusable_indices) > 0){ segIndex = (int)(intptr_t)Seq_remlo(memory->reusable_indices); reusedTest = 1; } else segIndex = Seq_length(memory->segment_seq); Seq_T segment = Seq_new(numWords); uint32_t initializer = 0; for(unsigned i = 0; i < numWords; i++){ Seq_addhi(segment, (void *)(uintptr_t)initializer); } if(reusedTest) { Seq_put(memory->segment_seq, segIndex, segment); } else { Seq_addhi(memory->segment_seq, segment); } return segIndex; }
/* Takes a file pointer to store the program at segment 0 and returns the newly * created segmented memory. */ T Segment_new() { WORD_SIZE i; T seg_mem = malloc(sizeof(struct Segment_T)); assert(seg_mem != NULL); Seq_T ids = Seq_new(MAP_INCREMENT); Seq_T segments = Seq_new(MAP_INCREMENT); for (i = 0; i < MAP_INCREMENT; i++) { Seq_addhi(ids, (void *)(uintptr_t)i); } seg_mem->unmapped_ids = ids; seg_mem->segments = segments; return seg_mem; }
T Seq_seq(void *x, ...) { va_list ap; T seq = Seq_new(0); va_start(ap, x); for ( ; x; x = va_arg(ap, void *)) Seq_addhi(seq, x); va_end(ap); return seq; }
T Segmem_new(int num_words) { T segmem = malloc(sizeof(*segmem)); segmem->memory = Seq_new(1); segmem->length = 0; int *segment = malloc(num_words * sizeof(int)); Seq_addhi(segmem->memory, segment); return segmem; }
/* Input: a file pointer * Output: a Mem_T structure * Purpose: Initializes the Mem_T memory structure. Stores 32 bit words from * input into segment 0 in memory. This is location which holds the * instructions that the um will execute */ Mem_T Mem_new(FILE *input) { long unsigned lSize; uint32_t *buffer; size_t buffer_size; assert(input != NULL); Mem_T memory = malloc(sizeof(*memory)); assert(memory!=NULL); memory->segment_seq = Seq_new(0); memory->reusable_indices = Seq_new(0); /* determines length of input file */ fseek(input, 0, SEEK_END); lSize = ftell(input); rewind(input); /* creates buffer that will grab words from input*/ buffer = malloc(sizeof(uint32_t) * lSize/4); assert(buffer != NULL); buffer_size = fread(buffer, 4, lSize / 4, input); if(buffer_size != lSize/4){ fprintf(stderr, "Reading error\n"); } /* Program index will be 0*/ int programIndex = map_segment(memory, buffer_size); /*transfers instructions from buffer to segment 0 in memory*/ for(unsigned i = 0; i < 3; i++){ uint32_t word = buffer[i]; word = flip_word(word); fprintf(stderr, "word: %x\n", word); store_word(memory, word, programIndex, i); } free(buffer); return memory; }
static void asdl_function(Symbol f, Symbol caller[], Symbol callee[], int ncalls) { list_ty codelist = Seq_new(0), save, calleelist = Seq_new(0), callerlist = Seq_new(0); int i; dopending(f); for (i = 0; caller[i] != NULL; i++) { asdl_local(caller[i]); Seq_addhi(callerlist, to_generic_int(symboluid(caller[i]))); } for (i = 0; callee[i] != NULL; i++) { asdl_local(callee[i]); Seq_addhi(calleelist, to_generic_int(symboluid(callee[i]))); } save = interfaces; interfaces = codelist; gencode(caller, callee); asdl_segment(CODE); emitcode(); interfaces = save; put(rcc_Function(symboluid(f), callerlist, calleelist, ncalls, codelist)); }
void check_map_large(){ //These variable created for compiling sake Seq_T SEG_MEMORY = Seq_new(100); Seq_T UNMAP_SEQ = Seq_new(10); Umsegment_Id one = map_segment(6); Umsegment_Id two = map_segment(6); Umsegment_Id three = map_segment(8000); Umsegment_Id four = map_segment(6); int size = UArray_length(Seq_get(SEG_MEMORY, three)); if (size != 8000) fprintf(stderr, "Big segment is incorrect size %d.\n", size); unmap_segment(one); unmap_segment(two); unmap_segment(three); unmap_segment(four); Seq_free(&SEG_MEMORY); Seq_free(&UNMAP_SEQ); }
void unblack (Bit2_T bitmap, int cur_x, int cur_y) { int w_pixels = Bit2_width (bitmap); int h_pixels = Bit2_height (bitmap); Seq_T point_queue = Seq_new(w_pixels*h_pixels); assert(point_queue); assert(0 <= cur_x && cur_x < w_pixels); assert(0 <= cur_y && cur_y < h_pixels); if (Bit2_get(bitmap, cur_x, cur_y) != 1) { // if pixel is white assert(point_queue); Seq_free(&point_queue); return; } else { Seq_addhi(point_queue, (void*)makePoint(cur_x,cur_y)); while (Seq_length(point_queue) > 0) { PointPair temp = (PointPair)Seq_remlo(point_queue); assert(temp); int i = temp->i; int j = temp->j; freePoint(temp); if (Bit2_get(bitmap, i, j) == 1) { // if current is black pixel Bit2_put(bitmap, i, j, 0); // set current to white assert(0 <= i && i < w_pixels); assert(0 <= j && j < h_pixels); if (j != 0 && j != h_pixels-1) { // if not a top/bottom pixel if (i+1 < w_pixels && Bit2_get(bitmap, i+1, j) == 1) { // if Seq_addhi(point_queue, (void*)makePoint(i+1,j)); } if (i > 0 && Bit2_get(bitmap, i-1, j) == 1) { Seq_addhi(point_queue, (void*)makePoint(i-1,j)); } } if (i != 0 && i != w_pixels-1) { if (j+1 < h_pixels && Bit2_get(bitmap, i, j+1) == 1) { Seq_addhi(point_queue, (void*)makePoint(i,j+1)); } if (j > 0 && Bit2_get(bitmap, i, j-1) == 1) { Seq_addhi(point_queue, (void*)makePoint(i,j-1)); } } } } assert(point_queue); Seq_free(&point_queue); return; } }
/* initialize the new mem and return it */ Mem new_mem(Segment_T program) { Mem myMem = malloc(sizeof(*myMem)); assert(myMem != NULL); myMem->segs = Seq_new(2); assert(myMem->segs != NULL); Seq_addlo(myMem->segs, (void*) program); myMem->unmapped = Stack_new(); myMem->prog_counter = 0; return myMem; }
void check_map_capcity(){ //These variable created for compiling sake Seq_T SEG_MEMORY = Seq_new(100); Seq_T UNMAP_SEQ = Seq_new(10); for (int i = 0; i < 2000; i++){ int size = rand() % 20; Umsegment_Id id = map_segment(size); (void) id; if (Seq_get(SEG_MEMORY, i) == NULL) fprintf(stderr, "Segment %d not mapped.\n", size); i++; } for (int j = 1999; j >= 0; j--){ unmap_segment((Umsegment_Id) j); if (Seq_get(SEG_MEMORY, j) != NULL) fprintf(stderr, "Segment %d not unmapped.\n", j); } Seq_free(&SEG_MEMORY); Seq_free(&UNMAP_SEQ); }
void check_unmap(){ //These variable created for compiling sake Seq_T SEG_MEMORY = Seq_new(100); Seq_T UNMAP_SEQ = Seq_new(10); Umsegment_Id one = map_segment(10); Umsegment_Id two = map_segment(15); Umsegment_Id three = map_segment(20); Umsegment_Id four = map_segment(25); unmap_segment(two); if ((Umsegment_Id)(uintptr_t)Seq_remhi(UNMAP_SEQ) != 15) fprintf(stderr, "Unmapped id was not found in sequence"); Umsegment_Id five = map_segment(5); if(five != two) fprintf(stderr, "Unmapped id was not used"); unmap_segment(one); unmap_segment(five); unmap_segment(three); unmap_segment(four); Seq_free(&SEG_MEMORY); Seq_free(&UNMAP_SEQ); }
static Node asdl_gen(Node p) { Node q; list_ty forest = Seq_new(0); for (q = p; p != NULL; p = p->link) if (specific(p->op) == JUMP+V && specific(p->kids[0]->op) == ADDRG+P && p->kids[0]->syms[0]->scope == LABELS) { p->syms[0] = p->kids[0]->syms[0]; p->kids[0] = NULL; } for (p = q; p != NULL; p = p->link) Seq_addhi(forest, visit(p)); put(rcc_Forest(forest)); temps = NULL; return q; }
/* * Input: Mem_T structure, unsigned integer * Ouput: void * Purpose: Copies the segment located at segIndex and stores it in segment 0 * The segment previously at index 0 is freed. */ void load_program(Mem_T memory, unsigned segIndex) { /*Segment that will become new program*/ Seq_T program_to_load_temp = Seq_get(memory->segment_seq, segIndex); int length = Seq_length(program_to_load_temp); /*New sequence that will be put in segment 0*/ Seq_T program_to_load = Seq_new(length); /*Copies program_to_load_temp to program_to_load*/ for(int i = 0; i < length; i++) { Seq_addhi(program_to_load, Seq_get(program_to_load_temp, i)); } Seq_T old_program = Seq_put(memory->segment_seq, 0, program_to_load); Seq_free(&old_program); }
int run( FILE* fin ) { char word[64]; size_t wsize = sizeof( word ); while (fgets( word, wsize, fin )) { Seq_T solutions = Seq_new( 5 ); if (wstable_hasWord( word, solutions )) { process_solutions( solutions ); } Seq_free( &solutions ); } return 0; }
int main(int argc, char *argv[]) { (void) argc; (void) argv; Seq_T segments = Seq_new(0); test_add(segments); test_get_seg(); test_get_word(segments); test_move(); test_clear(); }
/* returns a Segment array containing segments - hint must be >= 1 */ Segment Segments_new(int hint) { Seq_T outer = NULL; TRY outer = Seq_new(hint); assert(outer); for (int i = 0; i < hint; i++) { UArray_T inner = UArray_new(0, sizeof(uint32_t)); assert(inner); Seq_addhi(outer, inner); } EXCEPT (Mem_Failed) return NULL; END_TRY; return outer; }
/* typeuid - returns ty's uid, adding the type, if necessary */ static int typeuid(const Type ty) { sym_type_ty type; if (ty->x.typeno != 0) return ty->x.typeno; ty->x.typeno = pickle->nuids++; switch (ty->op) { #define xx(op) case op: type = sym_##op(ty->size, ty->align); break xx(INT); xx(UNSIGNED); xx(FLOAT); xx(VOID); #undef xx #define xx(op) case op: type = sym_##op(ty->size, ty->align, typeuid(ty->type)); break xx(POINTER); xx(CONST); xx(VOLATILE); #undef xx case ARRAY: type = sym_ARRAY(ty->size, ty->align, typeuid(ty->type), 0); if (ty->type->size > 0) type->v.sym_ARRAY.nelems = ty->size/ty->type->size; break; case CONST+VOLATILE: type = sym_CONST(ty->size, ty->align, typeuid(ty->type)); break; case ENUM: { list_ty ids = Seq_new(0); int i; for (i = 0; ty->u.sym->u.idlist[i] != NULL; i++) Seq_addhi(ids, sym_enum_(ty->u.sym->u.idlist[i]->name, ty->u.sym->u.idlist[i]->u.value)); assert(i > 0); type = sym_ENUM(ty->size, ty->align, ty->u.sym->name, ids); break; } case STRUCT: case UNION: { list_ty fields = Seq_new(0); Field p = fieldlist(ty); for ( ; p != NULL; p = p->link) Seq_addhi(fields, sym_field(p->name, typeuid(p->type), p->offset, p->bitsize, p->lsb)); if (ty->op == STRUCT) type = sym_STRUCT(ty->size, ty->align, ty->u.sym->name, fields); else type = sym_UNION (ty->size, ty->align, ty->u.sym->name, fields); break; } case FUNCTION: { list_ty formals = Seq_new(0); if (ty->u.f.proto != NULL && ty->u.f.proto[0] != NULL) { int i; for (i = 0; ty->u.f.proto[i] != NULL; i++) Seq_addhi(formals, to_generic_int(typeuid(ty->u.f.proto[i]))); } else if (ty->u.f.proto != NULL && ty->u.f.proto[0] == NULL) Seq_addhi(formals, to_generic_int(typeuid(voidtype))); type = sym_FUNCTION(ty->size, ty->align, typeuid(ty->type), formals); break; } default: assert(0); } Seq_addhi(pickle->items, sym_Type(ty->x.typeno, type)); return ty->x.typeno; }
__declspec(dllexport) void* curl_shim_alloc_strings() { Seq_T seq = Seq_new(0); return (void*)seq; }
int main(int argc, char *argv[]) { int c; sp = Seq_new(0); Fmt_register('D', MP_fmt); Fmt_register('U', MP_fmtu); while ((c = getchar()) != EOF) { MP_T x = NULL, y = NULL, z = NULL; TRY switch (c) { default: if (isprint(c)) Fmt_fprint(stderr, "?'%c'", c); else Fmt_fprint(stderr, "?'\\%03o'", c); Fmt_fprint(stderr, " is unimplemented\n"); break; case ' ': case '\t': case '\n': case '\f': case '\r': break; case 'c': while (Seq_length(sp) > 0) { MP_T x = Seq_remhi(sp); FREE(x); } break; case 'q': while (Seq_length(sp) > 0) { MP_T x = Seq_remhi(sp); FREE(x); } Seq_free(&sp); return EXIT_SUCCESS; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { char buf[512]; z = MP_new(0); { int i = 0; for ( ; strchr(&"zyxwvutsrqponmlkjihgfedcba9876543210"[36-ibase], tolower(c)); c = getchar(), i++) if (i < (int)sizeof (buf) - 1) buf[i] = c; if (i > (int)sizeof (buf) - 1) { i = (int)sizeof (buf) - 1; Fmt_fprint(stderr, "?integer constant exceeds %d digits\n", i); } buf[i] = '\0'; if (c != EOF) ungetc(c, stdin); } MP_fromstr(z, buf, ibase, NULL); break; } case '+': y = pop(); x = pop(); z = MP_new(0); (*f->add)(z, x, y); break; case '-': y = pop(); x = pop(); z = MP_new(0); (*f->sub)(z, x, y); break; case '*': y = pop(); x = pop(); z = MP_new(0); (*f->mul)(z, x, y); break; case '/': y = pop(); x = pop(); z = MP_new(0); (*f->div)(z, x, y); break; case '%': y = pop(); x = pop(); z = MP_new(0); (*f->mod)(z, x, y); break; case '&': y = pop(); x = pop(); z = MP_new(0); MP_and(z, x, y); break; case '|': y = pop(); x = pop(); z = MP_new(0); MP_or (z, x, y); break; case '^': y = pop(); x = pop(); z = MP_new(0); MP_xor(z, x, y); break; case '!': z = pop(); MP_not(z, z); break; case '~': z = pop(); MP_neg(z, z); break; case 'i': case 'o': { long n; x = pop(); n = MP_toint(x); if (n < 2 || n > 36) Fmt_fprint(stderr, "?%d is an illegal base\n",n); else if (c == 'i') ibase = n; else obase = n; if (obase == 2 || obase == 8 || obase == 16) f = &u; else f = &s; break; } case 'p': Fmt_print(f->fmt, z = pop(), obase); break; case 'f': { int n = Seq_length(sp); while (--n > 0) Fmt_print(f->fmt, Seq_get(sp, n), obase); break; } case '<': { long s; y = pop(); z = pop(); s = MP_toint(y); if (s < 0 || s > INT_MAX) { Fmt_fprint(stderr, "?%d is an illegal shift amount\n", s); break; }; MP_lshift(z, z, s); break; } case '>': { long s; y = pop(); z = pop(); s = MP_toint(y); if (s < 0 || s > INT_MAX) { Fmt_fprint(stderr, "?%d is an illegal shift amount\n", s); break; }; MP_rshift(z, z, s); break; } case 'k': { long n; x = pop(); n = MP_toint(x); if (n < 2 || n > INT_MAX) Fmt_fprint(stderr, "?%d is an illegal precision\n", n); else if (Seq_length(sp) > 0) Fmt_fprint(stderr, "?nonempty stack\n"); else MP_set(n); break; } case 'd': { MP_T x = pop(); z = MP_new(0); Seq_addhi(sp, x); MP_addui(z, x, 0); break; } } EXCEPT(MP_Overflow) Fmt_fprint(stderr, "?overflow\n"); EXCEPT(MP_Dividebyzero) Fmt_fprint(stderr, "?divide by 0\n"); END_TRY; if (z) Seq_addhi(sp, z); FREE(x); FREE(y); } while (Seq_length(sp) > 0) { MP_T x = Seq_remhi(sp); FREE(x); } Seq_free(&sp); return EXIT_SUCCESS; }