language

Some fools attempt at an interpreted language
Log | Files | Refs

bc.c (5856B)


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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <stdlib.h>
#include <stdio.h>

#include "bc.h"

#include "fh.h"
#include "var.h"
#include "helper.h"
#include "is.h"

/* Handles allocation for new `bc_cont` instances
 */
bc_cont* bc_cont_new(void)
{
	bc_cont *new = (bc_cont*)malloc(sizeof(bc_cont));
	M_ASSERT(new);

	new->args[0] = NULL;
	new->args[1] = NULL;
	new->args[2] = NULL;

	new->varg[0] = NULL;
	new->varg[1] = NULL;
	new->varg[2] = NULL;

	new->sarg[0] = 0;
	new->sarg[1] = 0;


	return new;
}

void bc_cont_del(bc_cont* ins)
{
	if (ins != NULL)
	{
		if (ins->args[0] != NULL) free(ins->args[0]);
		if (ins->args[1] != NULL) free(ins->args[1]);
		if (ins->args[2] != NULL) free(ins->args[2]);

		if (ins->varg[0] != NULL) var_del(ins->varg[0]);
		if (ins->varg[1] != NULL) var_del(ins->varg[1]);
		if (ins->varg[2] != NULL) var_del(ins->varg[2]);
	
		free(ins);
	}
}

/* Given a file object, and an instance of `bc_cont` with proper metadata, this
 * function will read arguements into bc_cont.
 *
 * 'bc_cont' proper metadata is generated by
 */
void get_opcode_adata(FILE* f, bc_cont* ins)
{
	int num_args,
	    arg_types[3];

	// `is.h`-> unencode
	unencode(ins->mdata, &num_args, arg_types);

	int x;
	for (x = 0; x < num_args; x++)
	{
		// A_BYTE -> byte-length arguement
		if (arg_types[x] == A_BYTE)
		{
			ins->args[x] = get_byte_arg(f, &ins->sarg[x]);
		} else
		// A_NAME -> name-length arguement
		if (arg_types[x] == A_NAME)
		{
			ins->args[x] = get_name_arg(f, &ins->sarg[x]);
		} else
		// A_ADDR -> address-length arguement
		if (arg_types[x] == A_ADDR)
		{
			ins->args[x] = get_addr_arg(f, &ins->sarg[x]);
		} else
		// A_DYNC -> dynamic-length arguement
		if (arg_types[x] == A_DYNC)
		{
			ins->args[x] = get_dync_arg(f, &ins->sarg[x]);
		}
	}
}
byte_t* get_byte_arg(FILE* f, int* size)
{
	*size = 1;
	return read_bytes(f, 1);
}
byte_t* get_name_arg(FILE* f, int* size)
{
	*size = NAMELEN;
	return read_bytes(f, NAMELEN);
}
byte_t* get_addr_arg(FILE* f, int* size)
{
	*size = ADDRLEN;
	return read_bytes(f, ADDRLEN);
}
byte_t* get_dync_arg(FILE* f, int* size)
{
	int n = 0;

	byte_t byte = read_byte(f);

	// This bit gets the length in bytes it needs to read into a buffer
	// NOTE:
	//      There are certain 'holes' where, a certain number, ex.
	//        0xFF00, 0xFF0001, 0xFFFF00, etc
	//      will yeild improper values due to the while loop below encountering
	//      a null octet prematurely.
	//      Numbers that work are the following:
	//        0xFF01, 0xFF0101, 0xFFFF01
	//      Whenever a seralizer is using dynamic arguements the seralizer must
	//      implement a method that asserts if a number is such that each octet
	//      does not equal 0. For a given bitwidth, the set of numbers is
	//      described roughly:
	//        { <valid octets> | !(0 in <octets>) }
	//      if the length of the body of a set of bytes is a member of
	//      <valid octets>, it can work with this function.
	while (byte != 0)
	{
		n = (n << 8 | byte);
		byte = read_byte(f);
	}

	*size = n;

	return read_bytes(f, n);
}

