double CMVPeriod::evaluate(CMTimeMachine* t,int,int) { CMTime evaltime( (t->Cycles() > traceno) ? t->At(traceno) : t->At(0)); if (periodformat == CMTime::MM) evaltime.SetTime(2000); int index = get_array_index(evaltime); if (periodformat == CMTime::MM) array[index]->Reset(); return array[index]->Evaluate(t,periodstate&forceEvaluation); }
int archive_write_set_format_filter_by_ext_def(struct archive *a, const char *filename, const char * def_ext) { int names_index = get_array_index(filename); if (names_index < 0) names_index = get_array_index(def_ext); if (names_index >= 0) { int format_state = (names[names_index].format)(a); if (format_state == ARCHIVE_OK) return ((names[names_index].filter)(a)); else return format_state; } archive_set_error(a, EINVAL, "No such format '%s'", filename); a->state = ARCHIVE_STATE_FATAL; return (ARCHIVE_FATAL); }
static int interpret_morse(linked_list_t **list_arr, char c, char *morse) { /* PRE: The given string contains only . and _ characters, no spaces. The given pointers are valid and legal. */ /* POSE: Interprets the given morse string and adds to the list of the given letter in the given array. */ int index = get_array_index(c); if (index == -1) return EXIT_FAILURE; linked_list_t *list = list_arr[index]; for (; *morse != '\0'; morse++) { /* For each char in the string: */ insert_list(list, *morse == '.' ? MORSE_DOT : MORSE_DASH); } return EXIT_SUCCESS; }
void gfc_assign_data_value_range (gfc_expr * lvalue, gfc_expr * rvalue, mpz_t index, mpz_t repeat) { gfc_ref *ref; gfc_expr *init, *expr; gfc_constructor *con, *last_con; gfc_symbol *symbol; gfc_typespec *last_ts; mpz_t offset; symbol = lvalue->symtree->n.sym; init = symbol->value; last_ts = &symbol->ts; last_con = NULL; mpz_init_set_si (offset, 0); /* Find/create the parent expressions for subobject references. */ for (ref = lvalue->ref; ref; ref = ref->next) { /* Use the existing initializer expression if it exists. Otherwise create a new one. */ if (init == NULL) expr = gfc_get_expr (); else expr = init; /* Find or create this element. */ switch (ref->type) { case REF_ARRAY: if (init == NULL) { /* The element typespec will be the same as the array typespec. */ expr->ts = *last_ts; /* Setup the expression to hold the constructor. */ expr->expr_type = EXPR_ARRAY; expr->rank = ref->u.ar.as->rank; } else gcc_assert (expr->expr_type == EXPR_ARRAY); if (ref->u.ar.type == AR_ELEMENT) { get_array_index (&ref->u.ar, &offset); /* This had better not be the bottom of the reference. We can still get to a full array via a component. */ gcc_assert (ref->next != NULL); } else { mpz_set (offset, index); /* We're at a full array or an array section. This means that we've better have found a full array, and that we're at the bottom of the reference. */ gcc_assert (ref->u.ar.type == AR_FULL); gcc_assert (ref->next == NULL); } /* Find the same element in the existing constructor. */ con = expr->value.constructor; con = find_con_by_offset (offset, con); /* Create a new constructor. */ if (con == NULL) { con = gfc_get_constructor (); mpz_set (con->n.offset, offset); if (ref->next == NULL) mpz_set (con->repeat, repeat); gfc_insert_constructor (expr, con); } else gcc_assert (ref->next != NULL); break; case REF_COMPONENT: if (init == NULL) { /* Setup the expression to hold the constructor. */ expr->expr_type = EXPR_STRUCTURE; expr->ts.type = BT_DERIVED; expr->ts.derived = ref->u.c.sym; } else gcc_assert (expr->expr_type == EXPR_STRUCTURE); last_ts = &ref->u.c.component->ts; /* Find the same element in the existing constructor. */ con = expr->value.constructor; con = find_con_by_component (ref->u.c.component, con); if (con == NULL) { /* Create a new constructor. */ con = gfc_get_constructor (); con->n.component = ref->u.c.component; con->next = expr->value.constructor; expr->value.constructor = con; } /* Since we're only intending to initialize arrays here, there better be an inner reference. */ gcc_assert (ref->next != NULL); break; case REF_SUBSTRING: default: gcc_unreachable (); } if (init == NULL) { /* Point the container at the new expression. */ if (last_con == NULL) symbol->value = expr; else last_con->expr = expr; } init = con->expr; last_con = con; } if (last_ts->type == BT_CHARACTER) expr = create_character_intializer (init, last_ts, NULL, rvalue); else { /* We should never be overwriting an existing initializer. */ gcc_assert (!init); expr = gfc_copy_expr (rvalue); if (!gfc_compare_types (&lvalue->ts, &expr->ts)) gfc_convert_type (expr, &lvalue->ts, 0); } if (last_con == NULL) symbol->value = expr; else last_con->expr = expr; }
void gfc_assign_data_value (gfc_expr * lvalue, gfc_expr * rvalue, mpz_t index) { gfc_ref *ref; gfc_expr *init; gfc_expr *expr; gfc_constructor *con; gfc_constructor *last_con; gfc_symbol *symbol; gfc_typespec *last_ts; mpz_t offset; symbol = lvalue->symtree->n.sym; init = symbol->value; last_ts = &symbol->ts; last_con = NULL; mpz_init_set_si (offset, 0); /* Find/create the parent expressions for subobject references. */ for (ref = lvalue->ref; ref; ref = ref->next) { /* Break out of the loop if we find a substring. */ if (ref->type == REF_SUBSTRING) { /* A substring should always be the last subobject reference. */ gcc_assert (ref->next == NULL); break; } /* Use the existing initializer expression if it exists. Otherwise create a new one. */ if (init == NULL) expr = gfc_get_expr (); else expr = init; /* Find or create this element. */ switch (ref->type) { case REF_ARRAY: if (init == NULL) { /* The element typespec will be the same as the array typespec. */ expr->ts = *last_ts; /* Setup the expression to hold the constructor. */ expr->expr_type = EXPR_ARRAY; expr->rank = ref->u.ar.as->rank; } else gcc_assert (expr->expr_type == EXPR_ARRAY); if (ref->u.ar.type == AR_ELEMENT) get_array_index (&ref->u.ar, &offset); else mpz_set (offset, index); /* Find the same element in the existing constructor. */ con = expr->value.constructor; con = find_con_by_offset (offset, con); if (con == NULL) { /* Create a new constructor. */ con = gfc_get_constructor (); mpz_set (con->n.offset, offset); gfc_insert_constructor (expr, con); } break; case REF_COMPONENT: if (init == NULL) { /* Setup the expression to hold the constructor. */ expr->expr_type = EXPR_STRUCTURE; expr->ts.type = BT_DERIVED; expr->ts.derived = ref->u.c.sym; } else gcc_assert (expr->expr_type == EXPR_STRUCTURE); last_ts = &ref->u.c.component->ts; /* Find the same element in the existing constructor. */ con = expr->value.constructor; con = find_con_by_component (ref->u.c.component, con); if (con == NULL) { /* Create a new constructor. */ con = gfc_get_constructor (); con->n.component = ref->u.c.component; con->next = expr->value.constructor; expr->value.constructor = con; } break; default: gcc_unreachable (); } if (init == NULL) { /* Point the container at the new expression. */ if (last_con == NULL) symbol->value = expr; else last_con->expr = expr; } init = con->expr; last_con = con; } if (ref || last_ts->type == BT_CHARACTER) expr = create_character_intializer (init, last_ts, ref, rvalue); else { /* Overwriting an existing initializer is non-standard but usually only provokes a warning from other compilers. */ if (init != NULL) { /* Order in which the expressions arrive here depends on whether they are from data statements or F95 style declarations. Therefore, check which is the most recent. */ #ifdef USE_MAPPED_LOCATION expr = (LOCATION_LINE (init->where.lb->location) > LOCATION_LINE (rvalue->where.lb->location)) ? init : rvalue; #else expr = (init->where.lb->linenum > rvalue->where.lb->linenum) ? init : rvalue; #endif gfc_notify_std (GFC_STD_GNU, "Extension: re-initialization " "of '%s' at %L", symbol->name, &expr->where); } expr = gfc_copy_expr (rvalue); if (!gfc_compare_types (&lvalue->ts, &expr->ts)) gfc_convert_type (expr, &lvalue->ts, 0); } if (last_con == NULL) symbol->value = expr; else last_con->expr = expr; }
bool gfc_assign_data_value (gfc_expr *lvalue, gfc_expr *rvalue, mpz_t index, mpz_t *repeat) { gfc_ref *ref; gfc_expr *init; gfc_expr *expr = NULL; gfc_constructor *con; gfc_constructor *last_con; gfc_symbol *symbol; gfc_typespec *last_ts; mpz_t offset; symbol = lvalue->symtree->n.sym; init = symbol->value; last_ts = &symbol->ts; last_con = NULL; mpz_init_set_si (offset, 0); /* Find/create the parent expressions for subobject references. */ for (ref = lvalue->ref; ref; ref = ref->next) { /* Break out of the loop if we find a substring. */ if (ref->type == REF_SUBSTRING) { /* A substring should always be the last subobject reference. */ gcc_assert (ref->next == NULL); break; } /* Use the existing initializer expression if it exists. Otherwise create a new one. */ if (init == NULL) expr = gfc_get_expr (); else expr = init; /* Find or create this element. */ switch (ref->type) { case REF_ARRAY: if (ref->u.ar.as->rank == 0) { gcc_assert (ref->u.ar.as->corank > 0); if (init == NULL) free (expr); continue; } if (init && expr->expr_type != EXPR_ARRAY) { gfc_error ("%qs at %L already is initialized at %L", lvalue->symtree->n.sym->name, &lvalue->where, &init->where); goto abort; } if (init == NULL) { /* The element typespec will be the same as the array typespec. */ expr->ts = *last_ts; /* Setup the expression to hold the constructor. */ expr->expr_type = EXPR_ARRAY; expr->rank = ref->u.ar.as->rank; } if (ref->u.ar.type == AR_ELEMENT) get_array_index (&ref->u.ar, &offset); else mpz_set (offset, index); /* Check the bounds. */ if (mpz_cmp_si (offset, 0) < 0) { gfc_error ("Data element below array lower bound at %L", &lvalue->where); goto abort; } else if (repeat != NULL && ref->u.ar.type != AR_ELEMENT) { mpz_t size, end; gcc_assert (ref->u.ar.type == AR_FULL && ref->next == NULL); mpz_init_set (end, offset); mpz_add (end, end, *repeat); if (spec_size (ref->u.ar.as, &size)) { if (mpz_cmp (end, size) > 0) { mpz_clear (size); gfc_error ("Data element above array upper bound at %L", &lvalue->where); goto abort; } mpz_clear (size); } con = gfc_constructor_lookup (expr->value.constructor, mpz_get_si (offset)); if (!con) { con = gfc_constructor_lookup_next (expr->value.constructor, mpz_get_si (offset)); if (con != NULL && mpz_cmp (con->offset, end) >= 0) con = NULL; } /* Overwriting an existing initializer is non-standard but usually only provokes a warning from other compilers. */ if (con != NULL && con->expr != NULL) { /* Order in which the expressions arrive here depends on whether they are from data statements or F95 style declarations. Therefore, check which is the most recent. */ gfc_expr *exprd; exprd = (LOCATION_LINE (con->expr->where.lb->location) > LOCATION_LINE (rvalue->where.lb->location)) ? con->expr : rvalue; if (gfc_notify_std (GFC_STD_GNU, "re-initialization of %qs at %L", symbol->name, &exprd->where) == false) return false; } while (con != NULL) { gfc_constructor *next_con = gfc_constructor_next (con); if (mpz_cmp (con->offset, end) >= 0) break; if (mpz_cmp (con->offset, offset) < 0) { gcc_assert (mpz_cmp_si (con->repeat, 1) > 0); mpz_sub (con->repeat, offset, con->offset); } else if (mpz_cmp_si (con->repeat, 1) > 0 && mpz_get_si (con->offset) + mpz_get_si (con->repeat) > mpz_get_si (end)) { int endi; splay_tree_node node = splay_tree_lookup (con->base, mpz_get_si (con->offset)); gcc_assert (node && con == (gfc_constructor *) node->value && node->key == (splay_tree_key) mpz_get_si (con->offset)); endi = mpz_get_si (con->offset) + mpz_get_si (con->repeat); if (endi > mpz_get_si (end) + 1) mpz_set_si (con->repeat, endi - mpz_get_si (end)); else mpz_set_si (con->repeat, 1); mpz_set (con->offset, end); node->key = (splay_tree_key) mpz_get_si (end); break; } else gfc_constructor_remove (con); con = next_con; } con = gfc_constructor_insert_expr (&expr->value.constructor, NULL, &rvalue->where, mpz_get_si (offset)); mpz_set (con->repeat, *repeat); repeat = NULL; mpz_clear (end); break; } else { mpz_t size; if (spec_size (ref->u.ar.as, &size)) { if (mpz_cmp (offset, size) >= 0) { mpz_clear (size); gfc_error ("Data element above array upper bound at %L", &lvalue->where); goto abort; } mpz_clear (size); } } con = gfc_constructor_lookup (expr->value.constructor, mpz_get_si (offset)); if (!con) { con = gfc_constructor_insert_expr (&expr->value.constructor, NULL, &rvalue->where, mpz_get_si (offset)); } else if (mpz_cmp_si (con->repeat, 1) > 0) { /* Need to split a range. */ if (mpz_cmp (con->offset, offset) < 0) { gfc_constructor *pred_con = con; con = gfc_constructor_insert_expr (&expr->value.constructor, NULL, &con->where, mpz_get_si (offset)); con->expr = gfc_copy_expr (pred_con->expr); mpz_add (con->repeat, pred_con->offset, pred_con->repeat); mpz_sub (con->repeat, con->repeat, offset); mpz_sub (pred_con->repeat, offset, pred_con->offset); } if (mpz_cmp_si (con->repeat, 1) > 0) { gfc_constructor *succ_con; succ_con = gfc_constructor_insert_expr (&expr->value.constructor, NULL, &con->where, mpz_get_si (offset) + 1); succ_con->expr = gfc_copy_expr (con->expr); mpz_sub_ui (succ_con->repeat, con->repeat, 1); mpz_set_si (con->repeat, 1); } } break; case REF_COMPONENT: if (init == NULL) { /* Setup the expression to hold the constructor. */ expr->expr_type = EXPR_STRUCTURE; expr->ts.type = BT_DERIVED; expr->ts.u.derived = ref->u.c.sym; } else gcc_assert (expr->expr_type == EXPR_STRUCTURE); last_ts = &ref->u.c.component->ts; /* Find the same element in the existing constructor. */ con = find_con_by_component (ref->u.c.component, expr->value.constructor); if (con == NULL) { /* Create a new constructor. */ con = gfc_constructor_append_expr (&expr->value.constructor, NULL, NULL); con->n.component = ref->u.c.component; } break; default: gcc_unreachable (); } if (init == NULL) { /* Point the container at the new expression. */ if (last_con == NULL) symbol->value = expr; else last_con->expr = expr; } init = con->expr; last_con = con; } mpz_clear (offset); gcc_assert (repeat == NULL); if (ref || last_ts->type == BT_CHARACTER) { if (lvalue->ts.u.cl->length == NULL && !(ref && ref->u.ss.length != NULL)) return false; expr = create_character_initializer (init, last_ts, ref, rvalue); } else { /* Overwriting an existing initializer is non-standard but usually only provokes a warning from other compilers. */ if (init != NULL) { /* Order in which the expressions arrive here depends on whether they are from data statements or F95 style declarations. Therefore, check which is the most recent. */ expr = (LOCATION_LINE (init->where.lb->location) > LOCATION_LINE (rvalue->where.lb->location)) ? init : rvalue; if (gfc_notify_std (GFC_STD_GNU, "re-initialization of %qs at %L", symbol->name, &expr->where) == false) return false; } expr = gfc_copy_expr (rvalue); if (!gfc_compare_types (&lvalue->ts, &expr->ts)) gfc_convert_type (expr, &lvalue->ts, 0); } if (last_con == NULL) symbol->value = expr; else last_con->expr = expr; return true; abort: if (!init) gfc_free_expr (expr); mpz_clear (offset); return false; }
int main(int argc, char *argv[]) { int N, R; int i, j; scanf("%d %d", &N, &R); for (i=0; i<N; i++) { scanf("%s%d%d%d", array[i].name, &array[i].base_address, &array[i].element_size, &array[i].D); array[i].constants[0] = 0; for (j=0; j < array[i].D; j++) { scanf("%d%d", &array[i].dimension[j].lower_bound, &array[i].dimension[j].upper_bound); array[i].constants[j+1] = 0; } } calculate_constants(N); for(i=0; i<R; i++) { char reference_array[20]; int target_dimensions[20]; int array_num; scanf("%s", reference_array); array_num = get_array_index(reference_array, N); if(array_num == -1) continue; int reference_dimensions = array[array_num].D; for(j=0; j<reference_dimensions; j++) { scanf("%d", &target_dimensions[j]); } int array_index = array[array_num].constants[0]; printf("%s", array[array_num].name); printf("["); for(j=0; j<reference_dimensions; j++) { array_index = array_index + array[array_num].constants[j+1] * target_dimensions[j]; printf("%d", target_dimensions[j]); if(j != (reference_dimensions - 1)) { printf(", "); } } printf("]"); printf(" = %d\n", array_index); } return 0; }
gfc_try gfc_assign_data_value (gfc_expr *lvalue, gfc_expr *rvalue, mpz_t index) { gfc_ref *ref; gfc_expr *init; gfc_expr *expr; gfc_constructor *con; gfc_constructor *last_con; gfc_constructor *pred; gfc_symbol *symbol; gfc_typespec *last_ts; mpz_t offset; splay_tree spt; splay_tree_node sptn; symbol = lvalue->symtree->n.sym; init = symbol->value; last_ts = &symbol->ts; last_con = NULL; mpz_init_set_si (offset, 0); /* Find/create the parent expressions for subobject references. */ for (ref = lvalue->ref; ref; ref = ref->next) { /* Break out of the loop if we find a substring. */ if (ref->type == REF_SUBSTRING) { /* A substring should always be the last subobject reference. */ gcc_assert (ref->next == NULL); break; } /* Use the existing initializer expression if it exists. Otherwise create a new one. */ if (init == NULL) expr = gfc_get_expr (); else expr = init; /* Find or create this element. */ switch (ref->type) { case REF_ARRAY: if (init && expr->expr_type != EXPR_ARRAY) { gfc_error ("'%s' at %L already is initialized at %L", lvalue->symtree->n.sym->name, &lvalue->where, &init->where); return FAILURE; } if (init == NULL) { /* The element typespec will be the same as the array typespec. */ expr->ts = *last_ts; /* Setup the expression to hold the constructor. */ expr->expr_type = EXPR_ARRAY; expr->rank = ref->u.ar.as->rank; } if (ref->u.ar.type == AR_ELEMENT) get_array_index (&ref->u.ar, &offset); else mpz_set (offset, index); /* Check the bounds. */ if (mpz_cmp_si (offset, 0) < 0) { gfc_error ("Data element below array lower bound at %L", &lvalue->where); return FAILURE; } else { mpz_t size; if (spec_size (ref->u.ar.as, &size) == SUCCESS) { if (mpz_cmp (offset, size) >= 0) { mpz_clear (size); gfc_error ("Data element above array upper bound at %L", &lvalue->where); return FAILURE; } mpz_clear (size); } } /* Splay tree containing offset and gfc_constructor. */ spt = expr->con_by_offset; if (spt == NULL) { spt = splay_tree_new (splay_tree_compare_ints, NULL, NULL); expr->con_by_offset = spt; con = NULL; } else con = find_con_by_offset (spt, offset); if (con == NULL) { splay_tree_key j; /* Create a new constructor. */ con = gfc_get_constructor (); mpz_set (con->n.offset, offset); j = (splay_tree_key) mpz_get_si (offset); sptn = splay_tree_insert (spt, j, (splay_tree_value) con); /* Fix up the linked list. */ sptn = splay_tree_predecessor (spt, j); if (sptn == NULL) { /* Insert at the head. */ con->next = expr->value.constructor; expr->value.constructor = con; } else { /* Insert in the chain. */ pred = (gfc_constructor*) sptn->value; con->next = pred->next; pred->next = con; } } break; case REF_COMPONENT: if (init == NULL) { /* Setup the expression to hold the constructor. */ expr->expr_type = EXPR_STRUCTURE; expr->ts.type = BT_DERIVED; expr->ts.derived = ref->u.c.sym; } else gcc_assert (expr->expr_type == EXPR_STRUCTURE); last_ts = &ref->u.c.component->ts; /* Find the same element in the existing constructor. */ con = expr->value.constructor; con = find_con_by_component (ref->u.c.component, con); if (con == NULL) { /* Create a new constructor. */ con = gfc_get_constructor (); con->n.component = ref->u.c.component; con->next = expr->value.constructor; expr->value.constructor = con; } break; default: gcc_unreachable (); } if (init == NULL) { /* Point the container at the new expression. */ if (last_con == NULL) symbol->value = expr; else last_con->expr = expr; } init = con->expr; last_con = con; } if (ref || last_ts->type == BT_CHARACTER) expr = create_character_intializer (init, last_ts, ref, rvalue); else { /* Overwriting an existing initializer is non-standard but usually only provokes a warning from other compilers. */ if (init != NULL) { /* Order in which the expressions arrive here depends on whether they are from data statements or F95 style declarations. Therefore, check which is the most recent. */ expr = (LOCATION_LINE (init->where.lb->location) > LOCATION_LINE (rvalue->where.lb->location)) ? init : rvalue; gfc_notify_std (GFC_STD_GNU, "Extension: re-initialization " "of '%s' at %L", symbol->name, &expr->where); } expr = gfc_copy_expr (rvalue); if (!gfc_compare_types (&lvalue->ts, &expr->ts)) gfc_convert_type (expr, &lvalue->ts, 0); } if (last_con == NULL) symbol->value = expr; else last_con->expr = expr; return SUCCESS; }
void main() { unsigned long* array = NULL; int set = 0, line = 0, k = 0; int start = 0, end = 0; struct timeval before, after; int bad = 0, try = 0, i = 0, tmp = 0; int bit = 0; float miss_time_all, miss_time, hit_time_all, hit_time, miss_hit_ratio, miss_hit_diff; uint32_t buffer = 0; /* Test: unsigned long must be 64bit*/ if( sizeof(unsigned long) != 8) { dfprintf(stderr, "Error: unsigned long must be 8 Byte for the program to work\n"); exit(1); } /* init array and load L1 data cache*/ do { array = malloc(sizeof(unsigned long) * ARRAY_LENGTH); }while( array == NULL && try++ <= 10); if( array == NULL) { dfprintf(stderr, "Error: cannot allocate array size:%d\n", ARRAY_LENGTH); exit(2); } /* Allign the start of the array to be the start of a cache line*/ if( ( (unsigned long)array & 0x038UL ) != 0) /*Start of array is not start of a cache line*/ { start = (0x08 - (( (unsigned long)array & 0x038UL ) >> 0x3)); }else{ start = 0; } dprintf("Allign the array with the cache line: start element's index is:%d\n", start); /* Initialize the array, load all L1 data cache, first round is cache miss, second round is cache hit, get ratio of cache miss/cach hit*/ for(i = 0; i < 2; i++) { for(set = 0; set < SET_NUM; set+=2) { bad = gettimeofday(&before, NULL); assert( !bad ); for( line = 0; line < LINE_NUM; line++) { for( k = get_array_index(set, line); k < get_array_index(set, line) + ELMT_PER_LINE; k++) { if(i == 0) array[k] = 0xFFFFFFFFFFFFFFFF; else tmp = array[k]; } } bad = gettimeofday(&after, NULL); assert( !bad ); if( i == 0 ) miss_time_all += get_elapsed_usec(before, after); else hit_time_all += get_elapsed_usec(before, after); } if( i == 0 ) { miss_time = miss_time_all / (SET_NUM / 2); } else { hit_time = hit_time_all / (SET_NUM / 2); } } dprintf("Initialize the L1D array: Each set(miss) takes:%.2fus; All array(miss) takes %.2fus in total\n", miss_time, miss_time_all); dprintf("Reload the L1D array: Each set(hit) takes:%.2fus; All array(hit) takes %.2f in total\n", hit_time, hit_time_all); miss_hit_ratio = miss_time * 1.0 / hit_time; miss_hit_diff = miss_time - hit_time; dprintf("Ratio of miss to hit per line is %.2f\n", miss_hit_ratio); sleep(2); /* Start the covert channel*/ dprintf("Start receiving the data\n"); buffer = 0; for(set = 0; set < SET_NUM; set += 2) /* Avoid prefetch effect*/ { bit = 0; bad = gettimeofday(&before, NULL); assert( !bad ); for( line = 0; line < LINE_NUM; line++) { for( k = get_array_index(set, line); k < get_array_index(set, line) + ELMT_PER_LINE; k++) { tmp = array[k]; } } bad = gettimeofday(&after, NULL); assert( !bad ); //if(get_elapsed_usec(before, after) / hit_time >= (miss_hit_ratio*3.0/5)) /*miss means the bit is 1*/ if(get_elapsed_usec(before, after) - hit_time >= (miss_hit_diff * 0.5) ) { bit = 1; } dprintf("set %d (bit %d): latency is %ld, miss_hit_diff is %.2f, bit received is: %d\n", set, set/2, get_elapsed_usec(before, after), get_elapsed_usec(before, after) - hit_time, bit); buffer |= (bit << set); //printf("Reload time is %ld, hit_time is %.2f, miss_hit_ratio is %.2f\n", get_elapsed_usec(before, after), hit_time, get_elapsed_usec(before, after) / hit_time); /* if( set%8 == 0 ) printf(" "); printf("%d", bit); */ } /* printf("\n"); */ printf("Receive 32bit: %#010x\n", buffer); free(array); }
void GLEPolish::internalPolish(GLEPcode& pcode, int *rtype) throw(ParserError) { GLESub* sub; string uc_token; int idx, ret, np, *plist, term_bracket = false; int curpri = 0; int nstk = 0, stk[50], stkp[50]; /* stack for operators */ int unary = 1; /* binary or unary operation expected */ bool isa_string = false; bool not_string = false; if (*rtype==1) not_string = true; if (*rtype>0) term_bracket = true; pcode.addInt(PCODE_EXPR); /* Expression follows */ int savelen = pcode.size(); /* Used to set acutal length at end */ pcode.addInt(0); /* Length of expression */ while (true) { string token = m_tokens.try_next_token(); int token_col = m_tokens.token_pos_col(); int token_len = token.length(); char first_char = token_len > 0 ? token[0] : ' '; // cout << "Token: '" << token << "'" << endl; // end of stream, or found ',' or ')' if (token_len == 0 || (token_len == 1 && (first_char == ',' || (first_char == ')' && curpri == 0) || (first_char == ']' && curpri == 0)))) { if (token_len != 0) { m_tokens.pushback_token(); } *rtype = 0; dbg gprint("Found END OF EXPRESSION \n"); if (curpri != 0) { throw error("unexpected end of expression, missing closing ')' or ']'"); } /* Pop everything off the stack */ for (int i = nstk; i > 0; i--) { dbg gprint("Adding left over operators I = %d op=%d \n",i,stk[i]); pcode.addInt(stk[i]); } if (unary == 1) { throw error("constant, function, or unary operator expected"); } pcode.setInt(savelen, pcode.size() - savelen - 1); #ifdef DEBUG_POLISH pcode.show(savelen); #endif return; } dbg gprint("First word token via (1=unary %d) cts {%s}\n ", unary, token.c_str()); switch (unary) { case 1: /* a unary operator, or function, or number or variable */ if (is_float(token)) { dbg gprint("Found number {%s}\n",token.c_str()); double value = atof(token.c_str()); pcode.addDouble(value); unary = 2; break; } str_to_uppercase(token, uc_token); /* NOT a number, is it a built in function? */ find_un((char*)uc_token.c_str(), &idx, &ret, &np, &plist); /* 1,2 = +,- */ if (idx > 3 && m_tokens.is_next_token("(")) { // // it is a built in function // dbg gprint("Found built in function \n"); get_params(pcode, np, plist, uc_token); pcode.addFunction(idx + FN_BUILTIN_MAGIC); unary = 2; break; } else if (idx > 0 && idx <= 3) { stack_fn(idx); unary = 1; break; } /* Is it a user-defined function, identical code too above. */ sub = sub_find((char*)uc_token.c_str()); if (sub != NULL && m_tokens.is_next_token("(")) { // // it is a user defined function // // printf("User cts=%s idx=%d ret=%d np=%d plist=%d\n",cts,idx,ret,np,plist); dbg gprint("Found user function \n"); get_params(pcode, sub->getNbParam(), sub->getParamTypes(), uc_token); pcode.addFunction(sub->getIndex()+LOCAL_START_INDEX); unary = 2; break; } /* Is it a 'known' variable */ int v; var_find((char*)uc_token.c_str(), &v, &ret); if (v >= 0) { // cout << "found var: '" << uc_token << "' -> " << v << endl; if (ret == 2) pcode.addStrVar(v); else pcode.addVar(v); unary = 2; if (m_vars != NULL && m_vars->try_get(uc_token) == -1) { /* Add it to list of vars */ m_vars->add_item(uc_token, v); } break; } /* Is it a string */ if (first_char == '"' || first_char == '\'') { dbg gprint("Found string \n"); string str_no_quote = token; str_remove_quote(str_no_quote); pcode.addString(str_no_quote); unary = 2; break; } if ((first_char == 'd' || first_char == 'D') && token_len == 1 && m_tokens.is_next_token("[")) { get_array_index(pcode); pcode.addFunction(FN_DI + FN_BUILTIN_MAGIC); unary = 2; break; } if (first_char == '(' && token_len == 1) { curpri = curpri + 100; break; } if ((first_char == ')' || first_char == ')') && token_len == 1) { throw error("constant, function, or unary operator expected"); } if (m_tokens.is_next_token("(")) { throw error(token_col, string("call to undefined function '"+token+"'")); } /* must be unquoted string, unless a binary operator was found, in which case it is an undelcared variable */ if (not_string || str_var(token)) { /* name that includes '$' is also assumed to be a variable */ dbg gprint("Found un-initialized variable {%s} /n",token.c_str()); if (!var_valid_name(uc_token)) { throw error(token_col, "illegal variable name '"+uc_token+"'"); } var_findadd((char*)uc_token.c_str(), &v, &ret); if (ret == 2) pcode.addStrVar(v); else pcode.addVar(v); not_string = true; unary = 2; if (m_vars != NULL && m_vars->try_get(uc_token) == -1) { /* Add it to list of vars */ m_vars->add_item(uc_token, v); } break; } // std::cout << "Unquoted string '" << token << "'" << std::endl; pcode.addString(token); if (!valid_unquoted_string(token)) { throw error(token_col, "invalid unquoted string '"+token+"'"); } isa_string = true; unary = 2; break; case 2: /* a binary operator, or space, or end of line */ /* MIGHT (gives error with a$ = b$+c$) */ if (first_char != '.') { if (isa_string) { throw error("left hand side contains unquoted string"); } not_string = true; } else { not_string = false; } /* Binary operators, +,-,*,/,^,<,>,<=,>=,.and.,.or. */ int priority = 0; if (token_len == 1) { switch (first_char) { case '+' : v = BIN_OP_PLUS; priority = 2; break; case '-' : v = BIN_OP_MINUS; priority = 2; break; case '*' : v = BIN_OP_MULTIPLY; priority = 3; break; case '/' : v = BIN_OP_DIVIDE; priority = 3; break; case '%' : v = BIN_OP_MOD; priority = 3; break; case '^' : v = BIN_OP_POW; priority = 4; break; case '=' : v = BIN_OP_EQUALS; priority = 1; break; case '&' : v = BIN_OP_AND; priority = 1; break; case '|' : v = BIN_OP_OR; priority = 1; break; case '<' : v = BIN_OP_LT; priority = 1; break; case '>' : v = BIN_OP_GT; priority = 1; break; case '.' : v = BIN_OP_DOT; priority = 2; break; default : v = 0; } } else { str_to_uppercase(token, uc_token); if (token == "<=") { v = BIN_OP_LE; priority = 1; } else if (token == "<>") { v = BIN_OP_NOT_EQUALS; priority = 1; } else if (token == ">=") { v = BIN_OP_GE; priority = 1; } else if (token == "**") { v = BIN_OP_POW; priority = 4; } else if (uc_token == "AND") { v = BIN_OP_AND; priority = 1; } else if (uc_token == "OR") { v = BIN_OP_OR; priority = 1; } else { v = 0; } } if (v > 0) { stack_bin(v, priority); dbg gprint("Found binary operator \n"); unary = 1; } else if (first_char == ')' && token_len == 1) { if (curpri > 0) { curpri = curpri - 100; unary = 2; break; } if (!term_bracket) { throw error("too many closing ')', expecting binary operator"); } } else { throw error(string("unknown binary operator '")+token+"'"); } } // end switch } // end for }
int main(int argc, char **argv) { /* Must be called: ./main "Hello World" output_file.s */ if (argc != 3) { printf("* Must supply a string to convert and an output file!\n"); return EXIT_FAILURE; } char *str = argv[1]; char *destination = argv[2]; int len = strlen(str); /* Array of pointers to linked lists */ linked_list_t **list_arr = malloc(sizeof(linked_list_t*) * NUM_TOTAL_CHARS); if (init_list_arr(list_arr)) { /* Couldn't initialise list_arr */ printf("* Couldn't initialise the array of lists. Stopping.\n"); return EXIT_FAILURE; } /* list_arr initialised and allocated on heap: now fill with morse data: */ if (populate_list_arr(list_arr)) { /* Couldn't populate list_arr with alphabet morse data */ printf("* Couldn't populate table with morse data. Stopping.\n"); return EXIT_FAILURE; } output_buffer_t *buffer = malloc(sizeof(output_buffer_t)); if (init_buffer(buffer, len)) { /* Error initialising the buffer */ printf("* Couldn't initialise output buffer. Stopping.\n"); return EXIT_FAILURE; } for (int i = 0; i < len; i++) { char c = str[i]; if (isspace(c)) continue; int index = get_array_index(c); if (index == -1) { printf("* Encountered non-valid character. Stopping.\n"); return EXIT_FAILURE; } linked_list_t *parts_list = list_arr[index]; for (linked_list_elem_t *list_elem = parts_list->header->next; list_elem != NULL; list_elem = list_elem->next) { add_to_buffer(buffer, list_elem->type); if (list_elem->next != NULL) add_to_buffer(buffer, MORSE_END_OF_ELEM); } if (i + 1 < len && isspace(str[i + 1])) { /* Do stuff for end of word */ add_to_buffer(buffer, MORSE_END_OF_WORD); } else { /* Do stuff for end of char */ add_to_buffer(buffer, MORSE_END_OF_CHAR); } } if (!write_to_file(destination, buffer, str)) { print_raspberry(); printf("* Successful. Find assembly file at: %s\n", destination); } terminate_buffer(buffer); terminate_list_arr(list_arr); return EXIT_SUCCESS; }