Example #1
0
void
gen_and_load_llvm_code (mathmap_t *mathmap, char *template_filename, filter_code_t **filter_codes)
{
    MemoryBuffer *buffer = MemoryBuffer::getFile(template_filename, NULL);

    g_assert(buffer != NULL);

    Module *module = ParseBitcodeFile (buffer, NULL);
    int i;
    filter_t *filter;

    assert(module != NULL);
    delete buffer;

    for (i = 0, filter = mathmap->filters;
	 filter != 0;
	 ++i, filter = filter->next)
    {
	if (filter->kind != FILTER_MATHMAP)
	    continue;

	make_init_frame_function(module, filter);
	make_init_x_or_y_function(module, filter, init_x_function_name(filter));
	make_init_x_or_y_function(module, filter, init_y_function_name(filter));
	make_main_filter_function(module, filter);
	make_filter_function(module, filter);
    }

    for (i = 0, filter = mathmap->filters;
	 filter != 0;
	 ++i, filter = filter->next)
    {
	filter_code_t *code = filter_codes[i];
	code_emitter *emitter;

	if (filter->kind != FILTER_MATHMAP)
	    continue;

	g_assert(code->filter == filter);

	emitter = new code_emitter (module, filter, code);
	try
	{
	    emitter->emit_init_frame_function();
	    emitter->emit_filter_function();
	    emitter->emit_main_filter_funcs();
	}
	catch (compiler_error error)
	{
	    delete emitter;
	    delete module;

	    strcpy(error_string, error.info.c_str());
	    return;
	}
	delete emitter;
    }

    PassManager pm;

    pm.add(new TargetData(module));
    pm.add(createFunctionInliningPass());
    pm.add(createInstructionCombiningPass());
    pm.add(createReassociatePass());
    pm.add(createLowerSetJmpPass());
    pm.add(createRaiseAllocationsPass());
    pm.add(createGVNPass());
    pm.add(createCFGSimplificationPass());
    pm.add(createPromoteMemoryToRegisterPass());
    pm.add(createGlobalOptimizerPass());
    pm.add(createGlobalDCEPass());

    pm.run(*module);

#ifdef DEBUG_OUTPUT
    module->dump();

    verifyModule(*module, PrintMessageAction);
#endif

    ExecutionEngine *ee = ExecutionEngine::create (module);

    ee->InstallLazyFunctionCreator(lazy_creator);

    module_info_t *module_info = g_new0(module_info_t, 1);

    module_info->ee = ee;

    void *init_frame_fptr = ee->getPointerToFunction(lookup_init_frame_function(module, mathmap->main_filter));
    void *main_filter_fptr = ee->getPointerToFunction(lookup_main_filter_function(module, mathmap->main_filter));
    void *init_x_fptr = ee->getPointerToFunction(lookup_init_x_function(module, mathmap->main_filter));
    void *init_y_fptr = ee->getPointerToFunction(lookup_init_y_function(module, mathmap->main_filter));
    g_assert(main_filter_fptr && init_x_fptr && init_y_fptr);

    module_info->mathfuncs.llvm_init_frame_func = (llvm_init_frame_func_t)init_frame_fptr;
    module_info->mathfuncs.main_filter_func = (llvm_filter_func_t)main_filter_fptr;
    module_info->mathfuncs.init_x_func = (init_x_or_y_func_t)init_x_fptr;
    module_info->mathfuncs.init_y_func = (init_x_or_y_func_t)init_y_fptr;

    mathmap->module_info = module_info;

    mathmap->mathfuncs = &module_info->mathfuncs;
}