+ if (is_main(entity)) {
+ /* force main to C linkage */
+ type_t *type = entity->declaration.type;
+ assert(is_type_function(type));
+ if (type->function.linkage != LINKAGE_C) {
+ type_t *new_type = duplicate_type(type);
+ new_type->function.linkage = LINKAGE_C;
+
+ type = typehash_insert(new_type);
+ if (type != new_type) {
+ obstack_free(type_obst, new_type);
+ }
+ entity->declaration.type = type;
+ }
+ }
+
+ symbol_t *symbol = entity->base.symbol;
+ ident *id = new_id_from_str(symbol->string);
+
+ ir_type *global_type = get_glob_type();
+ ir_type *ir_type_method = get_ir_type(entity->declaration.type);
+ bool const has_body = entity->function.statement != NULL;
+
+ /* already an entity defined? */
+ ir_entity *irentity = entitymap_get(&entitymap, symbol);
+ if (irentity != NULL) {
+ if (get_entity_visibility(irentity) == visibility_external_allocated
+ && has_body) {
+ set_entity_visibility(irentity, visibility_external_visible);
+ }
+ goto entity_created;
+ }
+
+ dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
+ irentity = new_d_entity(global_type, id, ir_type_method, dbgi);
+ set_entity_ld_ident(irentity, create_ld_ident(entity));
+
+ handle_gnu_attributes_ent(irentity, entity);
+
+ /* static inline => local
+ * extern inline => local
+ * inline without definition => local
+ * inline with definition => external_visible */
+ storage_class_tag_t const storage_class
+ = (storage_class_tag_t) entity->declaration.storage_class;
+ bool const is_inline = entity->function.is_inline;
+ if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
+ set_entity_visibility(irentity, visibility_external_visible);
+ } else if (storage_class == STORAGE_CLASS_STATIC ||
+ (is_inline && has_body)) {
+ if (!has_body) {
+ /* this entity was declared, but is defined nowhere */
+ set_entity_peculiarity(irentity, peculiarity_description);
+ }
+ set_entity_visibility(irentity, visibility_local);
+ } else if (has_body) {
+ set_entity_visibility(irentity, visibility_external_visible);
+ } else {
+ set_entity_visibility(irentity, visibility_external_allocated);
+ }
+ set_entity_allocation(irentity, allocation_static);
+
+ /* We should check for file scope here, but as long as we compile C only
+ this is not needed. */
+ if (! firm_opt.freestanding) {
+ /* check for a known runtime function */
+ for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
+ if (id != rts_idents[i])
+ continue;
+
+ /* ignore those rts functions not necessary needed for current mode */
+ if ((c_mode & rts_data[i].flags) == 0)
+ continue;
+ assert(rts_entities[rts_data[i].id] == NULL);
+ rts_entities[rts_data[i].id] = irentity;
+ }
+ }
+
+ entitymap_insert(&entitymap, symbol, irentity);
+
+entity_created:
+ entity->declaration.kind = DECLARATION_KIND_FUNCTION;
+ entity->function.entity = irentity;
+
+ return irentity;
+}
+
+/**
+ * Creates a Const node representing a constant.
+ */
+static ir_node *const_to_firm(const const_expression_t *cnst)
+{
+ dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
+ ir_mode *mode = get_ir_mode(cnst->base.type);
+
+ char buf[128];
+ tarval *tv;
+ size_t len;
+ if (mode_is_float(mode)) {
+ tv = new_tarval_from_double(cnst->v.float_value, mode);