<commit_before>
#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>

#include "../inc/cc.h"
#include "cc2.h"


static Node *reguse[NPAIRS];
static uint8_t upper[] = {[DE] = D, [HL] = H, [BC] = B,  [IY] = IYH};
static uint8_t lower[] = {[DE] = E, [HL] = L, [BC] = C, [IY] = IYL};
static uint8_t pair[] = {
	[A] = A,
	[H] = HL, [L] = HL, [HL] = HL,
	[B] = BC, [C] = BC, [BC] = BC,
	[D] = DE, [E] = DE, [DE] = DE,
	[IYL] = IY, [IYH] = IY, [IY] = IY
};

Node regs[] = {
	[E] =  {
		.op = REG,
		.reg = E
	},
	[D] = {
		.op = REG,
		.reg = D
	},
	[H] = {
		.op = REG,
		.reg = H
	},
	[L] = {
		.op = REG,
		.reg = L
	},
	[C] = {
		.op= REG,
		.reg = C
	},
	[B] = {
		.op= REG,
		.reg = B
	},
	[A] = {
		.op= REG,
		.reg = A
	},
	[IYL] = {
		.op = REG,
		.reg = IYL
	},
	[IYH] = {
		.op = REG,
		.reg = IYH
	},
	[DE] = {
		.op = REG,
		.reg = DE
	},
	[HL] = {
		.op = REG,
		.reg = HL
	},
	[BC] = {
		.op = REG,
		.reg = BC
	},
	[IX] = {
		.op = REG,
		.reg = IX
	},
	[IY] = {
		.op = REG,
		.reg = IY
	},
	[SP] = {
		.op = REG,
		.reg = SP
	}
};

static uint8_t
allocreg(Node *np)
{
	static uint8_t reg8[] = {A, B, C, D, E, H, L, IYL, IY, 0};
	static uint8_t reg16[] = {BC, DE, IY, 0};
	uint8_t *bp, c;

	switch (np->type.size) {
	case 1:
		bp = reg8;
		break;
	case 2:
		bp = reg16;
		break;
	default:
		abort();
	}
	while (c = *bp++) {
		if (reguse[c])
			continue;
		return c;
	}
	/* TODO: What to do here? */
	abort();
}

static void moveto(Node *np, uint8_t reg);

static void
spill(uint8_t reg)
{
	Node *np, *r;
	Symbol *sym;
	uint8_t p, h, l, new;

	if ((np = reguse[reg]) == NULL)
		return;
	sym = np->sym;
	r = &regs[reg];

	switch (np->type.size) {
	case 1:
		if (sym) {
			code(LDL, np, r);
			np->op = sym->kind;
			sym->dirty = 0;
		} else {
			new = allocreg(np);
			moveto(np, new);
		}
		break;
	default:
		abort();<commit_msg>Change allocreg to receive only a num<commit_after>
#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>

#include "../inc/cc.h"
#include "cc2.h"


static Node *reguse[NPAIRS];
static uint8_t upper[] = {[DE] = D, [HL] = H, [BC] = B,  [IY] = IYH};
static uint8_t lower[] = {[DE] = E, [HL] = L, [BC] = C, [IY] = IYL};
static uint8_t pair[] = {
	[A] = A,
	[H] = HL, [L] = HL, [HL] = HL,
	[B] = BC, [C] = BC, [BC] = BC,
	[D] = DE, [E] = DE, [DE] = DE,
	[IYL] = IY, [IYH] = IY, [IY] = IY
};

Node regs[] = {
	[E] =  {
		.op = REG,
		.reg = E
	},
	[D] = {
		.op = REG,
		.reg = D
	},
	[H] = {
		.op = REG,
		.reg = H
	},
	[L] = {
		.op = REG,
		.reg = L
	},
	[C] = {
		.op= REG,
		.reg = C
	},
	[B] = {
		.op= REG,
		.reg = B
	},
	[A] = {
		.op= REG,
		.reg = A
	},
	[IYL] = {
		.op = REG,
		.reg = IYL
	},
	[IYH] = {
		.op = REG,
		.reg = IYH
	},
	[DE] = {
		.op = REG,
		.reg = DE
	},
	[HL] = {
		.op = REG,
		.reg = HL
	},
	[BC] = {
		.op = REG,
		.reg = BC
	},
	[IX] = {
		.op = REG,
		.reg = IX
	},
	[IY] = {
		.op = REG,
		.reg = IY
	},
	[SP] = {
		.op = REG,
		.reg = SP
	}
};

static uint8_t
allocreg(uint8_t size)
{
	static uint8_t reg8[] = {A, B, C, D, E, H, L, IYL, IY, 0};
	static uint8_t reg16[] = {BC, DE, IY, 0};
	uint8_t *bp, c;

	switch (size) {
	case 1:
		bp = reg8;
		break;
	case 2:
		bp = reg16;
		break;
	default:
		abort();
	}
	while (c = *bp++) {
		if (reguse[c])
			continue;
		return c;
	}
	/* TODO: What to do here? */
	abort();
}

static void moveto(Node *np, uint8_t reg);

static void
spill(uint8_t reg)
{
	Node *np, *r;
	Symbol *sym;
	uint8_t p, h, l, new;

	if ((np = reguse[reg]) == NULL)
		return;
	sym = np->sym;
	r = &regs[reg];

	switch (np->type.size) {
	case 1:
		if (sym) {
			code(LDL, np, r);
			np->op = sym->kind;
			sym->dirty = 0;
		} else {
			new = allocreg(1);
			moveto(np, new);
		}
		break;
	default:
		abort();
{'fraction_non_alphanumeric': 0.24232633279483037, 'fraction_numerical': 0.012924071082390954, 'mean_word_length': 2.1139983235540654, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 20, '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': '4244118', 'n_tokens_mistral': 2124, 'n_tokens_neox': 1971, 'n_words': 499}