static void copy_segment_to_m0(UM_machine machine, UM_segment_ID segment_ID) { Seq_T mapped_segments = machine->address_space->mapped_segments; UM_segment temp = Seq_get(mapped_segments, segment_ID); int temp_length = UArray_length(temp->words); UM_segment duplicate = get_new_segment_of_size(temp_length); /* copy words into the new segment */ for(int i=0; i<temp_length; i++){ UM_word *word_i_p = UArray_at(temp->words, i); UM_word *duplicate_word_i_p = UArray_at(duplicate->words, i); *duplicate_word_i_p = *word_i_p; } duplicate->ID = 0; /* free the old program segment */ UM_segment program = Seq_get(mapped_segments, 0); free_segment_memory(&program); /* put the duplicate segment in the place of the old program segment */ Seq_put(mapped_segments, 0, duplicate); /* update num_instructions */ machine->num_instructions = temp_length; }
void verifyMapped(Seq_T memorySegments, Seq_T unmappedSegments) { // Verifies that the available unmapped segments is updated upon mapping for(int i = 0; i < Seq_length(unmappedSegments); i++){ UM_Word index = *(UM_Word*)Seq_get(unmappedSegments, i); if((UArray_T)Seq_get(memorySegments, index) != NULL){ fprintf(stderr, "Unmapped segments not updated correctly for: %u\n", index); exit(1); } } for(int i = 0; i < MEM_SEG_LEN; i++) { UArray_T segments = (UArray_T)Seq_get(memorySegments, i); if (segments != NULL) { for(int j = 0; j < UArray_length(segments); j++) { // Each 32-bit word must be initialized to 0 printf("memseg: %u\n", *(UM_Word*)UArray_at(segments, j)); if(*(UM_Word*)UArray_at(segments, j) != 0) { fprintf(stderr, "segment incorrectly initialized\n"); exit(1); } } } } printf("Verified bitch.\n"); }
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"); }
void verifyMapped(Seq_T memorySegments, Seq_T unmappedSegments) { assert(Seq_length(memorySegments) == Seq_length(unmappedSegments)); for(int i = 0; i < Seq_length(memorySegments); i++){ if(Seq_get(memorySegments, i) == Seq_get(unmappedSegments, i)){ fprintf(stderr, "Cannot be both mapped and unmapped: %d\n", i); /* if(Seq_get(unmappedSegments, i) == NULL) { fprintf(stderr, "Both NULL\n"); }*/ exit(1); } if(Seq_get(memorySegments, i) != NULL) { if(Seq_get(unmappedSegments, i) != NULL){ fprintf(stderr, "Failed to unmap index: %d\n", i); exit(1); } UArray_T temp = Seq_get(memorySegments, i); for(UM_Word j = 0; j < (UM_Word)UArray_length(temp); j++) { UM_Word value = *(UM_Word*)UArray_at(temp, j); if(value != 0){ printf("Segmented load at %d with value of %u\n", i, value); } } } } printf("Verified\n"); }
/* Moves the segment identified by source to the target segment. The source * segment is duplicated and replaces the segment at the target ID. */ void Segment_move(T seg_memory, ID_SIZE source, ID_SIZE target) { WORD_SIZE *src = Seq_get(seg_memory->segments, source); WORD_SIZE size = src[0]; Segment_unmap(seg_memory, target); assert (Segment_map(seg_memory, size) == target); WORD_SIZE *tar = Seq_get(seg_memory->segments, target); memcpy(tar, src, ((size + 1) * sizeof(WORD_SIZE))); }
/* * Input: Mem_T structure, unsigned segment index, unsigned offset * Output: Returns a uint32_t * Purpose: Retrieves a word from memory from at specified index and offset. */ uint32_t load_word(Mem_T memory, unsigned segIndex, unsigned offset) { Seq_T segment = (Seq_T)Seq_get(memory->segment_seq, segIndex); assert(segment != NULL); uint32_t word = (uint32_t)(uintptr_t)Seq_get(segment, offset); return word; }
/* * 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); }
static void free_address_space(UM_machine machine) { UM_address_space address_space = machine->address_space; /* free the mapped segments sequence */ int length = Seq_length(address_space->mapped_segments); for(int i=0; i<length; i++){ UM_segment segment = Seq_remlo(address_space->mapped_segments); /* if segment with ID == i is mapped, free it */ if(segment != NULL){ free_segment_memory(&segment); } } Seq_free(&address_space->mapped_segments); /* free the reusable IDs sequence */ for(int i=0; i<Seq_length(address_space->reusable_IDs); i++){ UM_segment_ID *seg_ID = Seq_get(address_space->reusable_IDs, i); free(seg_ID); } Seq_free(&(address_space->reusable_IDs)); free(address_space); }
void kref(char *name, FILE *fp, Table_T identifiers) { char buf[512], *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); } } }
/* load the val at myMem[rb][rc] */ uint32_t load_segment(Mem myMem, uint32_t rb, uint32_t rc) { assert(myMem != NULL); Segment_T seg = (Segment_T) Seq_get(myMem->segs, rb); return Seg_get(seg, rc); }
/* * Copies and inserts the segment at the given ID into the Segment_Table * Parameters: sm - Segment manager struct * id - ID of segment to copy * Return value: The id of the newly inserted segment */ unsigned copy_Segment(Segment_Manager sm, unsigned id) { Segment original = Seq_get(sm->Segment_Table, id); unsigned length = UArray_length(original); Segment copy = UArray_copy(original, length); return insert_Segment(sm, copy); }
bool put_word(Segment s, uint32_t ID, uint32_t address, uint32_t word) { TRY assert(s); UArray_T inner = Seq_get(s, ID); assert(inner); if (ID >= (unsigned) Seq_length(s)) { /* ID does not exist */ return false; } if (address >= (unsigned) UArray_length(inner)) { /* offset does not exist */ return false; } assert(inner); uint32_t *wordptr = UArray_at(inner, address); *wordptr = word; EXCEPT(Mem_Failed) return false; END_TRY; return true; }
/* * 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); }
/* store the val into myMem[rb][rc] */ void store_segment(Mem myMem, uint32_t ra, uint32_t rb, uint32_t val) { assert(myMem != NULL); Segment_T seg = (Segment_T) Seq_get(myMem->segs, ra); Seg_put(seg, rb, val); return; }
/* Unmaps identified segment from memory, allows unchecked runtime error if * segment doesn't exist. */ void Segment_unmap(T seg_memory, ID_SIZE id) { WORD_SIZE *temp; Seq_addlo(seg_memory->unmapped_ids, (void *)(uintptr_t)id); temp = Seq_get(seg_memory->segments, id); free(temp); Seq_put(seg_memory->segments, id, NULL); }
/* * Input: Mem_T structure, 32 bit word, unsigned segment index and an unsigned * offset * Output: void * Purpose: Puts a word into a segment in memory at index segIndex with an * offset. Used to execute the segmented store instruction and to put * put words into segment 0 during initialization */ void store_word(Mem_T memory, uint32_t word, unsigned segIndex, unsigned offset) { Seq_T segment = (Seq_T)Seq_get(memory->segment_seq, segIndex); assert(segment != NULL); Seq_put(segment, offset, (void *)(uintptr_t)word); Seq_put(memory->segment_seq, segIndex, segment); }
void test_move() { move_seg(SEG_LENGTH, SEG_LENGTH+1); Segment t1 = get_seg(SEG_LENGTH); Segment t2 = get_seg(SEG_LENGTH+1); for (unsigned i = 0; i < SEG_LENGTH; i++) { uint32_t *w = Seq_get(t1->data, i); uint32_t *w2 = Seq_get(t2->data, i); if (w != w2) printf("TEST MOVE IS A FAIL!!!!!\n"); } printf("TEST MOVE IS A FAIL!!!!!\n"); }
__declspec(dllexport) void curl_shim_free_strings(void* p) { int i, count; Seq_T seq = (Seq_T)p; count = Seq_length(seq); for (i = 0; i < count; i++) free(Seq_get(seq, i)); Seq_free(&seq); }
/* * Input: Mem_T structure * Output: void * Purpose: Frees all memory. Frees each segment, the sequence of segments, * the sequence of reusable indices and the pointer to the memory * structure */ void MemT_free(Mem_T memory){ int length = Seq_length(memory->segment_seq); for(int i = 0; i < length; i++){ Seq_T segment = Seq_get(memory->segment_seq, i); if(segment != NULL) Seq_free(&segment); } Seq_free(&(memory->segment_seq)); Seq_free(&(memory->reusable_indices)); free(memory); }
/* stabend - emits the symbol table */ static void stabend(Coordinate *cp, Symbol symroot, Coordinate *cpp[], Symbol sp[], Symbol *ignore) { Symbol addresses; int naddresses, nmodule; { /* annotate top-level symbols */ Symbol p; for (p = symroot; p != NULL; p = up(p->up)) symboluid(p); pickle->globals = symboluid(symroot); } { /* emit addresses of top-level and static symbols */ int i, lc = 0, count = Seq_length(statics); addresses = genident(STATIC, array(voidptype, 1, 0), GLOBAL); comment("addresses:\n"); defglobal(addresses, LIT); for (i = 0; i < count; i++) { Symbol p = Seq_get(statics, i); lc = emit_value(lc, voidptype, p); } lc = pad(maxalign, lc); naddresses = lc; Seq_free(&statics); } { /* emit bp count as an alias for the module */ Symbol spoints = mksymbol(AUTO, stringf("_spoints_V%x_%d", uname, Seq_length(pickle->spoints)), array(unsignedtype, 0, 0)); spoints->generated = 1; defglobal(spoints, LIT); } { /* emit module */ int lc; comment("module:\n"); defglobal(module, LIT); lc = emit_value( 0, unsignedtype, (unsigned long)uname); lc = emit_value(lc, voidptype, addresses); lc = pad(maxalign, lc); nmodule = lc; } Seq_free(&locals); #define printit(x) fprintf(stderr, "%7d " #x "\n", n##x); total += n##x { int total = 0; printit(addresses); printit(module); fprintf(stderr, "%7d bytes total\n", total); } #undef printit { /* complete and write symbol-table pickle */ FILE *f = fopen(stringf("%d.pickle", uname), "wb"); sym_write_module(pickle, f); fclose(f); } }
/* free the mem */ void free_mem(Mem* myMem) { assert(myMem != NULL); for (int i = 0; i < Seq_length((*myMem)->segs); i++) { Seg_free(Seq_get((*myMem)->segs, i)); } Seq_free(&((*myMem)->segs)); Stack_free(&((*myMem)->unmapped)); free(*myMem); }
/* * Sets the value of segment[id][offset] to the given value * Parameters: sm - Segment manager struct * id - ID of segment to set * offset - Offset within the segment * value - Value to set * Return value: the old value of the specified segment[id][offset] */ uint32_t seg_Set(Segment_Manager sm, unsigned id, unsigned offset, uint32_t value) { uint32_t old_value; Segment segment = Seq_get(sm->Segment_Table, id); uint32_t *location = UArray_at(segment, offset); old_value = *location; *location = value; return old_value; }
/* Frees all segmented memory created by the program, including the segment * where the program is stored. */ void Segment_free(T * seg_memory) { int len = Seq_length((*seg_memory)->segments); int i; for (i = 0; i < len; i++) { free(Seq_get((*seg_memory)->segments, i)); } Seq_free(&((*seg_memory)->segments)); Seq_free(&((*seg_memory)->unmapped_ids)); free (*seg_memory); }
void Segmem_free(T segmem) { int i; for(i = 0; i < segmem->length; i++) { int *array = (int *)Seq_get(segmem->memory, i); //printf("%p\n", (void *)(array)); free(array); } Seq_free(&(segmem->memory)); }
static int checkuid(list_ty list) { int i, n = 0, count = Seq_length(list); for (i = 0; i < count; i++) { rcc_interface_ty in = Seq_get(list, i); if (in->kind == rcc_Local_enum || in->kind == rcc_Address_enum) n++; else if (in->kind == rcc_Function_enum) n += checkuid(in->v.rcc_Function.codelist); } return n; }
UArray_T get_seg(Segment s, uint32_t ID) { assert(s); if (ID > (uint32_t) Seq_length(s)) { /* segment does not exist */ return NULL; } UArray_T inner = Seq_get(s, ID); return inner; }
/* frees all of the segments and their contents */ void free_all(Segment outer) { assert(outer); int num_segs = Seq_length(outer); for (int i = 0; i < num_segs; i++) { UArray_T inner = Seq_get(outer, i); UArray_free(&inner); } Seq_free(&outer); }
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); }
/* returns the word stored at index address in the segment with id ID */ uint32_t get_word(Segment s, uint32_t ID, uint32_t address) { assert(s); if(ID != 0 ){ assert((signed) ID < get_num_segs(s)); } UArray_T inner = Seq_get(s, ID); assert(inner); uint32_t *wordptr = UArray_at(inner, address); return *wordptr; }
extern void Um_write_sequence(FILE *output, Seq_T stream) { // use w+ to open and clear the file FILE *hexOut = fopen("hex.out", "a"); fprintf(hexOut, "STARTING NEW TEST SEQUENCE \n\n\n"); int i, j, instrByte; int len = Seq_length(stream); for (i = 0; i < len; i++) { Um_instruction instr = (uint32_t)(uintptr_t)Seq_get(stream, i); for (j = 24; j >= 0; j -= BITS_PER_BYTE) { instrByte = (int)Bitpack_getu((uint64_t)instr, BITS_PER_BYTE, j); fputc(instrByte, output); fprintf(hexOut, "%02x ", instrByte); } fprintf(hexOut, "\n"); } }