#include <stdio.h>
#define uint unsigned int
#define AddressOfLabel(sectionname,out) __asm{mov [out],offset sectionname};
void* CreateFunction(void* start,void *end) {
uint __start=(uint)start,__end=(uint)end-1
,size,__func_runtime;
void* func_runtime=malloc(size=(((__end)-(__start)))+1);
__func_runtime=(uint)func_runtime;
memcpy((void*)(__func_runtime),start,size);
((char*)func_runtime)[size]=0xC3; //ret
return func_runtime;
}
void CallRuntimeFunction(void* address) {
__asm {
call address
}
}
main() {
void* _start,*_end;
AddressOfLabel(__start,_start);
AddressOfLabel(__end,_end);
void* func = CreateFunction(_start,_end);
CallRuntimeFunction(func); //I expected this method to print "Test"
//but this method raised exception
return 0;
__start:
printf("Test");
__end:
}
Run Code Online (Sandbox Code Playgroud)
CreateFunction- 在内存中占两点(函数范围),分配,将其复制到分配的内存并返回它(void*使用像函数一样调用Assembly)
CallRuntimeFunction - 运行返回的函数 CreateFunction
#define AddressOfLabel(sectionname,out) - 将标签(sectionname)的地址输出到变量(out) …
我正在创建脚本语言.当我分配东西时,它会分配东西并返回地址然后我对它做任何事情然后删除它.我无法控制变量,比如在我的lang中创建struct(使用指针和bool来检查指针是否指向有效数据)等等因为它会让我的RAM在RAM中变得更慢更大.
例如:(我的脚本语言很容易被理解.我怀疑你不会理解这一点,但无论如何我会在其中加入一些评论)
MyStruct = { //Function. For create object with it use 'new' before it.
TestAliveVar=0
}
Func = { //I'll explain what exactly this function does every place it runs.
if (!exists(arg0)) //C++: ???
exit;
arg0.TestAliveVar=1
println "Still alive!";
}
var MyVar=new MyStruct(); //Returns address of the new object in the heap
//and runs on it the `MyStruct` function.
Func(MyVar); //Sets his 'TestAliveVar' to 1
//and prints 'Still Alive!' with new line
delete(MyVar); //C++: free(MyVar);
Func(MyVar); //Does nothing
Run Code Online (Sandbox Code Playgroud)
问题是如何创建 …
如何在启用优化的情况下编译我的项目,并查看我的代码中更改了哪些优化.
例如:
我原来的代码:
printf("Test: %d",52);
for (int empty=0;i<100000;i++) {
//Nothing here
}
Run Code Online (Sandbox Code Playgroud)
现在,当我使用optimzations编译我的代码时,我想看到:(我认为它会像那样)
printf("Test: 52");
Run Code Online (Sandbox Code Playgroud)