<commit_before>#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#include "asm.h"
#include "asm_gen.h"
#include "ir.h"
#include "misc.h"
#include "util.h"

static void block_init(IrBlock *block, char *name, u32 id)
{
	block->name = name;
	block->id = id;
	ARRAY_INIT(&block->instrs, IrInstr *, 10);
}

static void block_free(IrBlock *block)
{
	array_free(&block->instrs);
}

void trans_unit_init(TransUnit *trans_unit)
{
	ARRAY_INIT(&trans_unit->globals, IrGlobal *, 10);
	ARRAY_INIT(&trans_unit->types, IrGlobal *, 5);
	pool_init(&trans_unit->pool, 512);
}

void trans_unit_free(TransUnit *trans_unit)
{
	for (u32 i = 0; i < trans_unit->globals.size; i++) {
		IrGlobal *global = *ARRAY_REF(&trans_unit->globals, IrGlobal *, i);
		if (global->type.t == IR_FUNCTION) {
			if (global->initializer != NULL) {
				IrFunction *func = &global->initializer->u.function;
				for (u32 j = 0; j < func->blocks.size; j++) {
					IrBlock *block = *ARRAY_REF(&func->blocks, IrBlock *, j);
					block_free(block);
				}
				array_free(&func->blocks);
			}
		}
	}

	array_free(&trans_unit->globals);
	array_free(&trans_unit->types);
	pool_free(&trans_unit->pool);
}

IrBlock *add_block_to_function(
		TransUnit *trans_unit, IrFunction *function, char *name)
{
	IrBlock *block = pool_alloc(&trans_unit->pool, sizeof *block);
	*ARRAY_APPEND(&function->blocks, IrBlock *) = block;
	block_init(block, name, function->blocks.size - 1);

	return block;
}

IrGlobal *trans_unit_add_function(TransUnit *trans_unit, char *name,
		IrType return_type, u32 arity, bool variable_arity, IrType *arg_types)
{
	IrGlobal *new_global = pool_alloc(&trans_unit->pool, sizeof *new_global);
	*ARRAY_APPEND(&trans_unit->globals, IrGlobal *) = new_global;
	ZERO_STRUCT(new_global);

	new_global->name = name;

	IrType *return_type_ptr = pool_alloc(&trans_unit->pool, sizeof *return_type_ptr);
	*return_type_ptr = return_type;
	IrType *arg_types_ptr = pool_alloc(&trans_unit->pool, arity * sizeof *arg_types_ptr);
	memcpy(arg_types_ptr, arg_types, arity * sizeof *arg_types_ptr);

	IrType function_type = {
		.t = IR_FUNCTION,
		.u.function.arity = arity,
		.u.function.variable_arity = variable_arity,
		.u.function.return_type = return_type_ptr,
		.u.function.arg_types = arg_types_ptr,
	};
	new_global->type = function_type;

	return new_global;
}

IrConst *add_init_to_function(TransUnit *trans_unit, IrGlobal *global)
{
	IrConst *initializer = pool_alloc(&trans_unit->pool, sizeof *initializer);
	initializer->type = global->type;
	IrFunction *function = &initializer->u.function;

	global->initializer = initializer;

	ARRAY_INIT(&function->blocks, IrBlock *, 5);
	add_block_to_function(trans_unit, function, "entry");

	return initializer;
}

IrGlobal *trans_unit_add_var(TransUnit *trans_unit, char *name, IrType type)
{
	IrGlobal *new_global = pool_alloc(&trans_unit->pool, sizeof *new_global);
	*ARRAY_APPEND(&trans_unit->globals, IrGlobal *) = new_global;
	ZERO_STRUCT(new_global);

	new_global->name = name;
	new_global->type = type;
	new_global->initializer = NULL;

	return new_global;
}

IrType *trans_unit_add_struct(TransUnit *trans_unit, char *name, u32 num_fields)
{
	IrType *new_type = pool_alloc(&trans_unit->pool, sizeof *new_type);
	*ARRAY_APPEND(&trans_unit->types, IrType *) = new_type;

	if (name == NULL) {
		char fmt[] = "__anon_struct_%x";

		// - 2 adjusts down for the "%x" which isn't present in the output
		// sizeof(u32) * 2 is the max length of globals.size in hex
		// + 1 for the null terminator
		u32 name_max_length = sizeof fmt - 2 + sizeof(u32) * 2 + 1;
		name = pool_alloc(&trans_unit->pool, name_max_length);
		snprintf(name, name_max_length, fmt, trans_unit->globals.size);
	}

	new_type->t = IR_STRUCT;
	new_type->u.strukt.name = name;
	new_type->u.strukt.num_fields = num_fields;
	IrStructField *fields = pool_alloc(&trans_unit->pool, num_fields * sizeof *fields);
	new_type->u.strukt.fields = fields;
	new_type->u.strukt.total_size = 0;

	return new_type;
}

bool ir_type_eq(IrType *a, IrType *b)<commit_msg>Fix bug in anonymous struct naming.<commit_after>#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#include "asm.h"
#include "asm_gen.h"
#include "ir.h"
#include "misc.h"
#include "util.h"

