Exemple #1
0
int handle_ffidef(const char *buf)
{
	int parsed_funnum;
	unsigned char types[MAX_NUM_FFI_PARAMETERS];
	int i = 0, j = 0;
	long int val;
	const char *numptr = buf;
	memset(types, 0x00, sizeof(types));
	char *endptr;
	parsed_funnum = strtol(numptr, &endptr, 0);
	if(endptr == numptr || !parsed_funnum)
		return 0;
	numptr++;
	do {
		val = strtol(numptr, &endptr, 0);
		if(endptr == numptr)
			return 0;
		types[i] = val;
		numptr = endptr + 1;
		i++;
	} while(i < sizeof(types) - 1 && (!i || val != 0));
	output_nums(OPCODE_FFIDEF, parsed_funnum, types[0]);
	for(j = 1; j < i; j += 4) {
		output_char(types[j]);
		if(j + 1 < i)
			output_char(types[j + 1]);
		else
			output_char(0);
		if(j + 2 < i)
			output_char(types[j + 2]);
		else
			output_char(0);
		if(j + 3 < i)
			output_char(types[j + 3]);
		else
			output_char(0);
	}
	output_until_space(numptr, 0);
	sync_output();
	reset_labels();
	return 1;
}
Exemple #2
0
int main(int argc, char** argv) {
    if (argc!=3) {
        fprintf(stderr, "Usage: flip input ouptut\n");
	return 1;
    }
    FILE* fi=fopen(argv[1],"r");
    FILE* fo=fopen(argv[2],"w");
    if (!fi || !fo) {
        fprintf(stderr, "Unable to open file\n");
        return 2;
    }
    int howmany=0;	// how many numbers in the array?
    int* nums=read_nums_presize(fi, &howmany);
    output_nums(fo, nums, howmany);
    if (nums!=0) {
        free(nums);
    }
    fclose(fi);
    fclose(fo);
    return 0;
}
Exemple #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;
}