-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_env_generator.py
More file actions
108 lines (101 loc) · 6.01 KB
/
runtime_env_generator.py
File metadata and controls
108 lines (101 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import sys
def extract_funcs():
# 备用函数,解析粘贴的cppreference文档中的标识符
lines=sys.stdin.readlines()
l=[]
for line in lines:
line=line.strip()
if not line:continue
if line[0].isascii():
for i in range(1,len(lines)+1):
if not line[:i].isidentifier():
size=i-1;break
else:
size=len(lines)
if size>0:l.append(line[:size])
print("funcs.extend(%s)"%repr(l))
funcs=[] # 在std的函数
direct_funcs=[] # 不在std,使用::访问的函数
typedefs={} # 类型定义
if sys.platform == "win32":
typedefs.update({
"memchr": "void* (*{})(const void*, int, size_t)",
"strchr": "char* (*{})(const char*, int)",
"strrchr": "char* (*{})(const char*, int)",
"strpbrk": "char* (*{})(const char*, const char*)",
"strstr": "char* (*{})(const char*, const char*)"
})
else:
typedefs.update({
"memchr": "const void* (*{})(const void*, int, size_t)",
"strchr": "const char* (*{})(const char*, int)",
"strrchr": "const char* (*{})(const char*, int)",
"strpbrk": "const char* (*{})(const char*, const char*)",
"strstr": "const char* (*{})(const char*, const char*)"
})
# 参考 cppreference.com
funcs.extend(['malloc', 'calloc', 'realloc', 'free'])
funcs.extend(['scanf', 'fscanf', 'sscanf', 'vscanf', 'vfscanf', 'vsscanf', 'printf', 'fprintf', 'sprintf', 'snprintf', 'vprintf', 'vfprintf', 'vsprintf', 'vsnprintf','perror'])
funcs.extend(['fgetc', 'getc', 'fgets', 'fputc', 'putc', 'fputs', 'getchar', 'putchar', 'puts', 'ungetc'])
direct_funcs.extend(['abs', 'labs', 'llabs', 'div', 'ldiv', 'lldiv', 'fabs', 'fabsf', 'fabsl', 'fmod', 'fmodf', 'fmodl', 'remainder', 'remainderf', 'remainderl', 'remquo', 'remquof', 'remquol', 'fma', 'fmaf', 'fmal', 'fmax', 'fmaxf', 'fmaxl', 'fmin', 'fminf', 'fminl', 'fdim', 'fdimf', 'fdiml', 'exp', 'expf', 'expl', 'exp2', 'exp2f', 'exp2l', 'expm1', 'expm1f', 'expm1l', 'log', 'logf', 'logl', 'log10', 'log10f', 'log10l', 'log2', 'log2f', 'log2l', 'log1p', 'log1pf', 'log1pl', 'pow', 'powf', 'powl', 'sqrt', 'sqrtf', 'sqrtl', 'cbrt', 'cbrtf', 'cbrtl', 'hypot', 'hypotf', 'hypotl', 'sin', 'sinf', 'sinl', 'cos', 'cosf', 'cosl', 'tan', 'tanf', 'tanl', 'asin', 'asinf', 'asinl', 'acos', 'acosf', 'acosl', 'atan', 'atanf', 'atanl', 'atan2', 'atan2f', 'atan2l', 'sinh', 'sinhf', 'sinhl', 'cosh', 'coshf', 'coshl', 'tanh', 'tanhf', 'tanhl', 'asinh', 'asinhf', 'asinhl', 'acosh', 'acoshf', 'acoshl', 'atanh', 'atanhf', 'atanhl', 'erf', 'erff', 'erfl', 'erfc', 'erfcf', 'erfcl', 'tgamma', 'tgammaf', 'tgammal', 'lgamma', 'lgammaf', 'lgammal', 'ceil', 'ceilf', 'ceill', 'floor', 'floorf', 'floorl', 'trunc', 'truncf', 'truncl', 'round', 'roundf', 'roundl', 'lround', 'lroundf', 'lroundl', 'llround', 'llroundf', 'llroundl', 'nearbyint', 'nearbyintf', 'nearbyintl', 'rint', 'rintf', 'rintl', 'lrint', 'lrintf', 'lrintl', 'llrint', 'llrintf', 'llrintl', 'frexp', 'frexpf', 'frexpl', 'ldexp', 'ldexpf', 'ldexpl', 'modf', 'modff', 'modfl', 'scalbn', 'scalbnf', 'scalbnl', 'scalbln', 'scalblnf', 'scalblnl', 'ilogb', 'ilogbf', 'ilogbl', 'logb', 'logbf', 'logbl', 'nextafter', 'nextafterf', 'nextafterl', 'nexttoward', 'nexttowardf', 'nexttowardl', 'copysign', 'copysignf', 'copysignl'])
funcs.extend(['atof', 'atoi', 'atol', 'atoll', 'strtol', 'strtoll', 'strtoul', 'strtoull', 'strtof', 'strtod', 'strtold', 'strcpy', 'strncpy', 'strcat', 'strncat', 'strxfrm', 'strlen', 'strcmp', 'strncmp', 'strcoll', 'strspn', 'strcspn', 'strtok', 'memcmp', 'memset', 'memcpy', 'memmove', 'strerror'])
direct_funcs.extend(['strdup', 'strchr', 'strrchr', 'strpbrk', 'strstr', 'memchr', 'memccpy'])
funcs.extend(['isalnum', 'isalpha', 'islower', 'isupper', 'isdigit', 'isxdigit', 'iscntrl', 'isgraph', 'isspace', 'isblank', 'isprint', 'ispunct', 'tolower', 'toupper'])
funcs.extend(['fopen', 'freopen', 'fclose', 'fflush', 'setbuf', 'setvbuf', 'fread', 'fwrite'])
funcs.extend(['difftime', 'time', 'clock', 'asctime', 'ctime', 'strftime', 'gmtime', 'localtime', 'mktime'])
funcs.extend(['system','exit','raise','signal','longjmp'])
direct_funcs.extend(['access', 'chdir', 'getcwd', 'mkdir', 'rmdir', 'rename', 'unlink', 'close', 'dup', 'dup2', 'read', 'write', 'execve', 'getpid', 'sleep', 'usleep', 'getenv', 'isatty', 'getopt', 'ftruncate', 'lseek']) # windows可用的部分unistd.h函数
TAB=" "*4
with open("runtime_env.h","w",encoding="utf-8") as f:
print(f"""\
// Generated by {__file__}, do NOT edit
#pragma once
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>
#include <csignal>
#include <csetjmp>
#include <cerrno>
#include <unistd.h>
#ifndef _WIN32
#include <sys/stat.h>
#endif
#include "constants.h"
struct __attribute__((aligned(8))) RuntimeEnv {{
RuntimeVersion version;
int platform;
void* (*getFunc)(const char *);
int (*import)(const char *);
void* (*getLibraryFunc)(const char *,const char *);
void (*freeLibrary)(const char *);
void (*debugModuleInfo)();
FILE* (*getstdin)();
FILE* (*getstdout)();
FILE* (*getstderr)();
void (*stackTrace)();
void (*abort)();
int geterrno(){{return errno;}}
void seterrno(int id){{errno = id;}}\
""",file=f)
for func in funcs:
print(TAB+f"decltype(std::{func}) *{func};",file=f)
for func in direct_funcs:
if func in typedefs:
print(TAB+typedefs[func].format(func)+";",file=f)
else:
print(TAB+f"decltype(::{func}) *{func};",file=f)
print("""\
RuntimeEnv(){\n"""+TAB*2,end="",file=f)
print(("\n"+TAB*2).join(f"{func}=std::{func};" for func in funcs),file=f)
print(TAB*2,end="",file=f)
print(("\n"+TAB*2).join(f"{func}=::{func};" for func in direct_funcs),file=f)
print(TAB+"}",file=f)
print("""\
};
#include <string>
#include <unordered_map>
#include <utility>
static std::unordered_map<std::string,std::pair<void *,size_t>> imported_funcs;""",file=f)