static void block_init(IrBlock *block, char *name, u32 id)
{
	block->name = name;
	block->id = id;
	ARRAY_INIT(&block->instrs, IrInstr *, 10);
}

static void block_free(IrBlock *block)
{
	array_free(&block->instrs);
}

void trans_unit_init(TransUnit *trans_unit)
{
	ARRAY_INIT(&trans_unit->globals, IrGlobal *, 10);
	ARRAY_INIT(&trans_unit->types, IrGlobal *, 5);
	pool_init(&trans_unit->pool, 512);
}

void trans_unit_free(TransUnit *trans_unit)
{
	for (u32 i = 0; i < trans_unit->globals.size; i++) {
		IrGlobal *global = *ARRAY_REF(&trans_unit->globals, IrGlobal *, i);
		if (global->type.t == IR_FUNCTION) {
			if (global->initializer != NULL) {
				IrFunction *func = &global->initializer->u.function;
				for (u32 j = 0; j < func->blocks.size; j++) {
					IrBlock *block = *ARRAY_REF(&func->blocks, IrBlock *, j);
					block_free(block);
				}
				array_free(&func->blocks);
			}
		}
	}

	array_free(&trans_unit->globals);
	array_free(&trans_unit->types);
	pool_free(&trans_unit->pool);
}

IrBlock *add_block_to_function(
		TransUnit *trans_unit, IrFunction *function, char *name)
{
	IrBlock *block = pool_alloc(&trans_unit->pool, sizeof *block);
	*ARRAY_APPEND(&function->blocks, IrBlock *) = block;
	block_init(block, name, function->blocks.size - 1);

	return block;
}

IrGlobal *trans_unit_add_function(TransUnit *trans_unit, char *name,
		IrType return_type, u32 arity, bool variable_arity, IrType *arg_types)
{
	IrGlobal *new_global = pool_alloc(&trans_unit->pool, sizeof *new_global);
	*ARRAY_APPEND(&trans_unit->globals, IrGlobal *) = new_global;
	ZERO_STRUCT(new_global);

	new_global->name = name;

	IrType *return_type_ptr = pool_alloc(&trans_unit->pool, sizeof *return_type_ptr);
	*return_type_ptr = return_type;
	IrType *arg_types_ptr = pool_alloc(&trans_unit->pool, arity * sizeof *arg_types_ptr);
	memcpy(arg_types_ptr, arg_types, arity * sizeof *arg_types_ptr);

	IrType function_type = {
		.t = IR_FUNCTION,
		.u.function.arity = arity,
		.u.function.variable_arity = variable_arity,
		.u.function.return_type = return_type_ptr,
		.u.function.arg_types = arg_types_ptr,
	};
	new_global->type = function_type;

	return new_global;
}

IrConst *add_init_to_function(TransUnit *trans_unit, IrGlobal *global)
{
	IrConst *initializer = pool_alloc(&trans_unit->pool, sizeof *initializer);
	initializer->type = global->type;
	IrFunction *function = &initializer->u.function;

	global->initializer = initializer;

	ARRAY_INIT(&function->blocks, IrBlock *, 5);
	add_block_to_function(trans_unit, function, "entry");

	return initializer;
}

IrGlobal *trans_unit_add_var(TransUnit *trans_unit, char *name, IrType type)
{
	IrGlobal *new_global = pool_alloc(&trans_unit->pool, sizeof *new_global);
	*ARRAY_APPEND(&trans_unit->globals, IrGlobal *) = new_global;
	ZERO_STRUCT(new_global);

	new_global->name = name;
	new_global->type = type;
	new_global->initializer = NULL;

	return new_global;
}

IrType *trans_unit_add_struct(TransUnit *trans_unit, char *name, u32 num_fields)
{
	IrType *new_type = pool_alloc(&trans_unit->pool, sizeof *new_type);
	*ARRAY_APPEND(&trans_unit->types, IrType *) = new_type;

	if (name == NULL) {
		char fmt[] = "__anon_struct_%x";

		// - 2 adjusts down for the "%x" which isn't present in the output
		// sizeof(u32) * 2 is the max length of globals.size in hex
		// + 1 for the null terminator
		u32 name_max_length = sizeof fmt - 2 + sizeof(u32) * 2 + 1;
		name = pool_alloc(&trans_unit->pool, name_max_length);
		snprintf(name, name_max_length, fmt, trans_unit->types.size);
	}

	new_type->t = IR_STRUCT;
	new_type->u.strukt.name = name;
	new_type->u.strukt.num_fields = num_fields;
	IrStructField *fields = pool_alloc(&trans_unit->pool, num_fields * sizeof *fields);
	new_type->u.strukt.fields = fields;
	new_type->u.strukt.total_size = 0;

	return new_type;
}

bool ir_type_eq(IrType *a, IrType *b)
{'fraction_non_alphanumeric': 0.143403205918619, 'fraction_numerical': 0.008631319358816275, 'mean_word_length': 5.244033872209392, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '841265', 'n_tokens_mistral': 3160, 'n_tokens_neox': 2991, 'n_words': 614}