+ dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
+
+ ir_node *base_addr = expression_to_firm(expression->array_ref);
+ ir_node *offset = expression_to_firm(expression->index);
+ offset = create_conv(dbgi, offset, mode_Iu);
+
+ unsigned elem_size = get_type_size(expression->expression.datatype);
+ ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
+ ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
+ mode_Iu);
+ ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
+
+ return result;
+}
+
+static ir_node *array_access_to_firm(
+ const array_access_expression_t *expression)
+{
+
+ dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
+ ir_node *addr = array_access_addr(expression);
+ type_t *type = expression->expression.datatype;
+
+ return load_from_expression_addr(type, addr, dbgi);
+}
+
+static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
+{
+ type_t *type = expression->type;
+ if(type == NULL) {
+ type = expression->size_expression->datatype;
+ assert(type != NULL);
+ }
+
+ ir_mode *mode = get_ir_mode(expression->expression.datatype);
+ unsigned size = get_type_size(type);
+ ir_node *size_node = new_Const_long(mode, size);
+
+ return size_node;
+}
+
+static ir_node *expression_to_addr(const expression_t *expression)
+{
+ switch(expression->type) {
+ case EXPR_ARRAY_ACCESS:
+ return array_access_addr((const array_access_expression_t*) expression);
+ default:
+ break;
+ }
+ panic("trying to get address of non-lvalue");
+}
+
+static ir_node *_expression_to_firm(const expression_t *expression)
+{
+ switch(expression->type) {
+ case EXPR_CONST:
+ return const_to_firm((const const_t*) expression);
+ case EXPR_STRING_LITERAL:
+ return string_literal_to_firm((const string_literal_t*) expression);
+ case EXPR_REFERENCE:
+ return reference_expression_to_firm(
+ (const reference_expression_t*) expression);
+ case EXPR_CALL:
+ return call_expression_to_firm((const call_expression_t*) expression);
+ case EXPR_UNARY:
+ return unary_expression_to_firm((const unary_expression_t*) expression);
+ case EXPR_BINARY:
+ return binary_expression_to_firm(
+ (const binary_expression_t*) expression);
+ case EXPR_ARRAY_ACCESS:
+ return array_access_to_firm(
+ (const array_access_expression_t*) expression);
+ case EXPR_SIZEOF:
+ return sizeof_to_firm((const sizeof_expression_t*) expression);
+ default:
+ break;
+ }
+ panic("unsupported expression found");
+}
+
+static ir_node *expression_to_firm(const expression_t *expression)
+{
+ ir_node *res = _expression_to_firm(expression);
+
+ if(expression->datatype == type_void)
+ return NULL;
+
+ ir_mode *mode = get_ir_mode(expression->datatype);
+ res = create_conv(NULL, res, mode);
+ return res;
+}
+
+
+static void statement_to_firm(statement_t *statement);
+
+static void return_statement_to_firm(return_statement_t *statement)
+{
+ dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
+ ir_node *ret;
+
+ if(statement->return_value != NULL) {
+ ir_node *retval = expression_to_firm(statement->return_value);
+ ir_node *in[1];
+
+ in[0] = retval;
+ ret = new_d_Return(dbgi, get_store(), 1, in);
+ } else {
+ ret = new_d_Return(dbgi, get_store(), 0, NULL);
+ }
+ ir_node *end_block = get_irg_end_block(current_ir_graph);
+ add_immBlock_pred(end_block, ret);
+
+ set_cur_block(NULL);
+}
+
+static void compound_statement_to_firm(compound_statement_t *compound)
+{
+ statement_t *statement = compound->statements;
+ for( ; statement != NULL; statement = statement->next) {
+ //context2firm(&statement->context);
+ statement_to_firm(statement);
+ }
+}
+
+static void expression_statement_to_firm(expression_statement_t *statement)
+{
+ expression_to_firm(statement->expression);
+}
+
+static void if_statement_to_firm(if_statement_t *statement)
+{
+ dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
+ ir_node *condition = _expression_to_firm(statement->condition);
+ assert(condition != NULL);
+
+ /* make sure we have a mode_b condition */
+ condition = create_conv(dbgi, condition, mode_b);
+
+ ir_node *cond = new_d_Cond(dbgi, condition);
+ ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
+ ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
+
+ ir_node *fallthrough_block = new_immBlock();
+
+ /* the true (blocks) */
+ ir_node *true_block = new_immBlock();
+ add_immBlock_pred(true_block, true_proj);
+ mature_immBlock(true_block);
+
+ statement_to_firm(statement->true_statement);
+ if(get_cur_block() != NULL) {
+ ir_node *jmp = new_Jmp();
+ add_immBlock_pred(fallthrough_block, jmp);
+ }
+
+ /* the false (blocks) */
+ if(statement->false_statement != NULL) {
+ ir_node *false_block = new_immBlock();
+ add_immBlock_pred(false_block, false_proj);
+ mature_immBlock(false_block);
+
+ statement_to_firm(statement->false_statement);
+ if(get_cur_block() != NULL) {
+ ir_node *jmp = new_Jmp();
+ add_immBlock_pred(fallthrough_block, jmp);
+ }
+ } else {
+ add_immBlock_pred(fallthrough_block, false_proj);
+ }
+ mature_immBlock(fallthrough_block);
+
+ set_cur_block(fallthrough_block);
+}
+
+static void while_statement_to_firm(while_statement_t *statement)
+{
+ dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
+
+ ir_node *jmp = NULL;
+ if(get_cur_block() != NULL) {
+ jmp = new_Jmp();
+ }
+
+ /* create the header block */
+ ir_node *header_block = new_immBlock();
+ if(jmp != NULL) {
+ add_immBlock_pred(header_block, jmp);
+ }
+
+ /* create the condition */
+ ir_node *condition = _expression_to_firm(statement->condition);
+ condition = create_conv(dbgi, condition, mode_b);
+
+ ir_node *cond = new_d_Cond(dbgi, condition);
+ ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
+ ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
+
+ /* the false block */
+ ir_node *false_block = new_immBlock();
+ add_immBlock_pred(false_block, false_proj);
+
+ /* the loop body */
+ ir_node *body_block = new_immBlock();
+ add_immBlock_pred(body_block, true_proj);
+ mature_immBlock(body_block);
+
+ ir_node *old_continue_label = continue_label;
+ ir_node *old_break_label = break_label;
+ continue_label = header_block;
+ break_label = false_block;
+
+ statement_to_firm(statement->body);
+
+ assert(continue_label == header_block);
+ assert(break_label == false_block);
+ continue_label = old_continue_label;
+ break_label = old_break_label;
+
+ if(get_cur_block() != NULL) {
+ ir_node *jmp = new_Jmp();
+ add_immBlock_pred(header_block, jmp);
+ }
+
+ mature_immBlock(header_block);
+ mature_immBlock(false_block);
+
+ set_cur_block(false_block);
+}
+
+static void do_while_statement_to_firm(do_while_statement_t *statement)
+{
+ dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
+
+ ir_node *jmp = NULL;
+ if(get_cur_block() != NULL) {
+ jmp = new_Jmp();
+ }
+
+ /* create the header block */
+ ir_node *header_block = new_immBlock();
+
+ /* the false block */
+ ir_node *false_block = new_immBlock();
+
+ /* the loop body */
+ ir_node *body_block = new_immBlock();
+ if(jmp != NULL) {
+ add_immBlock_pred(body_block, jmp);
+ }
+
+ ir_node *old_continue_label = continue_label;
+ ir_node *old_break_label = break_label;
+ continue_label = header_block;
+ break_label = false_block;
+
+ statement_to_firm(statement->body);
+
+ assert(continue_label == header_block);
+ assert(break_label == false_block);
+ continue_label = old_continue_label;
+ break_label = old_break_label;
+
+ if(get_cur_block() == NULL) {
+ mature_immBlock(header_block);
+ mature_immBlock(body_block);
+ return;
+ }
+
+ ir_node *body_jmp = new_Jmp();
+ add_immBlock_pred(header_block, body_jmp);
+ mature_immBlock(header_block);
+
+ /* create the condition */
+ ir_node *condition = _expression_to_firm(statement->condition);
+ condition = create_conv(dbgi, condition, mode_b);
+
+ ir_node *cond = new_d_Cond(dbgi, condition);
+ ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
+ ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
+
+ add_immBlock_pred(body_block, true_proj);
+ mature_immBlock(body_block);
+
+ add_immBlock_pred(false_block, false_proj);
+ mature_immBlock(false_block);
+
+ set_cur_block(false_block);
+}
+
+static void create_declaration_entity(declaration_t *declaration,
+ declaration_type_t declaration_type,
+ ir_type *parent_type)
+{
+ ident *id = new_id_from_str(declaration->symbol->string);
+ ir_type *irtype = get_ir_type(declaration->type);
+ ir_entity *entity = new_entity(parent_type, id, irtype);
+ set_entity_ld_ident(entity, id);
+
+ declaration->declaration_type = declaration_type;
+ declaration->v.entity = entity;
+ set_entity_variability(entity, variability_uninitialized);
+ /* TODO: visibility? */
+}
+
+static void create_initializer(declaration_t *declaration)
+{
+ initializer_t *initializer = declaration->init.initializer;
+ if(initializer == NULL)
+ return;
+
+ if(initializer->type == INITIALIZER_VALUE) {
+ assert(initializer->designator == NULL);
+ assert(initializer->next == NULL);
+ ir_node *init_node = expression_to_firm(initializer->v.value);
+
+ if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
+ set_value(declaration->v.value_number, init_node);
+ } else {
+ panic("initializer not completely implemented yet");
+ }
+ } else {
+ assert(initializer->type == INITIALIZER_LIST);
+ panic("list initializer not supported yet");
+ }
+}
+
+static void create_local_variable(declaration_t *declaration)
+{
+ bool needs_entity = declaration->address_taken;
+
+ assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
+
+ if(needs_entity) {
+ ir_type *frame_type = get_irg_frame_type(current_ir_graph);
+ create_declaration_entity(declaration,
+ DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
+ frame_type);
+ } else {
+ declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
+ declaration->v.value_number = next_value_number_function;
+ ++next_value_number_function;
+ }
+
+ create_initializer(declaration);
+}
+
+static void declaration_statement_to_firm(declaration_statement_t *statement)
+{
+ declaration_t *declaration = statement->declarations_begin;
+ declaration_t *end = statement->declarations_end->next;
+ for( ; declaration != end; declaration = declaration->next) {
+ type_t *type = declaration->type;
+
+ switch(declaration->storage_class) {
+ case STORAGE_CLASS_TYPEDEF:
+ continue;
+ case STORAGE_CLASS_STATIC:
+ panic("static local vars not implemented yet");
+ case STORAGE_CLASS_ENUM_ENTRY:
+ panic("enum entry declaration in local block found");
+ case STORAGE_CLASS_EXTERN:
+ panic("extern declaration in local block found");
+ case STORAGE_CLASS_NONE:
+ case STORAGE_CLASS_AUTO:
+ case STORAGE_CLASS_REGISTER:
+ if(type->type == TYPE_FUNCTION) {
+ panic("nested functions not supported yet");
+ } else {
+ create_local_variable(declaration);
+ }
+ continue;
+ }
+ panic("invalid storage class found");
+ }
+}
+
+static void create_jump_statement(const statement_t *statement,
+ ir_node *target_block)
+{
+ dbg_info *dbgi = get_dbg_info(&statement->source_position);
+ ir_node *jump = new_d_Jmp(dbgi);
+ add_immBlock_pred(target_block, jump);
+
+ set_cur_block(NULL);
+}
+
+static void switch_statement_to_firm(const switch_statement_t *statement)
+{
+ dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
+
+ ir_node *expression = expression_to_firm(statement->expression);
+ ir_node *cond = new_d_Cond(dbgi, expression);
+ ir_node *break_block = new_immBlock();
+
+ set_cur_block(NULL);
+
+ ir_node *old_switch_cond = current_switch_cond;
+ ir_node *old_break_label = break_label;
+ current_switch_cond = cond;
+ break_label = break_block;
+
+ statement_to_firm(statement->body);
+
+ if(get_cur_block() != NULL) {
+ ir_node *jmp = new_Jmp();
+ add_immBlock_pred(break_block, jmp);
+ }
+
+ assert(current_switch_cond == cond);
+ assert(break_label == break_block);
+ current_switch_cond = old_switch_cond;
+ break_label = old_break_label;
+
+ mature_immBlock(break_block);
+ set_cur_block(break_block);
+}
+
+static void case_label_to_firm(const case_label_statement_t *statement)
+{
+ dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
+
+ /* let's create a node and hope firm constant folding creates a Const
+ * node... */
+ ir_node *proj;
+ set_cur_block(get_nodes_block(current_switch_cond));
+ if(statement->expression) {
+ ir_node *cnst = expression_to_firm(statement->expression);
+ if(!is_Const(cnst)) {
+ panic("couldn't fold constant for case label");
+ }
+ tarval *tv = get_Const_tarval(cnst);
+ if(!mode_is_int(get_tarval_mode(tv))) {
+ panic("case label not an integer");
+ }
+
+ long pn = get_tarval_long(tv);
+ if(pn == MAGIC_DEFAULT_PN_NUMBER) {
+ /* oops someone detected our cheating... */
+ panic("magic default pn used");
+ }
+ proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
+ } else {
+ proj = new_d_defaultProj(dbgi, current_switch_cond,
+ MAGIC_DEFAULT_PN_NUMBER);
+ }
+
+ ir_node *block = new_immBlock();
+ add_immBlock_pred(block, proj);
+ mature_immBlock(block);
+}
+
+static void statement_to_firm(statement_t *statement)
+{
+ switch(statement->type) {
+ case STATEMENT_COMPOUND:
+ compound_statement_to_firm((compound_statement_t*) statement);
+ return;
+ case STATEMENT_RETURN:
+ return_statement_to_firm((return_statement_t*) statement);
+ return;
+ case STATEMENT_EXPRESSION:
+ expression_statement_to_firm((expression_statement_t*) statement);
+ return;
+ case STATEMENT_IF:
+ if_statement_to_firm((if_statement_t*) statement);
+ return;
+ case STATEMENT_WHILE:
+ while_statement_to_firm((while_statement_t*) statement);
+ return;
+ case STATEMENT_DO_WHILE:
+ do_while_statement_to_firm((do_while_statement_t*) statement);
+ return;
+ case STATEMENT_DECLARATION:
+ declaration_statement_to_firm((declaration_statement_t*) statement);
+ return;
+ case STATEMENT_BREAK:
+ create_jump_statement(statement, break_label);
+ return;
+ case STATEMENT_CONTINUE:
+ create_jump_statement(statement, continue_label);
+ return;
+ case STATEMENT_SWITCH:
+ switch_statement_to_firm((switch_statement_t*) statement);
+ return;
+ case STATEMENT_CASE_LABEL:
+ case_label_to_firm((case_label_statement_t*) statement);
+ return;
+ default:
+ break;
+ }
+ panic("Statement not implemented\n");
+}
+
+static int get_function_n_local_vars(declaration_t *declaration)
+{
+ (void) declaration;
+ /* TODO */
+ return 30;
+}
+
+static void initialize_function_parameters(declaration_t *declaration)
+{
+ ir_graph *irg = current_ir_graph;
+ ir_node *args = get_irg_args(irg);
+ ir_node *start_block = get_irg_start_block(irg);
+
+ int n = 0;
+ declaration_t *parameter = declaration->context.declarations;
+ for( ; parameter != NULL; parameter = parameter->next) {
+ assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
+
+ if(parameter->address_taken) {
+ panic("address take from parameter not implemented yet");
+ }
+
+ ir_mode *mode = get_ir_mode(parameter->type);
+ long pn = n;
+ ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
+ ++n;
+
+ parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
+ parameter->v.value_number = next_value_number_function;
+ ++next_value_number_function;
+
+ set_value(parameter->v.value_number, proj);
+ }
+}
+
+static void create_function(declaration_t *declaration)
+{
+ ir_entity *entity = get_function_entity(declaration);
+
+ if(declaration->init.statement == NULL)
+ return;
+
+ int n_local_vars = get_function_n_local_vars(declaration);
+ ir_graph *irg = new_ir_graph(entity, n_local_vars);
+ ir_node *first_block = get_cur_block();
+
+ next_value_number_function = 0;
+ initialize_function_parameters(declaration);
+
+ statement_to_firm(declaration->init.statement);
+
+ ir_node *end_block = get_irg_end_block(irg);
+
+ /* do we have a return statement yet? */
+ if(get_cur_block() != NULL) {
+ ir_node *ret = new_Return(get_store(), 0, NULL);
+ add_immBlock_pred(end_block, ret);
+ }
+
+ mature_immBlock(first_block);
+ mature_immBlock(end_block);
+
+ irg_finalize_cons(irg);
+
+ /* finalize the frame type */
+ ir_type *frame_type = get_irg_frame_type(irg);
+ int n = get_compound_n_members(frame_type);
+ int align_all = 4;
+ int offset = 0;
+ for(int i = 0; i < n; ++i) {
+ ir_entity *entity = get_compound_member(frame_type, i);
+ ir_type *entity_type = get_entity_type(entity);
+
+ int align = get_type_alignment_bytes(entity_type);
+ if(align > align_all)
+ align_all = align;
+ int misalign = 0;
+ if(align > 0) {
+ misalign = offset % align;
+ offset += misalign;
+ }
+
+ set_entity_offset(entity, offset);
+ offset += get_type_size_bytes(entity_type);
+ }
+ set_type_size_bytes(frame_type, offset);
+ set_type_alignment_bytes(frame_type, align_all);
+ set_type_state(frame_type, layout_fixed);
+
+ irg_vrfy(irg);
+}
+
+static void context_to_firm(context_t *context)
+{
+ declaration_t *declaration = context->declarations;
+ for( ; declaration != NULL; declaration = declaration->next) {
+ if(declaration->namespace != NAMESPACE_NORMAL)
+ continue;
+
+ type_t *type = declaration->type;
+ if(type->type == TYPE_FUNCTION) {
+ create_function(declaration);
+ } else {
+ /* TODO... */
+ }
+ }
+}
+
+void translation_unit_to_firm(translation_unit_t *unit)
+{
+ /* remove me later TODO FIXME */
+ (void) get_type_size;
+
+ /* just to be sure */
+ continue_label = NULL;
+ break_label = NULL;
+ current_switch_cond = NULL;
+
+ context_to_firm(& unit->context);