summary refs log tree commit diff
path: root/lisc/util.c
blob: c1bfa863987287acf22be6adc459ea122cf81b47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "lisc.h"

Typ typ[NTyp];
Ins insb[NIns], *curi;

void
diag(char *s)
{
	fputs(s, stderr);
	fputc('\n', stderr);
	abort();
}

void *
alloc(size_t n)
{
	void *p;

	if (n == 0)
		return 0;
	p = calloc(1, n);
	if (!p)
		abort();
	return p;
}

Blk *
balloc()
{
	static Blk z;
	Blk *b;

	b = alloc(sizeof *b);
	*b = z;
	return b;
}

void
emit(int op, int w, Ref to, Ref arg0, Ref arg1)
{
	if (curi == insb)
		diag("emit: too many instructions");
	*--curi = (Ins){op, w, to, {arg0, arg1}};
}

void
emiti(Ins i)
{
	emit(i.op, i.wide, i.to, i.arg[0], i.arg[1]);
}

int
bcnt(Bits *b)
{
	int z, i, j;
	ulong tmp;

	i = 0;
	for (z=0; z<BITS; z++) {
		tmp = b->t[z];
		for (j=0; j<64; j++) {
			i += 1 & tmp;
			tmp >>= 1;
		}
	}
	return i;
}

void
idup(Ins **pd, Ins *s, ulong n)
{
	free(*pd);
	memcpy(*pd = alloc(n * sizeof(Ins)), s, n * sizeof(Ins));
}

Ins *
icpy(Ins *d, Ins *s, ulong n)
{
	memcpy(d, s, n * sizeof(Ins));
	return d + n;
}