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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/*
* Create strapping code for a list of symbols in pike.so,
* and the pike headerfiles.
*
* Henrik Grubbstrm 2005-01-03
*/
constant default_headers = ({
"global.h",
"pike_embed.h",
});
class C_Include_Handler(array(string) search_path)
{
string handle_include(string f, string current_file, int local_include)
{
if (!local_include) {
// Ignore system header files.
return "";
} else {
foreach(search_path, string path) {
path = combine_path(path, f);
if (file_stat(path)) {
return path;
}
}
}
return 0; // Header not found.
}
string read_include(string f)
{
if (!sizeof(f)) return ""; // System header.
return Stdio.read_file(f);
}
}
//! Returns a mapping from symbol to a tuple of return type and parameters.
mapping(string:array(array(Parser.C.Token))) parse(array(Parser.C.Token) tokens)
{
mapping(string:array(array(Parser.C.Token))) res = ([]);
int i;
int start;
for (start=i=0; i < sizeof(tokens); i++) {
array(Parser.C.Token)|object(Parser.C.Token) token = tokens[i];
if ((objectp(token)) && (token->text == ";")) {
if (i > start + 2) {
if (objectp(tokens[start]) && (tokens[start]->text == "typedef")) {
// Ignore.
} else {
array(Parser.C.Token)|Parser.C.Token last;
if (arrayp(last = tokens[i-1]) && sizeof(last)) {
if (last[0]->text == "(") {
// Function declaration.
res[(string)tokens[i-2]] = ({
tokens[start..i-3],
last,
});
} else {
// { : possible inline function, but probably an struct or enum.
// [ : array declaration.
// Ignored for now.
}
}
}
} else {
// Too few tokens to be a function.
}
start = i+1;
}
}
return res;
}
int main(int argc, array(string) argv)
{
array(string) headers = default_headers;
array(string) srcdirs = ({ "." });
array(string) symbols = ({ });
string out_name;
foreach(Getopt.find_all_options(argv, ({
({ "help", Getopt.NO_ARG, ({ "-h", "--help" }) }),
({ "header", Getopt.HAS_ARG, ({ "-i", "--include" }) }),
({ "dir", Getopt.HAS_ARG, ({ "-I", "--include-dir" }) }),
({ "out", Getopt.HAS_ARG, ({ "-o", "--out" }) }),
({ "sym", Getopt.HAS_ARG, ({ "-s", "--sym" }) }),
({ "sym-file", Getopt.HAS_ARG, ({ "-S", "--sym-file" }) }),
}), 1), [string key, string val]) {
switch(key) {
case "help":
// -h Help.
werror("Usage:\n"
" %s [ -I <include directory> ... ] \\\n"
" [ -i <header-file> ... ] \\\n"
" [ -s <symbol> ... ] \\\n"
" [ -S <symbol-file> ... ] \\\n"
" [ -o <output-file> ]\n",
argv[0]);
exit(0);
break;
case "header":
// -i Add include file.
headers += ({ val });
break;
case "dir":
// -I Add include directory.
srcdirs += ({ val });
break;
case "out":
// -o Specify output filename.
if (out_name) {
werror("Output file specified multiple times.\n");
exit(1);
}
out_name = val;
break;
case "sym":
// -s Add single symbol.
symbols += ({ val });
break;
case "sym-file":
// -S Specify symbol file.
string raw_syms = Stdio.read_file(val);
if (!raw_syms) {
werror("Failed to open symbol file %O.\n", val);
exit(1);
}
symbols += String.trim_whites((replace(raw_syms, "\r", "")/"\n")[*]) -
({""});
break;
}
}
argv = Getopt.get_args(argv, 1);
if (sizeof(argv) != 1) {
werror("Too many arguments to %s.\n", argv[0]);
exit(1);
}
string raw = cpp(sprintf("%{#include \"%s\"\n%}", headers), "include_all.c",
0,
C_Include_Handler(srcdirs));
array(Parser.C.Token|array) tokens =
Parser.C.strip_line_statements(
Parser.C.hide_whitespaces(
Parser.C.group(Parser.C.tokenize(Parser.C.split(raw)))
)
);
mapping(string:array(array(Parser.C.Token))) symbol_info = parse(tokens);
Stdio.File out = Stdio.stdout;
if (out_name) {
out = Stdio.File();
if (!out->open(out_name, "wct")) {
werror("Failed to open %O for writing.\n", out_name);
exit(1);
}
}
out->write("/*\n"
" * Automatically generated by " __FILE__ "\n"
" * on " __DATE__ ". Do NOT edit.\n"
" */\n"
"\n"
"%{#include \"%s\"\n%}\n"
"\n",
headers);
int fail;
foreach(Array.uniq(sort(symbols)), string sym) {
array(array(Parser.C.Token)) info = symbol_info[sym];
if (!info) {
werror("Symbol %O not found!\n", sym);
fail = 1;
continue;
}
string rettype = String.trim_whites(Parser.C.simple_reconstitute(info[0]));
string params = Parser.C.simple_reconstitute(info[1]);
out->write("protected %s (*vec_%s)%s;\n"
"%s %s%s\n"
"{\n"
// FIXME: Code to resolve the symbol here.
" %svec_%s(",
rettype, sym, params,
rettype, sym, params,
(rettype=="void")?"":"return ", sym);
array(array(Parser.C.Token)) args = info[1][1..sizeof(info[1])-2]/({","});
foreach(args; int n; array(Parser.C.Token) arg) {
if (sizeof(arg) == 1) continue; // probable void.
array(Parser.C.Token)|Parser.C.Token name = arg[-1];
if (arrayp(name)) {
if (name[0]->text == "(") {
// Function pointer arg.
name = arg[-2];
if (arrayp(name)) {
// Expect something like ( * foo ).
// FIXME: Support addresses to function pointers?
name = name[2];
} else {
// Syntax error.
werror("Failed to find variable name in %s\n",
Parser.C.simple_reconstitute(arg));
fail = 1;
}
} else {
// FIXME: Support array notation?
werror("Failed to find variable name in %s\n",
Parser.C.simple_reconstitute(arg));
fail = 1;
}
}
out->write("%s%s", n?", ":"", name->text);
}
out->write(");\n"
"}\n\n");
}
return fail;
}
|