Пример #1
0
int output_offset(const char *buf, const char *mnemonic,
		int opcode)
{
	char mn_buf[20];
	int mlen = strlen(mnemonic);
	snprintf(mn_buf, 19, "%s ", mnemonic);
	mn_buf[19] = '\0';
	if(!strncmp(buf, mn_buf, mlen + 1)) {
		int succ;
		long int parsed_num = getnum(buf + mlen + 1, &succ);
		if(!succ) {
			if(buf[mlen + 1] >= 'a' && buf[mlen + 1] <= 'z') {
				output_to_label(opcode, buf[mlen + 1] - 'a');
			}
			else {
				fprintf(stderr, "?\n");
			}
		}
		else {
			output_num(opcode, parsed_num);
		}
		return 1;
	}
	else
		return 0;
}
Пример #2
0
static void crash_handler(int signum, siginfo_t *info, void *data) {
  void *addrlist[MAX_FRAMES + 1];
  int addrlen;

  output_string("\n\n\n*******************************\nCaught signal ");
  output_num(signum);
  output_string("\n");

  addrlen = backtrace(addrlist, GPR_ARRAY_SIZE(addrlist));

  if (addrlen == 0) {
    output_string("  no backtrace\n");
  } else {
    backtrace_symbols_fd(addrlist, addrlen, STDERR_FILENO);
  }

  /* try to get a core dump for SIGTERM */
  if (signum == SIGTERM) signum = SIGQUIT;
  raise(signum);
}
Пример #3
0
int main(int argc, char **argv)
{
	char buf[1024];
	int fundef = 0;
	interactive = argc > 1 && !strncmp(argv[1], "-i", 2);
	reset_labels();
	if(!interactive) {
		int lib_index = 1;
		output_magic();
		output_version();
		for(lib_index = 1; lib_index < MAX_NUM_LIBS && lib_index < argc;
				lib_index++) {
			output_until_space(argv[lib_index], 1);
			output_header(' ');
		}
		output_header('\0');
	}
	while(1) {
		int emptyline;
		if(interactive) {
			printf(">>> ");
			fflush(stdout);
		}
		emptyline = (fgets(buf, 1024, stdin) == NULL);
		if(!interactive && emptyline)
			break;
		if(buf[0] == ':') {
			if(buf[1] >= 'a' && buf[1] <= 'z') {
				labels[buf[1] - 'a'] = current_addr;
			}
		}
		else if(!strncmp(buf, "ADD", 3)) {
			output(OPCODE_ADD);
		}
		else if(!strncmp(buf, "SUB", 3)) {
			output(OPCODE_SUB);
		}
		else if(!strncmp(buf, "MUL", 3)) {
			output(OPCODE_MUL);
		}
		else if(!strncmp(buf, "DIV", 3)) {
			output(OPCODE_DIV);
		}
		else if(!strncmp(buf, "LT", 2)) {
			output(OPCODE_LT);
		}
		else if(!strncmp(buf, "LE", 2)) {
			output(OPCODE_LE);
		}
		else if(!strncmp(buf, "EQ", 2)) {
			output(OPCODE_EQ);
		}
		else if(!strncmp(buf, "DUP", 3)) {
			output(OPCODE_DUP);
		}
		else if(!strncmp(buf, "DROP", 4)) {
			output(OPCODE_DROP);
		}
		else if(!strncmp(buf, "NOP", 3)) {
			output(OPCODE_NOP);
		}
		else if(!strncmp(buf, "SWAP", 4)) {
			output(OPCODE_SWAP);
		}
		else if(!strncmp(buf, "NEW", 3)) {
			output(OPCODE_NEW);
		}
		else if(!strncmp(buf, "RSTORE", 6)) {
			output(OPCODE_RSTORE);
		}
		else if(!strncmp(buf, "RLOAD", 4)) {
			output(OPCODE_RLOAD);
		}
		else if(!strncmp(buf, "END_THUNK", 9)) {
			output(OPCODE_END_THUNK);
		}
		else if(output_offset(buf, "BR", OPCODE_BRANCH));
		else if(output_offset(buf, "BRNZ", OPCODE_BRANCHNZ));
		else if(output_offset(buf, "THUNK", OPCODE_START_THUNK));
		else if(!strncmp(buf, "FUNCALL ", 8)) {
			int succ;
			long int parsed_num = getnum(buf + 8, &succ);
			if(!succ)
				fprintf(stderr, "?\n");
			else {
				output_num(OPCODE_CALLFUN, parsed_num);
			}
		}
		else if(!strncmp(buf, "FUNPCALL", 8)) {
			output(OPCODE_CALLPFUN);
		}
		else if(!strncmp(buf, "FUNDEF ", 7)) {
			if(fundef) {
				fprintf(stderr, "?\n");
			}
			else {
				int parsed_funnum, parsed_params;
				int succ = get_two_nums(buf + 7, &parsed_funnum, &parsed_params);
				if(!succ)
					fprintf(stderr, "?\n");
				else {
					output_nums(OPCODE_DEFUN_START, parsed_params, parsed_funnum);
					fundef = 1;
					reset_labels();
				}
			}
		}
		else if(!strncmp(buf, "FUNEND", 6)) {
			fundef = funend(OPCODE_DEFUN_END, fundef);
		}
		else if(!strncmp(buf, "RET0", 4)) {
			fundef = funend(OPCODE_RET0, fundef);
		}
		else if(!strncmp(buf, "RET1", 4)) {
			fundef = funend(OPCODE_RET1, fundef);
		}
		else if(!strncmp(buf, "LOAD ", 5)) {
			int succ;
			long int parsed_num = getnum(buf + 5, &succ);
			if(!succ)
				fprintf(stderr, "?\n");
			else
				output_num(OPCODE_LOAD, parsed_num);
		}
		else if(!strncmp(buf, "FFIDEF ", 7)) {
			if(fundef) {
				fprintf(stderr, "?\n");
			}
			else {
				int succ = handle_ffidef(buf + 7);
				if(!succ) {
					fprintf(stderr, "FFIDEF?\n");
				}
			}
		}
		else if(buf[0] == 'f') {
			int succ;
			long int parsed_num = getnum(buf + 1, &succ);
			if(!succ)
				fprintf(stderr, "?\n");
			else
				output_num(OPCODE_PFUN_ID, parsed_num);
		}
		else if(buf[0] == '-' || isdigit(buf[0])) {
			int succ;
			long int parsed_num = getnum(buf, &succ);
			if(!succ)
				fprintf(stderr, "?\n");
			else
				output_num(OPCODE_INT, parsed_num);
		}
		else if(interactive && !strncmp(buf, "run", 3)) {
			parse_buffer(interactive_buffer,
					interactive_bufferpos);
		}
		else if(buf[0]) {
			fprintf(stderr, "?\n");
		}
	}
	return 0;
}
Пример #4
0
void output_to_label(char opcode, int label_index)
{
	label_offsets[label_index] = output_buffer_offset;
	output_num(opcode, 0);
}