Пример #1
0
int parse_instructions(tvm_program_t *p, const char ***tokens, tvm_memory_t *pMemory)
{
	int line_idx;
	for(line_idx = 0; tokens[line_idx]; line_idx++)
	{
		p->instr = (int *)realloc(p->instr, sizeof(int) * (line_idx + 2));
		p->instr[line_idx] = 0;

		p->args = (int ***)realloc(p->args, sizeof(int **) * (line_idx + 2));
		p->args[line_idx] = (int **)calloc(MAX_ARGS, sizeof(int *));

		int token_idx;
		for(token_idx = 0; token_idx < MAX_TOKENS; token_idx++)
		{
			int opcode = 0;

			/* Skip empty tokens */
			if(!tokens[line_idx][token_idx]) continue;

			opcode = instr_to_opcode(tokens[line_idx][token_idx]);

			/* If it *isn't* an opcode, skip trying to parse arguments */
			if(opcode == -1) continue;

			int i, instr_idx = 0, num_instr = p->num_instructions;
			p->instr[p->num_instructions++] = opcode;

			for(i = ++token_idx; i < (token_idx + 2); ++i)
			{
				if(!tokens[line_idx][i] || strlen(tokens[line_idx][i]) <= 0) continue;

				char *newline = strchr(tokens[line_idx][i], '\n');
				if(newline) *newline = 0;

				/* Check to see if the token specifies a register */
				int *pRegister = token_to_register(tokens[line_idx][i], pMemory);
				if(pRegister)
				{
					p->args[num_instr][i - token_idx] = pRegister;
					continue;
				}

				/* Check to see whether the token specifies an address */
				if(tokens[line_idx][i][0] == '[')
				{
					char *end_symbol = strchr(tokens[line_idx][i], ']');
					if(end_symbol)
					{
						*end_symbol = 0;
						p->args[num_instr][i - token_idx] = &((int *)pMemory->mem_space)[tvm_parse_value(tokens[line_idx][i] + 1)];

						continue;
					}
				}

				/* Check if the argument is a label */
				instr_idx = htab_find(p->label_htab, tokens[line_idx][i]);

				if(instr_idx >= 0)
				{
					p->args[num_instr][i - token_idx] = tvm_add_value(p, instr_idx);
					continue;
				}

				/* F**k it, parse it as a value */
				p->args[num_instr][i - token_idx] = tvm_add_value(p, tvm_parse_value(tokens[line_idx][i]));
			}
		}
	}

	p->args[line_idx] = NULL;
	p->instr[line_idx] = -0x1;

	return 0;
}
Пример #2
0
void parse_instruction(tvm_program_t* p, const char** tokens, tvm_memory_t* pMemory)
{
	int token_idx;
	for(token_idx = 0; token_idx < MAX_TOKENS; token_idx++)
	{
		int opcode = 0;

		/* Skip empty tokens */
		if(!tokens[token_idx]) continue;

		opcode = instr_to_opcode(tokens[token_idx]);

		/* If it *is* an opcode, parse the arguments */
		if(opcode != -1)
		{
			int i, instr_idx = 0, num_instr = p->num_instructions;
			p->instr[p->num_instructions++] = opcode;

			for(i = ++token_idx; i < (token_idx + 2); ++i)
			{
				char* newline;
				if(!tokens[i] || strlen(tokens[i]) <= 0) continue;

				newline = strchr(tokens[i], '\n');
				if(newline) *newline = 0;

				/* Check to see if the token specifies a register */
				int* pRegister = token_to_register(tokens[i], pMemory);
				if(pRegister)
				{
					p->args[num_instr][i - token_idx] = pRegister;
					continue;
				}

				/* Check to see whether the token specifies an address */
				if(tokens[i][0] == '[')
				{
					char* end_symbol = strchr(tokens[i], ']');
					if(end_symbol)
					{
						*end_symbol = 0;
						p->args[num_instr][i - token_idx] = &((int*)pMemory->mem_space)[tvm_parse_value(tokens[i] + 1)];

						continue;
					}
				}

				/* Check if the argument is a label */
				instr_idx = htab_find(p->label_htab, tokens[i]);

				if(instr_idx >= 0)
				{
					p->args[num_instr][i - token_idx] = tvm_add_value(p, instr_idx);
					continue;
				}

				/* F**k it, parse it as a value */
				p->args[num_instr][i - token_idx] = tvm_add_value(p, tvm_parse_value(tokens[i]));
			}
		}
	}
}