Beispiel #1
0
/*メイン関数*/
int main(int argc, char *argv[]){
    /*変数宣言*/
    int result;  //解析結果とラン結果
    char *position;  //ブラケットが命令文の何文字目かのポインタ
    char *mem_ptr;  //メモリのポインタ
    char *inp_ptr = argv[1];  //命令文のポインタ
    
    
    /*初期設定*/
    mem_ptr = mem;
    e_bracket = NULL;
    
    printf("analyze code...\n");
    
    /*入力された命令の解析*/
    result = analyze(argv[1]);
    if(result == 1){
        printf("syntax error\n");
        return 1;
    }
    printf("run code:\n");
    
    /*入力された命令の実行*/
    result = runcode(mem_ptr, inp_ptr, position);
    if(result == 1){
        printf("error\n");
        return 1;
    }

    return 0;
}
Beispiel #2
0
qString RunLLVMBuilding(qValue * prog)
{
	InitializeNativeTarget();
	
	llvm::Module * m = CreateModule();
	globalllvmmodule=m;

	prog->LLVM_prebuild(m);
	prog->LLVM_build(m);

	std::string ErrorInfo;
	std::string Q;
	llvm::raw_string_ostream * ff1 = new llvm::raw_string_ostream(Q);
	llvm::AssemblyAnnotationWriter * Annotator = 0;
	m->print(*ff1, Annotator);
	
	if (ShouldWriteOutput())
		setFile("compile_llvm.txt",Q);

	for (int i =0;i<llvmglobalvars.size();i++) 
	{
		GlobalVariable * gv= (GlobalVariable*)llvmglobalvars[i]->llvmvar;

		llvmglobalvars[i]->dataptr = TheExecutionEngine->getOrEmitGlobalVariable(gv);

		if (llvmglobalvars[i]->pretty_name== "EMPTY_WND_PROC")
		{
			int * pttt = (int*)	llvmglobalvars[i]->dataptr;
			*pttt= (int)xxxWndProc;
		}
	}

	qneu_Function * ff = 0;//FIXME functions["main"][0];
	fun mainfun = (fun)TheExecutionEngine->getPointerToFunction(ff->llvmd);

	qdtprintf2("Running...\n\n");

	int wynik=runcode(mainfun);

	qdtprintf2("\n\n *** Return value: %d\n", wynik);

	return Q;
}
Beispiel #3
0
int main(int argc, char **argv) {
    branch *brstack;
    branch closer;
    char *bfp;
    char *mem;
    int count;


    if(argc != 2) {
        printf("Bugger off, need 2 args but you gave %d\n", argc);
        return -1;
    }

    u8 *code = mmap(NULL, 128*1024, PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0);
    u8 *ptr = code;
    if(code == (u8 *)-1) {
        printf("Could not mmap!\n");
        return -1;
    }

    mem = malloc(30000);
    if(!mem) {
        printf("Could not allocate machine memory!\n");
        return -1;
    }

    brstack = malloc(sizeof(branch)*MAX_RECURSE);
    if(!brstack) {
        printf("Could not allocate branch stack!\n");
        return -1;
    }


    bfp = argv[1];
    while(*bfp) {
        switch(*bfp) {
            case '>':
                bfp = get_reps(bfp, '>', &count);
                ptr = jit_inc_ptr(ptr, count);
                break;
            case '<':
                bfp = get_reps(bfp, '<', &count);
                ptr = jit_dec_ptr(ptr, count);
                break;
            case '+':
                bfp = get_reps(bfp, '+', &count);
                ptr = jit_inc(ptr, count);
                break;
            case '-':
                bfp = get_reps(bfp, '-', &count);
                ptr = jit_dec(ptr, count);
                break;
            case '[':
                //balanced_loop_unroll(bfp);
                ptr = jit_start_loop(ptr, brstack++);
                break;
            case ']':
                ptr = jit_end_loop(ptr, &closer);
                link_branches(--brstack, &closer);
                break;
            case '.':
                ptr = jit_put(ptr);
                break;
            case ',':
                ptr = jit_get(ptr);
                break;
            default:
                break;
        }
        bfp++;
    }
    *ptr++ = 0xc3; /* RETQ */
    //printf("%d\n", ptr - code);
    runcode(code, mem, (u8 *)&putchar, (u8 *)&getchar);
    munmap(code, 128*1024);
    free(mem);
    free(brstack);
}
Beispiel #4
0
/*

=item C<int main(int argc, char *argv[])>

Main compiler driver.

=cut

*/
int
main(int argc, char *argv[]) {
    char const * const program_name = argv[0];
    int                flexdebug    = 0;
    int                flags        = 0;
    int                execute      = 0;
    char              *filename     = NULL;
    char              *outputfile   = NULL;
    const char        *hdocoutfile  = NULL;
    unsigned           macrosize    = INIT_MACRO_SIZE;
    PARROT_INTERP                   = Parrot_new(NULL);

    /* skip program name */
    argc--;
    argv++;


    /* XXX test the parse_string() function. */
    /*
    parse_string(".sub main :main\nprint 42\n.end\n", LEXER_FLAG_OUTPUTPBC, 0, INIT_MACRO_SIZE);
    return 0;
    */


    /* XXX very basic argument handling; I'm too lazy to check out
     * the standard funtion for that, right now. This is a TODO. */
    while (argc > 0 && argv[0][0] == '-') {
        switch (argv[0][1]) {
            case 'b':
                SET_FLAG(flags, LEXER_FLAG_OUTPUTPBC);
                break;
            case 'E':
                SET_FLAG(flags, LEXER_FLAG_PREPROCESS);
                break;
            case 'f':
                flexdebug = 1;
                break;
            case 'h':
                print_help(program_name);
                exit(EXIT_SUCCESS); /* asking for help doesn't make you a failure */
                /* break; */
            case 'H':
                SET_FLAG(flags, LEXER_FLAG_HEREDOCONLY);
                break;
            case 'm':
                if (argc > 1) {
                    argc--;
                    argv++;
                    macrosize = atoi(argv[0]);
                }
                else {
                    fprintf(stderr, "Missing argument for option '-m'\n");
                    exit(EXIT_FAILURE);
                }
                break;
            case 'n':
                SET_FLAG(flags, LEXER_FLAG_NOOUTPUT);
                break;
            case 'o':
                if (argc > 1) { /* there must be at least 2 more args,
                                         the output file, and an input */
                    argc--;
                    argv++;
                    outputfile = argv[0];
                }
                else {
                    fprintf(stderr, "Missing argument for option '-o'\n");
                    exit(EXIT_FAILURE);
                }
                break;
            case 'p':
                SET_FLAG(flags, LEXER_FLAG_EMIT_PASM);
                break;
            case 'r':
                SET_FLAG(flags, LEXER_FLAG_REGALLOC);
                break;
            case 'S':
                SET_FLAG(flags, LEXER_FLAG_NOSTRENGTHREDUCTION);
                break;
            case 'v':
                SET_FLAG(flags, LEXER_FLAG_VERBOSE);
                break;
            case 'W':
                SET_FLAG(flags, LEXER_FLAG_WARNINGS);
                break;
            case 'x':
                execute = 1;
                break;
/* Only allow for debug flag if the generated parser supports it */
#ifdef YYDEBUG
            case 'y':
                yypirdebug = 1;
                break;
#endif
            default:
                fprintf(stderr, "Unknown option: '%c'\n", argv[0][1]);
                exit(EXIT_FAILURE);
        }
        /* goto next command line argument */
        argv++;
        argc--;
    }

/* The following code is to test thread safety. If TEST_THREAD_SAFETY
 * is false, no threads are started; only the main thread will do
 * a parse.
 * For thread safety testing, the pthreads library is used.
 */
#ifdef TEST_THREAD_SAFETY
{
    pthread_t threads[NUM_THREADS];
    int i;
    for (i = 0; i < NUM_THREADS; i++) {
        FILE *infile = NULL;
        parser_args args;


        if (argc < 1) { /* no file specified, read from stdin */
            infile   = stdin;
            filename = NULL;
        }
        else {
            /* done handling arguments, open the file */
            infile   = open_file(argv[0], "r");
            filename = argv[0];
        }

        if (infile == NULL) {
            fprintf(stderr, "Failed to open file '%s'\n", argv[0]);
            exit(EXIT_FAILURE);
        }

        args.flexdebug = flexdebug;
        args.infile    = infile;
        args.filename  = filename;
        args.thr_id    = i;
        args.flags     = flags;

        pthread_create(&threads[i], NULL, process_file, &args);

    }

    /* wait for all threads to finish */
    for (i = 0; i < NUM_THREADS; i++)
        pthread_join(threads[i], NULL);
}
#else
{
    /* non-thread testing code; this is the normal case */
    FILE *file = NULL;

    if (argc < 1) {
        fprintf(stderr, "pirc: no input specified\n");
        exit(EXIT_FAILURE);
    }

    if (outputfile != NULL && TEST_FLAG(flags, LEXER_FLAG_HEREDOCONLY)) {
        file = open_file(outputfile, "w");
        process_heredocs(interp, argv[0], file);
        fclose(file);
        return 0;
    }
    else if (TEST_FLAG(flags, LEXER_FLAG_HEREDOCONLY)) {
        process_heredocs(interp, argv[0], stdout);
        return 0;
    }
    else {
        hdocoutfile = "hdoctemp";
        file = open_file(hdocoutfile, "w");
        process_heredocs(interp, argv[0], file);
        fclose(file);
    }


    /* done handling arguments, open the file */
    file     = open_file(hdocoutfile, "r");
    filename = argv[0];

    if (file == NULL) {
        fprintf(stderr, "Failed to open file '%s'\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    parse_file(interp, flexdebug, file, filename, flags, 0, macrosize, outputfile);
/*
    fprintf(stderr, "done\n");
*/

    if (execute)
        runcode(interp, argc, argv);

}
#endif

    return 0;
}