exprt gen_not(const exprt &op) { return gen_unary("not", typet("bool"), op); }
/* Object evaluation */ void gen_address(Node node) /*;gen_address*/ { /* * This procedure generates code for the o_expressions * or, in other words, the left-handsides. */ Node pre_node, array_node, range_node, lbd_node, ubd_node, record_node, field_node, id_node; Symbol node_name, type_name, record_name, record_type, field_name, comp_type, proc_name, return_type; int f_off, bse, off, nk; Fortup ft1; #ifdef TRACE if (debug_flag) gen_trace_node("GEN_ADDRESS", node); #endif while (N_KIND(node) == as_insert) { FORTUP(pre_node=(Node), N_LIST(node), ft1); compile(pre_node); ENDFORTUP(ft1); node = N_AST1(node); } node_name = N_UNQ(node); if (is_simple_name(node)) { type_name = get_type(node); if (is_renaming(node_name)) gen_ks(I_PUSH, mu_addr, node_name); else gen_s(I_PUSH_EFFECTIVE_ADDRESS, node_name); /* Arrays are treated in a different manner, depending on their */ /* nature: parameters, constants, variables... */ if (is_array_type(type_name)) { if (is_formal_parameter(node_name)) { type_name = assoc_symbol_get(node_name, FORMAL_TEMPLATE); } gen_s(I_PUSH_EFFECTIVE_ADDRESS, type_name); } } else { switch (nk = N_KIND(node)) { case as_raise: compile(node); break; case as_index: gen_subscript(node); break; case as_slice: array_node = N_AST1(node); range_node = N_AST2(node); /*range_name = N_UNQ(range_node); -- never used ds 7-8-85 */ /* Note: case of type simple name changed into range attribute */ /* by expander */ if (N_KIND(range_node) == as_attribute) { gen_attribute(range_node); } else { /* range */ lbd_node = N_AST1(range_node); ubd_node = N_AST2(range_node); gen_value(lbd_node); gen_value(ubd_node); } if (N_KIND(array_node) == as_attribute) { gen_attribute(array_node); } else { gen_address(array_node); } gen(I_ARRAY_SLICE); break; case as_selector: record_node = N_AST1(node); field_node = N_AST2(node); record_name = N_UNQ(record_node); record_type = get_type(record_node); field_name = N_UNQ(field_node); f_off = FIELD_OFFSET(field_name); if (f_off >= 0 && ((! has_discriminant(record_type)) || NATURE(field_name) == na_discriminant)){ if (is_simple_name(record_node) && !(is_renaming(record_name)) && is_global(record_name)) { reference_of(record_name); bse = REFERENCE_SEGMENT; off = REFERENCE_OFFSET; /* The SETL version has generate(I_PUSH_IMMEDIATE, mu_addr, * ref, field_name); * which we translate as (I_PUSH_EFFECTIVE_ADDRESS ... * ref = [bse, off+f_off]; * Replace use of explicit ref by PUSH_IMMEDIATE */ /* gen_rc(I_PUSH_IMMEDIATE, explicit_ref_new(bse, * off+f_off), ""); */ gen_kv(I_PUSH_IMMEDIATE, mu_word, int_const(bse)); gen_kv(I_PUSH_IMMEDIATE, mu_word, int_const(off+f_off)); } else { gen_address(record_node); if (f_off != 0 ) { gen_ki(I_ADD_IMMEDIATE, mu_word, f_off); } } if (is_array_type(comp_type=TYPE_OF(field_name))) { gen_s(I_PUSH_EFFECTIVE_ADDRESS, comp_type); } } else { gen_address(record_node); gen_s(I_PUSH_EFFECTIVE_ADDRESS, record_type); /* translating following assuming field_name is comment part of *-- instruction ds 7-5-86 * gen_i(I_SELECT, FIELD_NUMBER(field_name), field_name); */ gen_i(I_SELECT, (int) FIELD_NUMBER(field_name)); } break; case as_all: id_node = N_AST1(node); gen_value(id_node); if (is_array_type(N_TYPE(node))) gen_k(I_DEREF, mu_dble); break; case as_call: id_node = N_AST1(node); proc_name = N_UNQ(id_node); return_type = TYPE_OF(proc_name); gen_kc(I_DUPLICATE, kind_of(return_type), "place holder"); compile(node); /* processed from now as a procedure call */ break; case as_un_op: gen_unary(node); break; case as_op: gen_binary(node); break; case as_string_ivalue: gen_value(node); break; default: compiler_error_k("GEN_ADDRESS called with kind ", node); } } }
// stack should look like this: // val // type static void push_expression(ClmExpNode *node) { if (node == NULL) return; ClmType expression_type = clm_type_of_exp(node, data.scope); switch (node->type) { case EXP_TYPE_INT: asm_push_const_i(node->ival); asm_push_const_i((int)expression_type); break; case EXP_TYPE_FLOAT: asm_push_const_f(node->fval); asm_push_const_i((int)expression_type); break; case EXP_TYPE_STRING: // TODO push a string onto the stack break; case EXP_TYPE_ARITH: { ClmType right_type = clm_type_of_exp(node->arithExp.right, data.scope); ClmType left_type = clm_type_of_exp(node->arithExp.left, data.scope); if (left_type == CLM_TYPE_MATRIX && clm_type_is_number(right_type)) { // here the only ops are mul & div... we are scaling matrix // gen left and then right here... if we don't then we have // int val // int type // matrix // cols // rows // matrix type // and we have to pop the int after we generate the value... which is hard // and since we are multiplying the matrix in place, it would be easiest // to // gen the matrix first and then the int, so we just have to pop two // values // in total push_expression(node->arithExp.left); asm_mov(EDX, ESP); push_expression(node->arithExp.right); gen_arith(node); } else { push_expression(node->arithExp.right); asm_mov(EDX, ESP); push_expression(node->arithExp.left); gen_arith(node); } break; } case EXP_TYPE_BOOL: push_expression(node->boolExp.right); asm_mov(EDX, ESP); push_expression(node->boolExp.left); gen_bool(node); break; case EXP_TYPE_CALL: { // first push everything thats not a matrix... and for matrices push a pointer int tempStartID = data.temporaryID; int i; ClmExpNode *param; char temporary[256]; // first for any matrices that are parameters that will be pushed through // the stack, push them on the stack and save their location into a temporary // global for (i = node->callExp.params->length - 1; i >= 0; i--) { param = node->callExp.params->data[i]; if(param->type == CLM_TYPE_MATRIX){ ClmLocation location = clm_location_of_exp(param, data.scope); switch(location){ case LOCATION_STACK: push_expression(param); next_temporary(temporary); asm_mov(temporary, ESP); break; default: break; } } } // then push every expression.. when we get to a matrix, push the pointer // to its location int tempOffset = 1; char index_str[256]; for (i = node->callExp.params->length - 1; i >= 0; i--) { param = node->callExp.params->data[i]; if(param->type == CLM_TYPE_MATRIX){ ClmLocation location = clm_location_of_exp(param, data.scope); switch(location){ case LOCATION_STACK: sprintf(temporary, "dword [temporary%d]", tempStartID + tempOffset); asm_push(temporary); tempOffset++; break; default: { // the only way its a matrix and not on the stack is if its an // ind exp with no indices ClmSymbol *symbol = clm_scope_find(data.scope, param->indExp.id); load_var_location(symbol, index_str, 0, NULL); asm_push(index_str); break; } } asm_push_const_i((int) CLM_TYPE_MATRIX); }else{ push_expression(param); } } asm_call(node->callExp.name); // TODO pop off arguments from the stack break; } case EXP_TYPE_INDEX: push_index(node); break; case EXP_TYPE_MAT_DEC: { int i; if (node->matDecExp.arr != NULL) { for (i = node->matDecExp.length - 1; i >= 0; i--) { // TODO... push f or push i? asm_push_const_i((int)node->matDecExp.arr[i]); } asm_push_const_i(node->matDecExp.size.cols); asm_push_const_i(node->matDecExp.size.rows); asm_push_const_i((int)CLM_TYPE_MATRIX); } else { // push a matrix onto the stack with all 0s char cmp_label[LABEL_SIZE]; char end_label[LABEL_SIZE]; next_label(cmp_label); next_label(end_label); gen_exp_size(node); asm_pop(EAX); // # rows asm_pop(EBX); // # cols asm_mov(ECX, EAX); asm_imul(ECX, EBX); asm_dec(ECX); asm_label(cmp_label); asm_cmp(ECX, "0"); asm_jmp_l(end_label); asm_push_const_i(0); asm_dec(ECX); asm_jmp(cmp_label); asm_label(end_label); asm_push(EBX); asm_push(EAX); asm_push_const_i((int)CLM_TYPE_MATRIX); } break; } case EXP_TYPE_PARAM: break; case EXP_TYPE_UNARY: push_expression(node->unaryExp.node); gen_unary(node); break; } }
exprt gen_not(const exprt &op) { return gen_unary(ID_not, bool_typet(), op); }