ski::node eval(ski::node const& n_orig) { arg_vector args; auto n = n_orig; while (true) { warn(n_orig); while (auto* app = n.get<ski::application>()) { args.push_back(n); warn("back:", args.back()); n = app->f; } bool id = is_identity(n); auto result = boost::apply_visitor(do_evaluate(args), n); if (!result) break; if (args.empty()) n = n_orig.update(*result); else if (id) n = get_fun(args.back()) = *result; else if (result->get<ski::application>()) n = get_fun(args.back()).update(*result); else n = get_fun(args.back()).update(ski::application{ ski::combinator{'I'}, *result}); } while (!args.empty()) n = ski::application{n, get_arg(extract(args))}; return n; }
LLVMValueRef genfun_proto(compile_t* c, gentype_t* g, const char *name, ast_t* typeargs) { ast_t* fun = get_fun(g, name, typeargs); LLVMValueRef func = get_prototype(c, g, name, typeargs, fun); // Disable debugloc on calls to methods that have no debug info. if(!ast_debug(fun)) dwarf_location(&c->dwarf, NULL); switch(ast_id(fun)) { case TK_NEW: case TK_BE: if(g->underlying == TK_ACTOR) { const char* fun_name = genname_fun(g->type_name, name, typeargs); const char* be_name = genname_be(fun_name); func = LLVMGetNamedFunction(c->module, be_name); } break; default: {} } ast_free_unattached(fun); return func; }
CK_FUNCTION_LIST *pkcs11_get_function_list(const char *param) { CK_FUNCTION_LIST *funcs; CK_RV (*get_fun)(); void *d; const char *e = param ? param : getenv("PKCS11_LIBRARY"); e = e ? e : DEFAULT_PKCSLIB; d = dlopen(e, RTLD_LAZY); if (d == NULL) { fprintf(stdout, "dlopen('%s') failed\n", e); return NULL; } *(void **) (&get_fun) = dlsym(d, "C_GetFunctionList"); if (get_fun == NULL) { fprintf(stdout, "Symbol lookup failed\n"); return NULL; } CK_RV rc = get_fun(&funcs); if (rc != CKR_OK) { show_error(stdout, "C_GetFunctionList", rc); funcs = NULL; } else if(funcs == NULL) { fprintf(stdout, "C_GetFunctionList returned empty value\n"); } return funcs; }
static void print_method(compile_t* c, printbuf_t* buf, reachable_type_t* t, const char* name, ast_t* typeargs) { const char* funname = genname_fun(t->name, name, typeargs); LLVMValueRef func = LLVMGetNamedFunction(c->module, funname); if(func == NULL) return; // Get a reified function. ast_t* fun = get_fun(t->ast, name, typeargs); if(fun == NULL) return; AST_GET_CHILDREN(fun, cap, id, typeparams, params, rtype, can_error, body, docstring); // Print the docstring if we have one. if(ast_id(docstring) == TK_STRING) { printbuf(buf, "/*\n" "%s" "*/\n", ast_name(docstring) ); } // Print the function signature. print_type_name(c, buf, rtype); printbuf(buf, " %s", funname); switch(ast_id(fun)) { case TK_NEW: case TK_BE: { ast_t* def = (ast_t*)ast_data(t->ast); if(ast_id(def) == TK_ACTOR) printbuf(buf, "__send"); break; } default: {} } printbuf(buf, "("); print_type_name(c, buf, t->ast); printbuf(buf, " self"); print_params(c, buf, params); printbuf(buf, ");\n\n"); ast_free_unattached(fun); }
static LLVMValueRef genfun_newbe(compile_t* c, gentype_t* g, const char *name, ast_t* typeargs) { ast_t* fun = get_fun(g, name, typeargs); LLVMValueRef func = get_prototype(c, g, name, typeargs, fun); if(func == NULL) { ast_free_unattached(fun); return NULL; } codegen_startfun(c, func, ast_debug(fun)); name_params(c, g->ast, ast_childidx(fun, 3), func); genfun_dwarf(c, g, name, typeargs, fun); if(!gen_field_init(c, g)) { ast_free_unattached(fun); return NULL; } ast_t* body = ast_childidx(fun, 6); LLVMValueRef value = gen_expr(c, body); if(value == NULL) { ast_free_unattached(fun); return NULL; } LLVMBuildRetVoid(c->builder); codegen_finishfun(c); // Generate the sender. LLVMValueRef sender = get_sender(c, g, name, typeargs); codegen_startfun(c, sender, false); LLVMValueRef this_ptr = LLVMGetParam(sender, 0); // Send the arguments in a message to 'this'. uint32_t index = genfun_vtable_index(c, g, name, typeargs); LLVMTypeRef msg_type_ptr = send_message(c, fun, this_ptr, sender, index); genfun_dwarf_return(c, body); // Return 'this'. LLVMBuildRet(c->builder, this_ptr); codegen_finishfun(c); // Add the dispatch case. add_dispatch_case(c, g, fun, index, func, msg_type_ptr); ast_free_unattached(fun); return func; }
LLVMTypeRef genfun_sig(compile_t* c, gentype_t* g, const char *name, ast_t* typeargs) { // If the function already exists, return its type. const char* funname = genname_fun(g->type_name, name, typeargs); LLVMValueRef func = LLVMGetNamedFunction(c->module, funname); if(func != NULL) return LLVMGetElementType(LLVMTypeOf(func)); ast_t* fun = get_fun(g, name, typeargs); LLVMTypeRef type = get_signature(c, g, fun); ast_free_unattached(fun); return type; }
TEST(AutoTests, BasicTest) { auto a = 1 + 2; LOG_INFO("type of a: %s", typeid(a).name()); auto b = add(1, 1.2); LOG_INFO("type of b: %s", typeid(b).name()); auto d = {1, 2}; LOG_INFO("type of d: %s", typeid(d).name()); auto my_lambda = [](int x) { return x + 3; }; LOG_INFO("my_lambda: %d", my_lambda(5)); auto my_fun = get_fun(2); LOG_INFO("type of my_fun: %s", typeid(my_fun).name()); LOG_INFO("my_fun: %lf", my_fun(3)); }
static LLVMValueRef genfun_new(compile_t* c, gentype_t* g, const char *name, ast_t* typeargs) { ast_t* fun = get_fun(g, name, typeargs); LLVMValueRef func = get_prototype(c, g, name, typeargs, fun); if(func == NULL) { ast_free_unattached(fun); return NULL; } if(LLVMCountBasicBlocks(func) != 0) { ast_free_unattached(fun); return func; } codegen_startfun(c, func, ast_debug(fun)); name_params(c, g->ast, ast_childidx(fun, 3), func); genfun_dwarf(c, g, name, typeargs, fun); if(!gen_field_init(c, g)) { ast_free_unattached(fun); return NULL; } ast_t* body = ast_childidx(fun, 6); LLVMValueRef value = gen_expr(c, body); if(value == NULL) { ast_free_unattached(fun); return NULL; } genfun_dwarf_return(c, body); // Return 'this'. LLVMBuildRet(c->builder, LLVMGetParam(func, 0)); codegen_finishfun(c); ast_free_unattached(fun); return func; }
int main() { auto a = 1 + 2; std::cout << "type of a: " << typeid(a).name() << '\n'; auto b = add(1, 1.2); std::cout << "type of b: " << typeid(b).name() << '\n'; //auto int c; //compile-time error auto d = {1, 2}; std::cout << "type of d: " << typeid(d).name() << '\n'; auto my_lambda = [](int x) { return x + 3; }; std::cout << "my_lambda: " << my_lambda(5) << '\n'; auto my_fun = get_fun(2); std::cout << "type of my_fun: " << typeid(my_fun).name() << '\n'; std::cout << "my_fun: " << my_fun(3) << '\n'; }
CK_FUNCTION_LIST *pkcs11_get_function_list(const char *param) { CK_FUNCTION_LIST *funcs; void *d; const char *e = param ? param : getenv("PKCS11_LIBRARY"); e = e ? e : DEFAULT_PKCSLIB; d = LoadLibrary(e); if (d == NULL) { fprintf(stdout, "LoadLibrary Failed (%s)\n", e); return NULL; } #ifndef USE_GET_FUNCTION_LIST /* Look-up all symbols from dll */ funcs = (CK_FUNCTION_LIST_PTR) malloc(sizeof(CK_FUNCTION_LIST)); if(funcs) { #undef CK_NEED_ARG_LIST #undef CK_PKCS11_FUNCTION_INFO #define CK_PKCS11_FUNCTION_INFO(name) \ if((funcs->name = (CK_RV (*)())GetProcAddress(d, #name)) == NULL) { \ fprintf(stdout, "Error looking up %s\n", #name); \ free(funcs); \ return NULL; \ } #include "pkcs11f.h" } #else /* Look-up C_GetFunctionList and use it to get all functions */ CK_RV (*get_fun)(); get_fun = (CK_RV (*)())GetProcAddress(d, "C_GetFunctionList"); if (get_fun == NULL) { fprintf(stdout, "Symbol lookup failed\n"); return NULL; } CK_RV rc = get_fun(&funcs); if (rc != CKR_OK) { show_error(stdout, "C_GetFunctionList", rc); funcs = NULL; } else if(funcs == NULL) { fprintf(stdout, "C_GetFunctionList returned empty value\n"); } #endif return funcs; }
static LLVMValueRef genfun_fun(compile_t* c, gentype_t* g, const char *name, ast_t* typeargs) { ast_t* fun = get_fun(g, name, typeargs); LLVMValueRef func = get_prototype(c, g, name, typeargs, fun); if(func == NULL) { ast_free_unattached(fun); return NULL; } if(LLVMCountBasicBlocks(func) != 0) { ast_free_unattached(fun); return func; } codegen_startfun(c, func, ast_debug(fun)); name_params(c, g->ast, ast_childidx(fun, 3), func); genfun_dwarf(c, g, name, typeargs, fun); ast_t* body = ast_childidx(fun, 6); LLVMValueRef value = gen_expr(c, body); if(value == NULL) { ast_free_unattached(fun); return NULL; } else if(value != GEN_NOVALUE) { genfun_dwarf_return(c, body); LLVMTypeRef f_type = LLVMGetElementType(LLVMTypeOf(func)); LLVMTypeRef r_type = LLVMGetReturnType(f_type); LLVMValueRef ret = gen_assign_cast(c, r_type, value, ast_type(body)); LLVMBuildRet(c->builder, ret); } codegen_finishfun(c); ast_free_unattached(fun); return func; }
int test_auto1() { auto a = 1 + 2; std::cout << "type of a: " << typeid(a).name() << '\n'; // type of a: int auto b = add(1, 1.2); std::cout << "type of b: " << typeid(b).name() << '\n'; // type of b: double auto c = { 1, 2 }; std::cout << "type of c: " << typeid(c).name() << '\n'; // type of c: class std::initializer_list<int> auto my_lambda = [](int x) { return x + 3; }; std::cout << "my_lambda: " << my_lambda(5) << '\n'; // my_lambda: 8 auto my_fun = get_fun(2); std::cout << "type of my_fun: " << typeid(my_fun).name() << '\n'; // type of my_fun: double (__cdecl*)(double) std::cout << "my_fun: " << my_fun(3) << '\n'; // my_fun: 0.14112 // auto int x; // error as of C++11: "auto" is no longer a storage-class specifier // error C3530: “auto”不能与任何其他类型说明符组合 return 0; }
static int cmd_bt(char *args){ if(args != NULL) printf("(there is no need to input any arguments)\n"); uint32_t tmp = cpu.ebp; uint32_t addr = cpu.eip; char name[32]; int i = 0, j; while(get_fun(addr, name)){ name[31] = '\0'; printf("#%02d %08x in %s(",i++, addr, name); for(j = 2; j < 6; ++j){ if(tmp + j * 4 > 0 && tmp + j * 4 < 0x8000000) printf("%d, ", swaddr_read(tmp + j*4, 4, R_SS)); } if(tmp + j * 4 > 0 && tmp + j * 4 < 0x8000000) printf("%d", swaddr_read(tmp + j * 4, 4, R_SS)); printf(")\n"); addr = swaddr_read(tmp + 4, 4, R_SS); tmp = swaddr_read(tmp, 4, R_SS); } return 0; }
short main (short argc, char *argv[]) { unsigned short status=0; /* TX and RX status */ unsigned short tries; /* Attempts to send a file */ unsigned short cmp_size; /* Size after compression */ unsigned short data_written; /* Data written to the file */ unsigned short data_read; /* Data read from the file */ char *file_name; /* filename */ char *function; /* Receive, Transmit */ char *com_port; /* Communications adapter port */ file_name = get_inp (argc, argv); /* Get file name */ if (file_name == NULL ) { disp(); /* Display usage message */ return JM_FNF; } function = get_fun (argc, argv); /* Get function 'R' or 'S' */ if (function == NULL) { disp(); /* Display usage message */ return JM_CMD; } com_port = get_prt (argc, argv); /* Get port '1 to 4 ' */ if (com_port == NULL) { disp(); /* Display usage message */ return JM_CMD; } port = get_port(*com_port); /* Convert port to an offset */ /****************************************************************************/ /* Allocate buffers */ /****************************************************************************/ in_buffer = allocate_memory(DAT_LEN); /* Get some memory for input */ if (in_buffer == NULL) return JM_MEM; /* No memory available */ out_buffer = allocate_memory(DAT_LEN); /* Get some memory for output */ if (out_buffer == NULL) return JM_MEM; /* No memory available */ comp_buffer=allocate_memory(DAT_LEN); /* Get memory for compression */ if (comp_buffer == NULL) return JM_MEM; /* No memory available */ file_buffer=allocate_memory(DAT_LEN); /* Get memory for file buffer */ if (file_buffer == NULL) return JM_MEM; /* No memory available */ int_buffer =allocate_memory(DAT_LEN); /* Memory for interrupt buffer */ if (int_buffer == NULL) return JM_MEM; /* No memory available */ /****************************************************************************/ screen (SCR_SGN,NULL,NULL); /* Write signon screen */ syst.s_len = BLK_SIZ; syst.s_byt = 0; syst.s_blk = 0; syst.s_sta = okay; switch(*function) /* Functions are TX and RX */ { /****************************************************************************/ /* Receive JMODEM file */ /****************************************************************************/ case 'R': { if (!file_io(CREATE, &handle, &file_name, NULL) ) { buff = (JBUF *) in_buffer; /* Assign type JBUF */ open_chan(port); /* Open com channel */ screen (SCR_STA,NULL,NULL); /* Write status block */ status = rx_sync(); /* Synchronize */ if (!status) screen (SCR_SYR,NULL,NULL); data_written = 0xFFFF; tries = 10; /* Attempts to receive */ while ( (data_written) /* Write file okay */ && (!user_abort ) /* No break key */ && (!status ) /* Recev block okay */ && (tries--) ) /* 10 retries */ { time(&start); /* Get starting time */ screen (SCR_SYS,&syst,NULL); /* Show status block */ status = recv_blk ( /* Receive data-block*/ &syst.s_len, /* Block length */ in_buffer); /* Input buffer */ if (status) /* If bad */ break; /* Abort the WHILE */ if( (!(calc_crc(GET_CRC, /* Calculate CRC */ syst.s_len, /* Amount to check */ in_buffer) )) /* Receiver buffer */ && ( buff->blk_num == /* Check block also */ (unsigned char) (syst.s_blk +1))) /* Block number */ { syst.s_sta = okay; /* Text pointer */ tries=10; /* Reset count */ syst.s_len -= OVRHD; /* Subtract overhead */ *out_buffer = ACK; /* Good */ write_chan(1,out_buffer); /* Send the ACK */ /* If data was compressed */ if ( (buff->blk_typ & COMP) == COMP) { syst.s_len = decode ( /* Decode the data */ syst.s_len, /* Data-block length */ &buff->blk_dat, /* Where to start */ file_buffer); /* Where to put data */ } else /* Data was normal (not compressed, just copy ) */ { memcpy (file_buffer,&buff->blk_dat,syst.s_len); } /* Write to the file */ data_written = file_io( WRITE , /* Function */ &handle, /* File handle */ &file_buffer , /* Where data is */ syst.s_len ); /* Amount to write */ syst.s_byt += data_written; /* Total bytes */ syst.s_blk++; /* Block number */ time(&finish); /* Get end time */ if (finish - start) /* Check div/0 */ syst.s_cps = (short) /* Calc Block CPS */ (data_written / (finish - start) ); /* Check for end-of-file */ if ( (buff->blk_typ & EOF_) == EOF_) { /* This was the end of file */ file_io(CLOSE, /* Function */ &handle, /* Open handle */ &file_name, /* Name not used */ NULL); /* Buffer not used */ close_chan(port); /* Close the port */ status = JM_NRM; /* Set status */ goto cleanup; /* exit routine */ } } else { *out_buffer = NAK; /* Bad block */ syst.s_sta = retry; /* Char pointer */ write_chan(1,out_buffer); /* Send the NAK */ } } close_chan(port); /* Aborted */ file_io(DELETE, /* Function */ &handle, /* File handle */ &file_name, /* Name */ NULL); /* Buffer not used */ status = JM_ABT; break; /* Exit if() {} */ } else /* Can't create file */ { status = JM_CRE; break; /* Exit while() {} */ } break; /* Exit case 'R' */ } /****************************************************************************/ /* Send JMODEM file */ /****************************************************************************/ case 'S': /* Send JMODEM file */ { if (!file_io(OPEN_READ, &handle, &file_name, NULL) ) { buff = (JBUF *)out_buffer; /* Assign type JBUF */ syst.s_byt = 0; /* Restore byte count */ open_chan(port); /* Open COM port */ data_read = 0xFFFF; /* Initialize */ screen (SCR_STA,NULL,NULL); /* Write status block */ status = tx_sync(); /* Synchronize */ if (!status) screen (SCR_SYT,NULL,NULL); while ( (!user_abort) /* Ctrl - break */ && (!status) ) /* sent okay */ { time(&start); /* Get starting time */ data_read = file_io( READ , /* Read a record */ &handle , /* File pointer */ &file_buffer , /* Where to put */ syst.s_len ); /* Amount to read */ if (!data_read) /* Past end of file */ break; syst.s_byt += (long) data_read; /* Running count */ screen (SCR_SYS,&syst,NULL); /* Show status block */ buff->blk_num = (unsigned char) ++syst.s_blk; /* Block number */ if (data_read != syst.s_len) /* Byte request */ buff->blk_typ = EOF_; /* Into control-byte */ else buff->blk_typ = NORM; /* Normal block */ cmp_size = encode (data_read, /* Encode size */ file_buffer, /* Source */ comp_buffer); /* Destination */ if ( cmp_size < data_read ) /* If compressed */ { buff->len = (cmp_size+OVRHD); /* Length of block */ buff->blk_typ |= COMP; /* Show compressed */ memcpy (&buff->blk_dat, /* Start of data */ comp_buffer, /* Copy from here */ cmp_size); /* This much */ } else /* Not compressed */ { buff->len = (data_read+OVRHD);/* Length of block */ memcpy (&buff->blk_dat, /* Copy to */ file_buffer, /* Copy from */ data_read); /* This amount */ } calc_crc(SET_CRC, /* Calculate CRC */ buff->len , /* Length of block */ out_buffer); /* Where data is */ status = send_blk( /* Send the block */ buff->len, /* Block length */ &syst, /* Read block ptr. */ out_buffer); /* Buffer pointer */ time(&finish); /* Get end time */ if (finish - start) /* Guard div/zero */ syst.s_cps = (short) /* Calc Block CPS */ (data_read / (finish - start) ); if ( buff->blk_typ == EOF_) /* Last record */ break; } close_chan(port); /* Close the port */ if (status) syst.s_sta = abrt; /* A text pointer */ else syst.s_sta = done; /* A text pointer */ file_io(CLOSE, &handle, &file_name, NULL); /* Close the file */ screen (SCR_SYS,&syst,NULL); /* Show status block */ } else /* File not found */ { status = JM_FNF; } break; /* End of CASE 'S' */ } } cleanup: free (in_buffer); /* Free buffers */ free (out_buffer); free (comp_buffer); free (file_buffer); /* Five-second timer to display error messages */ if (status != JM_NRM) { time(&finish); start = 0; finish += 5; while ( finish > start ) time(&start); } screen (SCR_END,NULL,NULL); /* Clear the screen */ return status; /* Normal exit */ }
Data* parse_factor( char** p) { Data* ret; int curves = 0; while(1) { parse_ws(p); if( **p == '(' ) curves ++; else break; (*p)++; break; } if( curves ) {/* (((...(( parsed , expression expected */ ret = parse_expression(p); if( ret->type == TYPE_ERROR ) {/* error occured*/ return ret; } while(curves--) { /*check ')'*/ parse_ws(p); if( **p != ')' ) {/*syntax error , curves dose not match*/ dispose(ret); return Raise(SYNTAX); } (*p)++; } } else {/* Constant , Variable , Array and Functions*/ /* INKEY$*/ parse_ws(p); if( **p == '"' ) ret = parse_string(p); else if(ISNUMERIC(**p) || **p == '.') ret = parse_numeric(p); else if( ISALPHA(**p) || **p == '_' ) { char* id = parse_id(p); parse_ws(p); if(**p == '(' ) { Subscript *arg; Function* f; arg = parse_arg(p); f = get_fun(id); if( f == NULL ) return Raise(SYNTAX); if( arg == NULL ) return Raise(SYNTAX); if( arg->dimension[arg->dimensions-1]->type == TYPE_ERROR ) { ret = Data_new( TYPE_ERROR , (Storage)arg->dimension[--arg->dimensions]->storage, 1); free(arg); } else { if( f->is_function ) {/*function call*/ ret = f->f.foo( arg ); } else {/*array*/ ret = get_arr_element( &f->f.arr , arg ); } dispose_arg(arg); } } else { /*variable*/ if( strcmp( id , "INKEY$") == 0 ) { ret = Data_new( TYPE_STRING , (Storage)(char*)malloc(2) , 1 ); ret->storage.String[0] = inkey(); ret->storage.String[1] = 0; } else if( strcmp( id , "__LASTERR$" ) == 0 ) { ret = Data_new( TYPE_STRING , (Storage)(char*)malloc(strlen(err_str[Last_Error.id])) , 1 ); strcpy( ret->storage.String , err_str[Last_Error.id] ); } else if( strcmp( id, "__LASTERRNO") == 0 ) { ret = Data_new( TYPE_REAL , (Storage)(double)Last_Error.id,1); } else ret = get_var(id); } free(id); } else { ret =Raise(SYNTAX); } } return ret; }