/* Given an instruction, convert raw arguement data into typed data
 *  bc_cont* - bytecode container
 */
void process_args(bc_cont* ins)
{
	int num_args,
	    arg_types[3];

	// `is.h`-> unencode
	unencode(ins->adata, &num_args, arg_types);

	int x;
	for (x = 0; x < num_args; x++)
	{
		if (arg_types[x] == BTOI)
		{
			ins->varg[x] = raw_to_int(ins->sarg[x], 0, ins->args[x]);
			ins->varg[x]->ownership = 0;
		} else
		if (arg_types[x] == BTOT)
		{
			ins->varg[x] = byte_to_type(ins->args[x][0]);
			ins->varg[x]->ownership = 0;
		} else
		if (arg_types[x] == DTOL)
		{
			ins->varg[x] = raw_to_plist(ins->sarg[x], ins->args[x]);
			ins->varg[x]->ownership = 0;
		} else
		if (arg_types[x] == DTOV)
		{
			ins->varg[x] = raw_to_var(ins->sarg[x], ins->args[x]);
			ins->varg[x]->ownership = 0;
		}
	}
}

/* Reads program into bc_cont instances
 *  FILE*    - File descriptor
 *  bc_addr* - pointer to size variable
 */
bc_cont** bc_read(FILE* f, bc_addr* len)
{
	N_ASSERT(f, "bc_read\n");

	long fsize = read_size(f);

	bc_addr addr = 0;
	bc_cont* ptr;
	byte_t byte;

	bc_cont** heap = (bc_cont**)malloc(sizeof(bc_cont*)*fsize);
	N_ASSERT(heap, "bc_read\n");

	/* Loop through file byte-by-byte */
	while (ftell(f) < fsize)
	{
		// Creates a new bc_cont instance
		ptr = bc_cont_new();

		// Gets the opcode
		byte = read_byte(f);
		// Gets opcode metadata
		get_opcode_mdata(byte, ptr);
		// Gets opcode arguement metadata
		get_opcode_adata(f, ptr);
		// Process raw arguements
		process_args(ptr);
		// Set the real address of instruction
		ptr->real_addr = addr;
		// Add instruction to program
		heap[addr] = ptr;
		// Increment address
		addr++;
	}

	*len = addr;
	return heap;
}

/* Reads program into bc_t instance
 *  char* - filename
 */
bc_t* bc_init(char* fname)
{
	N_ASSERT(fname, "bc_init\n");

	FILE* f;

	bc_t* program;
	
	f = fopen(fname, "rb");

	program = (bc_t*)malloc(sizeof(bc_t));
	N_ASSERT(program, "bc_read\n");

	// Read bytecode into array of bc_cont*
	program->heap = bc_read(f, &program->size);

	fclose(f);

	return program;
}

/* Deletes instance of bc_t*
 */
void bc_del(bc_t* program)
{
	N_ASSERT(program, "bc_del\n");

	int i;
	for (i=0; i < program->size; i++)
	{
		if (program->heap[i] != NULL)
			bc_cont_del(program->heap[i]);
	}

	free(program->heap);

	free(program);
}

/* Thing for pretty printing
 */
void bc_print_op(bc_cont* op)
{
	int num_args,
	    arg_types[3];

	unencode(op->mdata, &num_args, arg_types);

	printf("%x\t", op->op);
	for (int i = 0; i < num_args && num_args != 0; i++)
	{
		if (arg_types[i] == A_BYTE)
		{
			printf("%x, ", op->args[i][0]);
		} else
		if (arg_types[i] == A_NAME)
		{
			printf("%x %x, ", op->args[i][0], op->args[i][1]);
		} else
		if (arg_types[i] == A_DYNC)
		{
			for (int x = 0; x < op->sarg[i]; x++)
				printf("%x ", op->args[i][x]);
		}
	}
}