| 1 |
/* vim: ts=4 sw=4 sts=4 et tw=78 |
| 2 |
* Portions copyright (c) 2015-present, Facebook, Inc. All rights reserved. |
| 3 |
* Portions copyright (c) 2011 James R. McKaskill. |
| 4 |
* |
| 5 |
* This source code is licensed under the BSD-style license found in the |
| 6 |
* LICENSE file in the root directory of this source tree. An additional grant |
| 7 |
* of patent rights can be found in the PATENTS file in the same directory. |
| 8 |
*/ |
| 9 |
#include "ffi.h" |
| 10 |
|
| 11 |
#define IS_CONST(tok) (IS_LITERAL(tok, "const") || IS_LITERAL(tok, "__const") || IS_LITERAL(tok, "__const__")) |
| 12 |
#define IS_VOLATILE(tok) (IS_LITERAL(tok, "volatile") || IS_LITERAL(tok, "__volatile") || IS_LITERAL(tok, "__volatile__")) |
| 13 |
#define IS_RESTRICT(tok) (IS_LITERAL(tok, "restrict") || IS_LITERAL(tok, "__restrict") || IS_LITERAL(tok, "__restrict__")) |
| 14 |
#define IS_INLINE(tok) (IS_LITERAL(tok, "inline") || IS_LITERAL(tok, "__inline") || IS_LITERAL(tok, "__inline__")) |
| 15 |
|
| 16 |
enum etoken { |
| 17 |
TOK_NIL, |
| 18 |
TOK_NUMBER, |
| 19 |
TOK_STRING, |
| 20 |
TOK_TOKEN, |
| 21 |
|
| 22 |
/* the order of these values must match the token strings in lex.c */ |
| 23 |
|
| 24 |
TOK_3_BEGIN, |
| 25 |
TOK_VA_ARG, |
| 26 |
|
| 27 |
TOK_2_BEGIN, |
| 28 |
TOK_LEFT_SHIFT, TOK_RIGHT_SHIFT, TOK_LOGICAL_AND, TOK_LOGICAL_OR, TOK_LESS_EQUAL, |
| 29 |
TOK_GREATER_EQUAL, TOK_EQUAL, TOK_NOT_EQUAL, |
| 30 |
|
| 31 |
TOK_1_BEGIN, |
| 32 |
TOK_OPEN_CURLY, TOK_CLOSE_CURLY, TOK_SEMICOLON, TOK_COMMA, TOK_COLON, |
| 33 |
TOK_ASSIGN, TOK_OPEN_PAREN, TOK_CLOSE_PAREN, TOK_OPEN_SQUARE, TOK_CLOSE_SQUARE, |
| 34 |
TOK_DOT, TOK_AMPERSAND, TOK_LOGICAL_NOT, TOK_BITWISE_NOT, TOK_MINUS, |
| 35 |
TOK_PLUS, TOK_STAR, TOK_DIVIDE, TOK_MODULUS, TOK_LESS, |
| 36 |
TOK_GREATER, TOK_BITWISE_XOR, TOK_BITWISE_OR, TOK_QUESTION, TOK_POUND, |
| 37 |
|
| 38 |
TOK_REFERENCE = TOK_AMPERSAND, |
| 39 |
TOK_MULTIPLY = TOK_STAR, |
| 40 |
TOK_BITWISE_AND = TOK_AMPERSAND, |
| 41 |
}; |
| 42 |
|
| 43 |
struct token { |
| 44 |
enum etoken type; |
| 45 |
int64_t integer; |
| 46 |
const char* str; |
| 47 |
size_t size; |
| 48 |
}; |
| 49 |
|
| 50 |
#define IS_LITERAL(TOK, STR) \ |
| 51 |
(((TOK).size == sizeof(STR) - 1) && 0 == memcmp((TOK).str, STR, sizeof(STR) - 1)) |
| 52 |
|
| 53 |
/* the order of tokens _must_ match the order of the enum etoken enum */ |
| 54 |
|
| 55 |
static char tok3[][4] = { |
| 56 |
"...", /* unused ">>=", "<<=", */ |
| 57 |
}; |
| 58 |
|
| 59 |
static char tok2[][3] = { |
| 60 |
"<<", ">>", "&&", "||", "<=", |
| 61 |
">=", "==", "!=", |
| 62 |
/* unused "+=", "-=", "*=", "/=", "%=", "&=", "^=", "|=", "++", "--", "->", "::", */ |
| 63 |
}; |
| 64 |
|
| 65 |
static char tok1[] = { |
| 66 |
'{', '}', ';', ',', ':', |
| 67 |
'=', '(', ')', '[', ']', |
| 68 |
'.', '&', '!', '~', '-', |
| 69 |
'+', '*', '/', '%', '<', |
| 70 |
'>', '^', '|', '?', '#' |
| 71 |
}; |
| 72 |
|
| 73 |
static const char* etype_tostring(int type); |
| 74 |
static const char* etype_tostring(int type) |
| 75 |
{ |
| 76 |
switch (type) { |
| 77 |
case VOID_TYPE: return "void"; |
| 78 |
case DOUBLE_TYPE: return "double"; |
| 79 |
case FLOAT_TYPE: return "float"; |
| 80 |
case COMPLEX_DOUBLE_TYPE: return "complex double"; |
| 81 |
case COMPLEX_FLOAT_TYPE: return "complex float"; |
| 82 |
case BOOL_TYPE: return "bool"; |
| 83 |
case INT8_TYPE: return "int8"; |
| 84 |
case INT16_TYPE: return "int16"; |
| 85 |
case INT32_TYPE: return "int32"; |
| 86 |
case INT64_TYPE: return "int64"; |
| 87 |
case INTPTR_TYPE: return "intptr"; |
| 88 |
case ENUM_TYPE: return "enum"; |
| 89 |
case UNION_TYPE: return "union"; |
| 90 |
case STRUCT_TYPE: return "struct"; |
| 91 |
case FUNCTION_PTR_TYPE: return "function ptr"; |
| 92 |
case FUNCTION_TYPE: return "function"; |
| 93 |
default: return "invalid"; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
static void debug_print_type(const struct ctype* ct) |
| 98 |
{ |
| 99 |
printf("%s:%d is reference %d \n", __FILE__, __LINE__, ct->is_reference); |
| 100 |
printf("%s:%d is pointers %d \n", __FILE__, __LINE__, ct->pointers); |
| 101 |
printf(" sz %zu %zu %zu align %d ptr %d %d %d type %s%s %d %d %d name %d call %d %d var %d %d %d bit %d %d %d %d jit %d\n", |
| 102 |
/* sz */ |
| 103 |
ct->base_size, |
| 104 |
ct->array_size, |
| 105 |
ct->offset, |
| 106 |
/* align */ |
| 107 |
ct->align_mask, |
| 108 |
/* ptr */ |
| 109 |
ct->is_array, |
| 110 |
ct->pointers, |
| 111 |
ct->const_mask, |
| 112 |
/* type */ |
| 113 |
ct->is_unsigned ? "u" : "", |
| 114 |
etype_tostring(ct->type), |
| 115 |
ct->is_reference, |
| 116 |
ct->is_defined, |
| 117 |
ct->is_null, |
| 118 |
/* name */ |
| 119 |
ct->has_member_name, |
| 120 |
/* call */ |
| 121 |
ct->calling_convention, |
| 122 |
ct->has_var_arg, |
| 123 |
/* var */ |
| 124 |
ct->is_variable_array, |
| 125 |
ct->is_variable_struct, |
| 126 |
ct->variable_size_known, |
| 127 |
/* bit */ |
| 128 |
ct->is_bitfield, |
| 129 |
ct->has_bitfield, |
| 130 |
ct->bit_offset, |
| 131 |
ct->bit_size, |
| 132 |
/* jit */ |
| 133 |
ct->is_jitted); |
| 134 |
} |
| 135 |
|
| 136 |
static int next_token(lua_State* L, struct parser* P, struct token* tok) |
| 137 |
{ |
| 138 |
size_t i; |
| 139 |
const char* s = P->next; |
| 140 |
|
| 141 |
/* UTF8 BOM */ |
| 142 |
if (s[0] == '\xEF' && s[1] == '\xBB' && s[2] == '\xBF') { |
| 143 |
s += 3; |
| 144 |
} |
| 145 |
|
| 146 |
/* consume whitespace and comments */ |
| 147 |
for (;;) { |
| 148 |
/* consume whitespace */ |
| 149 |
while(*s == '\t' || *s == '\n' || *s == ' ' || *s == '\v' || *s == '\r') { |
| 150 |
if (*s == '\n') { |
| 151 |
P->line++; |
| 152 |
} |
| 153 |
s++; |
| 154 |
} |
| 155 |
|
| 156 |
/* consume comments */ |
| 157 |
if (*s == '/' && *(s+1) == '/') { |
| 158 |
|
| 159 |
s = strchr(s, '\n'); |
| 160 |
if (!s) { |
| 161 |
luaL_error(L, "non-terminated comment"); |
| 162 |
} |
| 163 |
|
| 164 |
} else if (*s == '/' && *(s+1) == '*') { |
| 165 |
s += 2; |
| 166 |
|
| 167 |
for (;;) { |
| 168 |
if (s[0] == '\0') { |
| 169 |
luaL_error(L, "non-terminated comment"); |
| 170 |
} else if (s[0] == '*' && s[1] == '/') { |
| 171 |
s += 2; |
| 172 |
break; |
| 173 |
} else if (s[0] == '\n') { |
| 174 |
P->line++; |
| 175 |
} |
| 176 |
s++; |
| 177 |
} |
| 178 |
|
| 179 |
} else if (*s == '\0') { |
| 180 |
tok->type = TOK_NIL; |
| 181 |
return 0; |
| 182 |
|
| 183 |
} else { |
| 184 |
break; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
P->prev = s; |
| 189 |
|
| 190 |
for (i = 0; i < sizeof(tok3) / sizeof(tok3[0]); i++) { |
| 191 |
if (s[0] == tok3[i][0] && s[1] == tok3[i][1] && s[2] == tok3[i][2]) { |
| 192 |
tok->type = (enum etoken) (TOK_3_BEGIN + 1 + i); |
| 193 |
P->next = s + 3; |
| 194 |
goto end; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
for (i = 0; i < sizeof(tok2) / sizeof(tok2[0]); i++) { |
| 199 |
if (s[0] == tok2[i][0] && s[1] == tok2[i][1]) { |
| 200 |
tok->type = (enum etoken) (TOK_2_BEGIN + 1 + i); |
| 201 |
P->next = s + 2; |
| 202 |
goto end; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
for (i = 0; i < sizeof(tok1) / sizeof(tok1[0]); i++) { |
| 207 |
if (s[0] == tok1[i]) { |
| 208 |
tok->type = (enum etoken) (TOK_1_BEGIN + 1 + i); |
| 209 |
P->next = s + 1; |
| 210 |
goto end; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if (*s == '.' || *s == '-' || ('0' <= *s && *s <= '9')) { |
| 215 |
/* number */ |
| 216 |
tok->type = TOK_NUMBER; |
| 217 |
|
| 218 |
/* split out the negative case so we get the full range of bits for |
| 219 |
* unsigned (eg to support 0xFFFFFFFF where sizeof(long) == 4) |
| 220 |
*/ |
| 221 |
if (*s == '-') { |
| 222 |
tok->integer = strtol(s, (char**) &s, 0); |
| 223 |
} else { |
| 224 |
tok->integer = strtoul(s, (char**) &s, 0); |
| 225 |
} |
| 226 |
|
| 227 |
while (*s == 'u' || *s == 'U' || *s == 'l' || *s == 'L') { |
| 228 |
s++; |
| 229 |
} |
| 230 |
|
| 231 |
P->next = s; |
| 232 |
goto end; |
| 233 |
|
| 234 |
} else if (*s == '\'' || *s == '\"') { |
| 235 |
/* "..." or '...' */ |
| 236 |
char quote = *s; |
| 237 |
s++; /* jump over " */ |
| 238 |
|
| 239 |
tok->type = TOK_STRING; |
| 240 |
tok->str = s; |
| 241 |
|
| 242 |
while (*s != quote) { |
| 243 |
|
| 244 |
if (*s == '\0' || (*s == '\\' && *(s+1) == '\0')) { |
| 245 |
return luaL_error(L, "string not finished"); |
| 246 |
} |
| 247 |
|
| 248 |
if (*s == '\\') { |
| 249 |
s++; |
| 250 |
} |
| 251 |
|
| 252 |
s++; |
| 253 |
} |
| 254 |
|
| 255 |
tok->size = s - tok->str; |
| 256 |
s++; /* jump over " */ |
| 257 |
P->next = s; |
| 258 |
goto end; |
| 259 |
|
| 260 |
} else if (('a' <= *s && *s <= 'z') || ('A' <= *s && *s <= 'Z') || *s == '_') { |
| 261 |
/* tokens */ |
| 262 |
tok->type = TOK_TOKEN; |
| 263 |
tok->str = s; |
| 264 |
|
| 265 |
while (('a' <= *s && *s <= 'z') || ('A' <= *s && *s <= 'Z') || *s == '_' || ('0' <= *s && *s <= '9')) { |
| 266 |
s++; |
| 267 |
} |
| 268 |
|
| 269 |
tok->size = s - tok->str; |
| 270 |
P->next = s; |
| 271 |
goto end; |
| 272 |
|
| 273 |
} else { |
| 274 |
return luaL_error(L, "invalid character %d", P->line); |
| 275 |
} |
| 276 |
|
| 277 |
end: |
| 278 |
/*fprintf(stderr, "token %d %d %.*s %.10s\n", tok->type, (int) tok->size, (tok->type == TOK_TOKEN || tok->type == TOK_STRING) ? (int) tok->size : 0, tok->str, P->next);*/ |
| 279 |
return 1; |
| 280 |
} |
| 281 |
|
| 282 |
#define require_token(L, P, tok) require_token_line(L, P, tok, __FILE__, __LINE__) |
| 283 |
|
| 284 |
static void require_token_line(lua_State* L, struct parser* P, struct token* tok, const char* file, int line) |
| 285 |
{ |
| 286 |
if (!next_token(L, P, tok)) { |
| 287 |
luaL_error(L, "unexpected end on line %s:%d", file, line); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
static void check_token(lua_State* L, struct parser* P, int type, const char* str, const char* err, ...) |
| 292 |
{ |
| 293 |
struct token tok; |
| 294 |
if (!next_token(L, P, &tok) || tok.type != type || (tok.type == TOK_TOKEN && (tok.size != strlen(str) || memcmp(tok.str, str, tok.size) != 0))) { |
| 295 |
va_list ap; |
| 296 |
va_start(ap, err); |
| 297 |
lua_pushvfstring(L, err, ap); |
| 298 |
lua_error(L); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
static void put_back(struct parser* P) |
| 303 |
{ P->next = P->prev; } |
| 304 |
|
| 305 |
|
| 306 |
int64_t calculate_constant(lua_State* L, struct parser* P); |
| 307 |
|
| 308 |
static int g_name_key; |
| 309 |
static int g_front_name_key; |
| 310 |
static int g_back_name_key; |
| 311 |
|
| 312 |
#ifndef max |
| 313 |
#define max(a,b) ((a) < (b) ? (b) : (a)) |
| 314 |
#endif |
| 315 |
|
| 316 |
#ifndef min |
| 317 |
#define min(a,b) ((a) < (b) ? (a) : (b)) |
| 318 |
#endif |
| 319 |
|
| 320 |
enum test {TEST}; |
| 321 |
|
| 322 |
/* Parses an enum definition from after the open curly through to the close |
| 323 |
* curly. Expects the user table to be on the top of the stack |
| 324 |
*/ |
| 325 |
static int parse_enum(lua_State* L, struct parser* P, struct ctype* type) |
| 326 |
{ |
| 327 |
struct token tok; |
| 328 |
int value = -1; |
| 329 |
int ct_usr = lua_gettop(L); |
| 330 |
|
| 331 |
for (;;) { |
| 332 |
require_token(L, P, &tok); |
| 333 |
|
| 334 |
assert(lua_gettop(L) == ct_usr); |
| 335 |
|
| 336 |
if (tok.type == TOK_CLOSE_CURLY) { |
| 337 |
break; |
| 338 |
} else if (tok.type != TOK_TOKEN) { |
| 339 |
return luaL_error(L, "unexpected token in enum at line %d", P->line); |
| 340 |
} |
| 341 |
|
| 342 |
lua_pushlstring(L, tok.str, tok.size); |
| 343 |
|
| 344 |
require_token(L, P, &tok); |
| 345 |
|
| 346 |
if (tok.type == TOK_COMMA || tok.type == TOK_CLOSE_CURLY) { |
| 347 |
/* we have an auto calculated enum value */ |
| 348 |
value++; |
| 349 |
} else if (tok.type == TOK_ASSIGN) { |
| 350 |
/* we have an explicit enum value */ |
| 351 |
value = (int) calculate_constant(L, P); |
| 352 |
require_token(L, P, &tok); |
| 353 |
} else { |
| 354 |
return luaL_error(L, "unexpected token in enum at line %d", P->line); |
| 355 |
} |
| 356 |
|
| 357 |
assert(lua_gettop(L) == ct_usr + 1); |
| 358 |
|
| 359 |
/* add the enum value to the constants table */ |
| 360 |
push_upval(L, &constants_key); |
| 361 |
lua_pushvalue(L, -2); |
| 362 |
lua_pushinteger(L, value); |
| 363 |
lua_rawset(L, -3); |
| 364 |
lua_pop(L, 1); |
| 365 |
|
| 366 |
assert(lua_gettop(L) == ct_usr + 1); |
| 367 |
|
| 368 |
/* add the enum value to the enum usr value table */ |
| 369 |
lua_pushinteger(L, value); |
| 370 |
lua_rawset(L, ct_usr); |
| 371 |
|
| 372 |
if (tok.type == TOK_CLOSE_CURLY) { |
| 373 |
break; |
| 374 |
} else if (tok.type != TOK_COMMA) { |
| 375 |
return luaL_error(L, "unexpected token in enum at line %d", P->line); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
type->base_size = sizeof(enum test); |
| 380 |
type->align_mask = sizeof(enum test) - 1; |
| 381 |
|
| 382 |
assert(lua_gettop(L) == ct_usr); |
| 383 |
return 0; |
| 384 |
} |
| 385 |
|
| 386 |
static void calculate_member_position(lua_State* L, struct parser* P, struct ctype* ct, struct ctype* mt, int* pbit_offset, int* pbitfield_type) |
| 387 |
{ |
| 388 |
int bit_offset = *pbit_offset; |
| 389 |
|
| 390 |
if (ct->type == UNION_TYPE) { |
| 391 |
size_t msize; |
| 392 |
|
| 393 |
if (mt->is_variable_struct || mt->is_variable_array) { |
| 394 |
luaL_error(L, "NYI: variable sized members in unions"); |
| 395 |
return; |
| 396 |
|
| 397 |
} else if (mt->is_bitfield) { |
| 398 |
msize = (mt->align_mask + 1); |
| 399 |
#ifdef _WIN32 |
| 400 |
/* MSVC has a bug where it doesn't update the alignment of |
| 401 |
* a union for bitfield members. */ |
| 402 |
mt->align_mask = 0; |
| 403 |
#endif |
| 404 |
|
| 405 |
} else if (mt->is_array) { |
| 406 |
msize = mt->array_size * (mt->pointers > 1 ? sizeof(void*) : mt->base_size); |
| 407 |
|
| 408 |
} else { |
| 409 |
msize = mt->pointers ? sizeof(void*) : mt->base_size; |
| 410 |
} |
| 411 |
|
| 412 |
ct->base_size = max(ct->base_size, msize); |
| 413 |
|
| 414 |
} else if (mt->is_bitfield) { |
| 415 |
if (mt->has_member_name && mt->bit_size == 0) { |
| 416 |
luaL_error(L, "zero length bitfields must be unnamed on line %d", P->line); |
| 417 |
} |
| 418 |
|
| 419 |
#if defined _WIN32 |
| 420 |
/* MSVC uses a seperate storage unit for each size. This is aligned |
| 421 |
* before the first bitfield. :0 finishes up the storage unit using |
| 422 |
* the greater alignment of the storage unit or the type used with the |
| 423 |
* :0. This is equivalent to the :0 always creating a new storage |
| 424 |
* unit, but not necesserily using it yet. |
| 425 |
*/ |
| 426 |
|
| 427 |
if (*pbitfield_type == -1 && mt->bit_size == 0) { |
| 428 |
/* :0 not after a bitfield are ignored */ |
| 429 |
return; |
| 430 |
} |
| 431 |
|
| 432 |
{ |
| 433 |
int different_storage = mt->align_mask != *pbitfield_type; |
| 434 |
int no_room_left = bit_offset + mt->bit_size > (mt->align_mask + 1) * CHAR_BIT; |
| 435 |
|
| 436 |
if (different_storage || no_room_left || !mt->bit_size) { |
| 437 |
ct->base_size += (bit_offset + CHAR_BIT - 1) / CHAR_BIT; |
| 438 |
bit_offset = 0; |
| 439 |
if (*pbitfield_type >= 0) { |
| 440 |
ct->base_size = ALIGN_UP(ct->base_size, *pbitfield_type); |
| 441 |
} |
| 442 |
ct->base_size = ALIGN_UP(ct->base_size, mt->align_mask); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
mt->bit_offset = bit_offset; |
| 447 |
mt->offset = ct->base_size; |
| 448 |
|
| 449 |
*pbitfield_type = mt->align_mask; |
| 450 |
bit_offset += mt->bit_size; |
| 451 |
|
| 452 |
// #elif defined OS_OSX |
| 453 |
// /* OSX doesn't use containers and bitfields are not aligned. So |
| 454 |
// * bitfields never add any padding, except for :0 which still forces |
| 455 |
// * an alignment based off the type used with the :0 */ |
| 456 |
// if (mt->bit_size) { |
| 457 |
// mt->offset = ct->base_size; |
| 458 |
// mt->bit_offset = bit_offset; |
| 459 |
// bit_offset += mt->bit_size; |
| 460 |
// ct->base_size += bit_offset / CHAR_BIT; |
| 461 |
// bit_offset = bit_offset % CHAR_BIT; |
| 462 |
// } else { |
| 463 |
// ct->base_size += (bit_offset + CHAR_BIT - 1) / CHAR_BIT; |
| 464 |
// ct->base_size = ALIGN_UP(ct->base_size, mt->align_mask); |
| 465 |
// bit_offset = 0; |
| 466 |
// } |
| 467 |
|
| 468 |
// if (!mt->has_member_name) { |
| 469 |
// /* unnamed bitfields don't update the struct alignment */ |
| 470 |
// mt->align_mask = 0; |
| 471 |
// } |
| 472 |
|
| 473 |
#elif defined __GNUC__ |
| 474 |
/* GCC tries to pack bitfields in as close as much as possible, but |
| 475 |
* still making sure that they don't cross alignment boundaries. |
| 476 |
* :0 forces an alignment based off the type used with the :0 |
| 477 |
*/ |
| 478 |
|
| 479 |
int bits_used = (ct->base_size - ALIGN_DOWN(ct->base_size, mt->align_mask)) * CHAR_BIT + bit_offset; |
| 480 |
int need_to_realign = bits_used + mt->bit_size > mt->base_size * CHAR_BIT; |
| 481 |
|
| 482 |
if (!mt->is_packed && (!mt->bit_size || need_to_realign)) { |
| 483 |
ct->base_size += (bit_offset + CHAR_BIT - 1) / CHAR_BIT; |
| 484 |
ct->base_size = ALIGN_UP(ct->base_size, mt->align_mask); |
| 485 |
bit_offset = 0; |
| 486 |
} |
| 487 |
|
| 488 |
mt->bit_offset = bit_offset; |
| 489 |
mt->offset = ct->base_size; |
| 490 |
|
| 491 |
bit_offset += mt->bit_size; |
| 492 |
ct->base_size += bit_offset / CHAR_BIT; |
| 493 |
bit_offset = bit_offset % CHAR_BIT; |
| 494 |
|
| 495 |
if (!mt->has_member_name) { |
| 496 |
#ifndef ARCH_ARM64 |
| 497 |
/* unnamed bitfields don't update the struct alignment in X86-64 |
| 498 |
* however it seem to have an effect in ARMv8 */ |
| 499 |
mt->align_mask = 0; |
| 500 |
#else |
| 501 |
#if defined OS_OSX |
| 502 |
/* unnamed bitfields don't update the struct alignment in X86-64 |
| 503 |
* and in ARMv* Apple Mac M1 |
| 504 |
* however it seem to have an effect in ubuntu linux ARMv8 */ |
| 505 |
mt->align_mask = 0; |
| 506 |
#endif |
| 507 |
#endif |
| 508 |
} |
| 509 |
#else |
| 510 |
#error |
| 511 |
#endif |
| 512 |
|
| 513 |
} else { |
| 514 |
/* finish up the current bitfield storage unit */ |
| 515 |
ct->base_size += (bit_offset + CHAR_BIT - 1) / CHAR_BIT; |
| 516 |
bit_offset = 0; |
| 517 |
|
| 518 |
if (*pbitfield_type >= 0) { |
| 519 |
ct->base_size = ALIGN_UP(ct->base_size, *pbitfield_type); |
| 520 |
} |
| 521 |
|
| 522 |
*pbitfield_type = -1; |
| 523 |
|
| 524 |
ct->base_size = ALIGN_UP(ct->base_size, mt->align_mask); |
| 525 |
mt->offset = ct->base_size; |
| 526 |
|
| 527 |
if (mt->is_variable_array) { |
| 528 |
ct->is_variable_struct = 1; |
| 529 |
ct->variable_increment = mt->pointers > 1 ? sizeof(void*) : mt->base_size; |
| 530 |
|
| 531 |
} else if (mt->is_variable_struct) { |
| 532 |
assert(!mt->variable_size_known && !mt->is_array && !mt->pointers); |
| 533 |
ct->base_size += mt->base_size; |
| 534 |
ct->is_variable_struct = 1; |
| 535 |
ct->variable_increment = mt->variable_increment; |
| 536 |
|
| 537 |
} else if (mt->is_array) { |
| 538 |
ct->base_size += mt->array_size * (mt->pointers > 1 ? sizeof(void*) : mt->base_size); |
| 539 |
|
| 540 |
} else { |
| 541 |
ct->base_size += mt->pointers ? sizeof(void*) : mt->base_size; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
/* increase the outer struct/union alignment if needed */ |
| 546 |
if (mt->align_mask > (int) ct->align_mask) { |
| 547 |
ct->align_mask = mt->align_mask; |
| 548 |
} |
| 549 |
|
| 550 |
if (mt->has_bitfield || mt->is_bitfield) { |
| 551 |
ct->has_bitfield = 1; |
| 552 |
} |
| 553 |
|
| 554 |
*pbit_offset = bit_offset; |
| 555 |
} |
| 556 |
|
| 557 |
static int copy_submembers(lua_State* L, int to_usr, int from_usr, const struct ctype* ft, int* midx) |
| 558 |
{ |
| 559 |
struct ctype ct; |
| 560 |
int i, sublen; |
| 561 |
|
| 562 |
from_usr = lua_absindex(L, from_usr); |
| 563 |
to_usr = lua_absindex(L, to_usr); |
| 564 |
|
| 565 |
/* integer keys */ |
| 566 |
sublen = (int) lua_rawlen(L, from_usr); |
| 567 |
for (i = 1; i <= sublen; i++) { |
| 568 |
lua_rawgeti(L, from_usr, i); |
| 569 |
|
| 570 |
//ct = *(const struct ctype*) lua_touserdata(L, -1); |
| 571 |
memcpy(&ct, (const struct ctype*) lua_touserdata(L, -1), sizeof(struct ctype)); |
| 572 |
ct.offset += ft->offset; |
| 573 |
lua_getuservalue(L, -1); |
| 574 |
|
| 575 |
push_ctype(L, -1, &ct); |
| 576 |
lua_rawseti(L, to_usr, (*midx)++); |
| 577 |
|
| 578 |
lua_pop(L, 2); /* ctype, user value */ |
| 579 |
} |
| 580 |
|
| 581 |
/* string keys */ |
| 582 |
lua_pushnil(L); |
| 583 |
while (lua_next(L, from_usr)) { |
| 584 |
if (lua_type(L, -2) == LUA_TSTRING) { |
| 585 |
struct ctype ct; |
| 586 |
//struct ctype ct = *(const struct ctype*) lua_touserdata(L, -1); |
| 587 |
memcpy(&ct, (const struct ctype*) lua_touserdata(L, -1), sizeof(struct ctype)); |
| 588 |
ct.offset += ft->offset; |
| 589 |
lua_getuservalue(L, -1); |
| 590 |
|
| 591 |
/* uservalue[sub_mname] = new_sub_mtype */ |
| 592 |
lua_pushvalue(L, -3); |
| 593 |
push_ctype(L, -2, &ct); |
| 594 |
lua_rawset(L, to_usr); |
| 595 |
|
| 596 |
lua_pop(L, 1); /* remove submember user value */ |
| 597 |
} |
| 598 |
lua_pop(L, 1); |
| 599 |
} |
| 600 |
|
| 601 |
return 0; |
| 602 |
} |
| 603 |
|
| 604 |
static int add_member(lua_State* L, int ct_usr, int mname, int mbr_usr, const struct ctype* mt, int* midx) |
| 605 |
{ |
| 606 |
ct_usr = lua_absindex(L, ct_usr); |
| 607 |
mname = lua_absindex(L, mname); |
| 608 |
|
| 609 |
push_ctype(L, mbr_usr, mt); |
| 610 |
|
| 611 |
/* usrvalue[mbr index] = pushed mtype */ |
| 612 |
lua_pushvalue(L, -1); |
| 613 |
lua_rawseti(L, ct_usr, (*midx)++); |
| 614 |
|
| 615 |
/* set usrvalue[mname] = pushed mtype */ |
| 616 |
lua_pushvalue(L, mname); |
| 617 |
lua_pushvalue(L, -2); |
| 618 |
lua_rawset(L, ct_usr); |
| 619 |
|
| 620 |
/* set usrvalue[mtype] = mname */ |
| 621 |
lua_pushvalue(L, -1); |
| 622 |
lua_pushvalue(L, mname); |
| 623 |
lua_rawset(L, ct_usr); |
| 624 |
|
| 625 |
lua_pop(L, 1); |
| 626 |
|
| 627 |
return 0; |
| 628 |
} |
| 629 |
|
| 630 |
/* Parses a struct from after the open curly through to the close curly. |
| 631 |
*/ |
| 632 |
static int parse_struct(lua_State* L, struct parser* P, int tmp_usr, const struct ctype* ct) |
| 633 |
{ |
| 634 |
struct token tok; |
| 635 |
int midx = 1; |
| 636 |
int top = lua_gettop(L); |
| 637 |
|
| 638 |
tmp_usr = lua_absindex(L, tmp_usr); |
| 639 |
|
| 640 |
/* parse members */ |
| 641 |
for (;;) { |
| 642 |
struct ctype mbase; |
| 643 |
|
| 644 |
assert(lua_gettop(L) == top); |
| 645 |
|
| 646 |
/* see if we're at the end of the struct */ |
| 647 |
require_token(L, P, &tok); |
| 648 |
if (tok.type == TOK_CLOSE_CURLY) { |
| 649 |
break; |
| 650 |
} else if (ct->is_variable_struct) { |
| 651 |
return luaL_error(L, "can't have members after a variable sized member on line %d", P->line); |
| 652 |
} else { |
| 653 |
put_back(P); |
| 654 |
} |
| 655 |
|
| 656 |
/* members are of the form |
| 657 |
* <base type> <arg>, <arg>, <arg>; |
| 658 |
* eg struct foo bar, *bar2[2]; |
| 659 |
* mbase is 'struct foo' |
| 660 |
* mtype is '' then '*[2]' |
| 661 |
* mname is 'bar' then 'bar2' |
| 662 |
*/ |
| 663 |
|
| 664 |
parse_type(L, P, &mbase); |
| 665 |
|
| 666 |
for (;;) { |
| 667 |
struct token mname; |
| 668 |
struct ctype mt = mbase; |
| 669 |
|
| 670 |
memset(&mname, 0, sizeof(mname)); |
| 671 |
|
| 672 |
if (ct->is_variable_struct) { |
| 673 |
return luaL_error(L, "can't have members after a variable sized member on line %d", P->line); |
| 674 |
} |
| 675 |
|
| 676 |
assert(lua_gettop(L) == top + 1); |
| 677 |
parse_argument(L, P, -1, &mt, &mname, NULL); |
| 678 |
assert(lua_gettop(L) == top + 2); |
| 679 |
|
| 680 |
if (!mt.is_defined && (mt.pointers - mt.is_array) == 0) { |
| 681 |
return luaL_error(L, "member type is undefined on line %d", P->line); |
| 682 |
} |
| 683 |
|
| 684 |
if (mt.type == VOID_TYPE && (mt.pointers - mt.is_array) == 0) { |
| 685 |
return luaL_error(L, "member type can not be void on line %d", P->line); |
| 686 |
} |
| 687 |
|
| 688 |
mt.has_member_name = (mname.size > 0); |
| 689 |
lua_pushlstring(L, mname.str, mname.size); |
| 690 |
|
| 691 |
add_member(L, tmp_usr, -1, -2, &mt, &midx); |
| 692 |
|
| 693 |
/* pop the usr value from push_argument and the member name */ |
| 694 |
lua_pop(L, 2); |
| 695 |
assert(lua_gettop(L) == top + 1); |
| 696 |
|
| 697 |
require_token(L, P, &tok); |
| 698 |
if (tok.type == TOK_SEMICOLON) { |
| 699 |
break; |
| 700 |
} else if (tok.type != TOK_COMMA) { |
| 701 |
luaL_error(L, "unexpected token in struct definition on line %d", P->line); |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
/* pop the usr value from push_type */ |
| 706 |
lua_pop(L, 1); |
| 707 |
} |
| 708 |
|
| 709 |
assert(lua_gettop(L) == top); |
| 710 |
return 0; |
| 711 |
} |
| 712 |
|
| 713 |
static int calculate_struct_offsets(lua_State* L, struct parser* P, int ct_usr, struct ctype* ct, int tmp_usr) |
| 714 |
{ |
| 715 |
int i; |
| 716 |
int midx = 1; |
| 717 |
int sz = (int) lua_rawlen(L, tmp_usr); |
| 718 |
int bit_offset = 0; |
| 719 |
int bitfield_type = -1; |
| 720 |
|
| 721 |
ct_usr = lua_absindex(L, ct_usr); |
| 722 |
tmp_usr = lua_absindex(L, tmp_usr); |
| 723 |
|
| 724 |
for (i = 1; i <= sz; i++) { |
| 725 |
struct ctype mt; |
| 726 |
|
| 727 |
/* get the member type */ |
| 728 |
lua_rawgeti(L, tmp_usr, i); |
| 729 |
//mt = *(const struct ctype*) lua_touserdata(L, -1); |
| 730 |
memcpy(&mt, (const struct ctype*) lua_touserdata(L, -1), sizeof(struct ctype)); |
| 731 |
//debug_print_type(&mt); |
| 732 |
|
| 733 |
/* get the member user table */ |
| 734 |
lua_getuservalue(L, -1); |
| 735 |
|
| 736 |
/* get the member name */ |
| 737 |
lua_pushvalue(L, -2); |
| 738 |
lua_rawget(L, tmp_usr); |
| 739 |
|
| 740 |
calculate_member_position(L, P, ct, &mt, &bit_offset, &bitfield_type); |
| 741 |
|
| 742 |
if (mt.has_member_name) { |
| 743 |
assert(!lua_isnil(L, -1)); |
| 744 |
add_member(L, ct_usr, -1, -2, &mt, &midx); |
| 745 |
|
| 746 |
} else if (mt.type == STRUCT_TYPE || mt.type == UNION_TYPE) { |
| 747 |
/* With an unnamed member we copy all of the submembers into our |
| 748 |
* usr value adjusting the offset as necessary. Note ctypes are |
| 749 |
* immutable so need to push a new ctype to update the offset. |
| 750 |
*/ |
| 751 |
copy_submembers(L, ct_usr, -2, &mt, &midx); |
| 752 |
|
| 753 |
} else { |
| 754 |
/* We ignore unnamed members that aren't structs or unions. These |
| 755 |
* are there just to change the padding */ |
| 756 |
} |
| 757 |
//debug_print_type(&mt); |
| 758 |
|
| 759 |
lua_pop(L, 3); |
| 760 |
} |
| 761 |
|
| 762 |
/* finish up the current bitfield storage unit */ |
| 763 |
ct->base_size += (bit_offset + CHAR_BIT - 1) / CHAR_BIT; |
| 764 |
|
| 765 |
/* only void is allowed 0 size */ |
| 766 |
if (ct->base_size == 0) { |
| 767 |
ct->base_size = 1; |
| 768 |
} |
| 769 |
|
| 770 |
ct->base_size = ALIGN_UP(ct->base_size, ct->align_mask); |
| 771 |
return 0; |
| 772 |
} |
| 773 |
|
| 774 |
/* copy over attributes that could be specified before the typedef eg |
| 775 |
* __attribute__(packed) const type_t */ |
| 776 |
static void instantiate_typedef(struct parser* P, struct ctype* tt, const struct ctype* ft) |
| 777 |
{ |
| 778 |
struct ctype pt = *tt; |
| 779 |
*tt = *ft; |
| 780 |
|
| 781 |
tt->const_mask |= pt.const_mask; |
| 782 |
tt->is_packed = pt.is_packed; |
| 783 |
|
| 784 |
if (tt->is_packed) { |
| 785 |
tt->align_mask = 0; |
| 786 |
} else { |
| 787 |
/* Instantiate the typedef in the current packing. This may be |
| 788 |
* further updated if a pointer is added or another alignment |
| 789 |
* attribute is applied. If pt.align_mask is already non-zero than an |
| 790 |
* increased alignment via __declspec(aligned(#)) has been set. */ |
| 791 |
tt->align_mask = max(min(P->align_mask, tt->align_mask), pt.align_mask); |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
/* this parses a struct or union starting with the optional |
| 796 |
* name before the opening brace |
| 797 |
* leaves the type usr value on the stack |
| 798 |
*/ |
| 799 |
static int parse_record(lua_State* L, struct parser* P, struct ctype* ct) |
| 800 |
{ |
| 801 |
struct token tok; |
| 802 |
int top = lua_gettop(L); |
| 803 |
|
| 804 |
require_token(L, P, &tok); |
| 805 |
|
| 806 |
/* name is optional */ |
| 807 |
if (tok.type == TOK_TOKEN) { |
| 808 |
/* declaration */ |
| 809 |
lua_pushlstring(L, tok.str, tok.size); |
| 810 |
|
| 811 |
assert(lua_gettop(L) == top+1); |
| 812 |
|
| 813 |
/* lookup the name to see if we've seen this type before */ |
| 814 |
push_upval(L, &types_key); |
| 815 |
lua_pushvalue(L, -2); |
| 816 |
lua_rawget(L, top+2); |
| 817 |
|
| 818 |
assert(lua_gettop(L) == top+3); |
| 819 |
|
| 820 |
if (lua_isnil(L, -1)) { |
| 821 |
lua_pop(L, 1); /* pop the nil usr value */ |
| 822 |
lua_newtable(L); /* the new usr table */ |
| 823 |
|
| 824 |
/* stack layout is: |
| 825 |
* top+1: record name |
| 826 |
* top+2: types table |
| 827 |
* top+3: new usr table |
| 828 |
*/ |
| 829 |
|
| 830 |
lua_pushlightuserdata(L, &g_name_key); |
| 831 |
lua_pushvalue(L, top+1); |
| 832 |
lua_rawset(L, top+3); /* usr[name_key] = name */ |
| 833 |
|
| 834 |
lua_pushvalue(L, top+1); |
| 835 |
push_ctype(L, top+3, ct); |
| 836 |
lua_rawset(L, top+2); /* types[name] = new_ctype */ |
| 837 |
|
| 838 |
} else { |
| 839 |
/* get the exsting declared type */ |
| 840 |
const struct ctype* prevt = (const struct ctype*) lua_touserdata(L, top+3); |
| 841 |
|
| 842 |
if (prevt->type != ct->type) { |
| 843 |
lua_getuservalue(L, top+3); |
| 844 |
push_type_name(L, -1, ct); |
| 845 |
push_type_name(L, top+3, prevt); |
| 846 |
luaL_error(L, "type '%s' previously declared as '%s'", lua_tostring(L, -2), lua_tostring(L, -1)); |
| 847 |
} |
| 848 |
|
| 849 |
instantiate_typedef(P, ct, prevt); |
| 850 |
|
| 851 |
/* replace the ctype with its usr value */ |
| 852 |
lua_getuservalue(L, -1); |
| 853 |
lua_replace(L, -2); |
| 854 |
} |
| 855 |
|
| 856 |
/* remove the extra name and types table */ |
| 857 |
lua_replace(L, -3); |
| 858 |
lua_pop(L, 1); |
| 859 |
|
| 860 |
assert(lua_gettop(L) == top + 1 && lua_istable(L, -1)); |
| 861 |
|
| 862 |
/* if a name is given then we may be at the end of the string |
| 863 |
* eg for ffi.new('struct foo') |
| 864 |
*/ |
| 865 |
if (!next_token(L, P, &tok)) { |
| 866 |
return 0; |
| 867 |
} |
| 868 |
|
| 869 |
} else { |
| 870 |
/* create a new unnamed record */ |
| 871 |
int num; |
| 872 |
|
| 873 |
/* get the next unnamed number */ |
| 874 |
push_upval(L, &next_unnamed_key); |
| 875 |
num = lua_tointeger(L, -1); |
| 876 |
lua_pop(L, 1); |
| 877 |
|
| 878 |
/* increment the unnamed upval */ |
| 879 |
lua_pushinteger(L, num + 1); |
| 880 |
set_upval(L, &next_unnamed_key); |
| 881 |
|
| 882 |
lua_newtable(L); /* the new usr table - leave on stack */ |
| 883 |
|
| 884 |
/* usr[name_key] = num */ |
| 885 |
lua_pushlightuserdata(L, &g_name_key); |
| 886 |
lua_pushfstring(L, "%d", num); |
| 887 |
lua_rawset(L, -3); |
| 888 |
} |
| 889 |
|
| 890 |
if (tok.type != TOK_OPEN_CURLY) { |
| 891 |
/* this may just be a declaration or use of the type as an argument or |
| 892 |
* member */ |
| 893 |
put_back(P); |
| 894 |
return 0; |
| 895 |
} |
| 896 |
|
| 897 |
if (ct->is_defined) { |
| 898 |
return luaL_error(L, "redefinition in line %d", P->line); |
| 899 |
} |
| 900 |
|
| 901 |
assert(lua_gettop(L) == top + 1 && lua_istable(L, -1)); |
| 902 |
|
| 903 |
if (ct->type == ENUM_TYPE) { |
| 904 |
parse_enum(L, P, ct); |
| 905 |
} else { |
| 906 |
/* we do a two stage parse, where we parse the content first and build up |
| 907 |
* the temp user table. We then iterate over that to calculate the offsets |
| 908 |
* and fill out ct_usr. This is so we can handle out of order members |
| 909 |
* (eg vtable) and attributes specified at the end of the struct. |
| 910 |
*/ |
| 911 |
lua_newtable(L); |
| 912 |
parse_struct(L, P, -1, ct); |
| 913 |
//debug_print_type(ct); |
| 914 |
calculate_struct_offsets(L, P, -2, ct, -1); |
| 915 |
//debug_print_type(ct); |
| 916 |
assert(lua_gettop(L) == top + 2 && lua_istable(L, -1)); |
| 917 |
lua_pop(L, 1); |
| 918 |
} |
| 919 |
|
| 920 |
assert(lua_gettop(L) == top + 1 && lua_istable(L, -1)); |
| 921 |
set_defined(L, -1, ct); |
| 922 |
assert(lua_gettop(L) == top + 1); |
| 923 |
return 0; |
| 924 |
} |
| 925 |
|
| 926 |
/* parses single or multi work built in types, and pushes it onto the stack */ |
| 927 |
static int parse_type_name(lua_State* L, struct parser* P) |
| 928 |
{ |
| 929 |
struct token tok; |
| 930 |
int flags = 0; |
| 931 |
|
| 932 |
enum { |
| 933 |
UNSIGNED = 0x01, |
| 934 |
SIGNED = 0x02, |
| 935 |
LONG = 0x04, |
| 936 |
SHORT = 0x08, |
| 937 |
INT = 0x10, |
| 938 |
CHAR = 0x20, |
| 939 |
LONG_LONG = 0x40, |
| 940 |
INT8 = 0x80, |
| 941 |
INT16 = 0x100, |
| 942 |
INT32 = 0x200, |
| 943 |
INT64 = 0x400, |
| 944 |
DOUBLE = 0x800, |
| 945 |
FLOAT = 0x1000, |
| 946 |
COMPLEX = 0x2000, |
| 947 |
}; |
| 948 |
|
| 949 |
require_token(L, P, &tok); |
| 950 |
|
| 951 |
/* we have to manually decode the builtin types since they can take up |
| 952 |
* more then one token |
| 953 |
*/ |
| 954 |
for (;;) { |
| 955 |
if (tok.type != TOK_TOKEN) { |
| 956 |
break; |
| 957 |
} else if (IS_LITERAL(tok, "unsigned")) { |
| 958 |
flags |= UNSIGNED; |
| 959 |
} else if (IS_LITERAL(tok, "signed")) { |
| 960 |
flags |= SIGNED; |
| 961 |
} else if (IS_LITERAL(tok, "short")) { |
| 962 |
flags |= SHORT; |
| 963 |
} else if (IS_LITERAL(tok, "char")) { |
| 964 |
flags |= CHAR; |
| 965 |
} else if (IS_LITERAL(tok, "long")) { |
| 966 |
flags |= (flags & LONG) ? LONG_LONG : LONG; |
| 967 |
} else if (IS_LITERAL(tok, "int")) { |
| 968 |
flags |= INT; |
| 969 |
} else if (IS_LITERAL(tok, "__int8")) { |
| 970 |
flags |= INT8; |
| 971 |
} else if (IS_LITERAL(tok, "__int16")) { |
| 972 |
flags |= INT16; |
| 973 |
} else if (IS_LITERAL(tok, "__int32")) { |
| 974 |
flags |= INT32; |
| 975 |
} else if (IS_LITERAL(tok, "__int64")) { |
| 976 |
flags |= INT64; |
| 977 |
} else if (IS_LITERAL(tok, "double")) { |
| 978 |
flags |= DOUBLE; |
| 979 |
} else if (IS_LITERAL(tok, "float")) { |
| 980 |
flags |= FLOAT; |
| 981 |
} else if (IS_LITERAL(tok, "complex") || IS_LITERAL(tok, "_Complex")) { |
| 982 |
flags |= COMPLEX; |
| 983 |
} else if (IS_LITERAL(tok, "register")) { |
| 984 |
/* ignore */ |
| 985 |
} else { |
| 986 |
break; |
| 987 |
} |
| 988 |
|
| 989 |
if (!next_token(L, P, &tok)) { |
| 990 |
break; |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
if (flags) { |
| 995 |
put_back(P); |
| 996 |
} |
| 997 |
|
| 998 |
if (flags & CHAR) { |
| 999 |
if (flags & SIGNED) { |
| 1000 |
lua_pushliteral(L, "int8_t"); |
| 1001 |
} else if (flags & UNSIGNED) { |
| 1002 |
lua_pushliteral(L, "uint8_t"); |
| 1003 |
} else { |
| 1004 |
lua_pushstring(L, (((char) -1) > 0) ? "uint8_t" : "int8_t"); |
| 1005 |
} |
| 1006 |
|
| 1007 |
} else if (flags & INT8) { |
| 1008 |
lua_pushstring(L, (flags & UNSIGNED) ? "uint8_t" : "int8_t"); |
| 1009 |
} else if (flags & INT16) { |
| 1010 |
lua_pushstring(L, (flags & UNSIGNED) ? "uint16_t" : "int16_t"); |
| 1011 |
} else if (flags & INT32) { |
| 1012 |
lua_pushstring(L, (flags & UNSIGNED) ? "uint32_t" : "int32_t"); |
| 1013 |
} else if (flags & (INT64 | LONG_LONG)) { |
| 1014 |
lua_pushstring(L, (flags & UNSIGNED) ? "uint64_t" : "int64_t"); |
| 1015 |
|
| 1016 |
} else if (flags & COMPLEX) { |
| 1017 |
if (flags & LONG) { |
| 1018 |
lua_pushliteral(L, "complex long double"); |
| 1019 |
} else if (flags & FLOAT) { |
| 1020 |
lua_pushliteral(L, "complex float"); |
| 1021 |
} else { |
| 1022 |
lua_pushliteral(L, "complex double"); |
| 1023 |
} |
| 1024 |
|
| 1025 |
} else if (flags & DOUBLE) { |
| 1026 |
if (flags & LONG) { |
| 1027 |
lua_pushliteral(L, "long double"); |
| 1028 |
} else { |
| 1029 |
lua_pushliteral(L, "double"); |
| 1030 |
} |
| 1031 |
|
| 1032 |
} else if (flags & FLOAT) { |
| 1033 |
lua_pushliteral(L, "float"); |
| 1034 |
|
| 1035 |
} else if (flags & SHORT) { |
| 1036 |
#define SHORT_TYPE(u) (sizeof(short) == sizeof(int64_t) ? u "int64_t" : sizeof(short) == sizeof(int32_t) ? u "int32_t" : u "int16_t") |
| 1037 |
if (flags & UNSIGNED) { |
| 1038 |
lua_pushstring(L, SHORT_TYPE("u")); |
| 1039 |
} else { |
| 1040 |
lua_pushstring(L, SHORT_TYPE("")); |
| 1041 |
} |
| 1042 |
#undef SHORT_TYPE |
| 1043 |
|
| 1044 |
} else if (flags & LONG) { |
| 1045 |
#define LONG_TYPE(u) (sizeof(long) == sizeof(int64_t) ? u "int64_t" : u "int32_t") |
| 1046 |
if (flags & UNSIGNED) { |
| 1047 |
lua_pushstring(L, LONG_TYPE("u")); |
| 1048 |
} else { |
| 1049 |
lua_pushstring(L, LONG_TYPE("")); |
| 1050 |
} |
| 1051 |
#undef LONG_TYPE |
| 1052 |
|
| 1053 |
} else if (flags) { |
| 1054 |
#define INT_TYPE(u) (sizeof(int) == sizeof(int64_t) ? u "int64_t" : sizeof(int) == sizeof(int32_t) ? u "int32_t" : u "int16_t") |
| 1055 |
if (flags & UNSIGNED) { |
| 1056 |
lua_pushstring(L, INT_TYPE("u")); |
| 1057 |
} else { |
| 1058 |
lua_pushstring(L, INT_TYPE("")); |
| 1059 |
} |
| 1060 |
#undef INT_TYPE |
| 1061 |
|
| 1062 |
} else { |
| 1063 |
lua_pushlstring(L, tok.str, tok.size); |
| 1064 |
} |
| 1065 |
|
| 1066 |
return 0; |
| 1067 |
} |
| 1068 |
|
| 1069 |
/* parse_attribute parses a token to see if it is an attribute. It may then |
| 1070 |
* parse some following tokens to decode the attribute setting the appropriate |
| 1071 |
* fields in ct. It will return 1 if the token was used (and possibly some |
| 1072 |
* more following it) or 0 if not. If the token was used, the next token must |
| 1073 |
* be retrieved using next_token/require_token. |
| 1074 |
*/ |
| 1075 |
static int parse_attribute(lua_State* L, struct parser* P, struct token* tok, struct ctype* ct, struct parser* asmname) |
| 1076 |
{ |
| 1077 |
if (tok->type != TOK_TOKEN) { |
| 1078 |
return 0; |
| 1079 |
|
| 1080 |
} else if (asmname && (IS_LITERAL(*tok, "__asm__") || IS_LITERAL(*tok, "__asm"))) { |
| 1081 |
check_token(L, P, TOK_OPEN_PAREN, NULL, "unexpected token after __asm__ on line %d", P->line); |
| 1082 |
*asmname = *P; |
| 1083 |
|
| 1084 |
require_token(L, P, tok); |
| 1085 |
while (tok->type == TOK_STRING) { |
| 1086 |
require_token(L, P, tok); |
| 1087 |
} |
| 1088 |
|
| 1089 |
if (tok->type != TOK_CLOSE_PAREN) { |
| 1090 |
luaL_error(L, "unexpected token after __asm__ on line %d", P->line); |
| 1091 |
} |
| 1092 |
return 1; |
| 1093 |
|
| 1094 |
} else if (IS_LITERAL(*tok, "__attribute__") || IS_LITERAL(*tok, "__declspec")) { |
| 1095 |
int parens = 1; |
| 1096 |
check_token(L, P, TOK_OPEN_PAREN, NULL, "expected parenthesis after __attribute__ or __declspec on line %d", P->line); |
| 1097 |
|
| 1098 |
for (;;) { |
| 1099 |
require_token(L, P, tok); |
| 1100 |
if (tok->type == TOK_OPEN_PAREN) { |
| 1101 |
parens++; |
| 1102 |
} else if (tok->type == TOK_CLOSE_PAREN) { |
| 1103 |
if (--parens == 0) { |
| 1104 |
break; |
| 1105 |
} |
| 1106 |
|
| 1107 |
} else if (tok->type != TOK_TOKEN) { |
| 1108 |
/* ignore unknown symbols within parentheses */ |
| 1109 |
|
| 1110 |
} else if (IS_LITERAL(*tok, "align") || IS_LITERAL(*tok, "aligned") || IS_LITERAL(*tok, "__aligned__")) { |
| 1111 |
unsigned align = 0; |
| 1112 |
require_token(L, P, tok); |
| 1113 |
|
| 1114 |
switch (tok->type) { |
| 1115 |
case TOK_CLOSE_PAREN: |
| 1116 |
align = ALIGNED_DEFAULT; |
| 1117 |
put_back(P); |
| 1118 |
break; |
| 1119 |
|
| 1120 |
case TOK_OPEN_PAREN: |
| 1121 |
require_token(L, P, tok); |
| 1122 |
|
| 1123 |
if (tok->type != TOK_NUMBER) { |
| 1124 |
luaL_error(L, "expected align(#) on line %d", P->line); |
| 1125 |
} |
| 1126 |
|
| 1127 |
switch (tok->integer) { |
| 1128 |
case 1: align = 0; break; |
| 1129 |
case 2: align = 1; break; |
| 1130 |
case 4: align = 3; break; |
| 1131 |
case 8: align = 7; break; |
| 1132 |
case 16: align = 15; break; |
| 1133 |
default: |
| 1134 |
luaL_error(L, "unsupported align size on line %d", P->line); |
| 1135 |
} |
| 1136 |
|
| 1137 |
check_token(L, P, TOK_CLOSE_PAREN, NULL, "expected align(#) on line %d", P->line); |
| 1138 |
break; |
| 1139 |
|
| 1140 |
default: |
| 1141 |
luaL_error(L, "expected align(#) on line %d", P->line); |
| 1142 |
} |
| 1143 |
|
| 1144 |
/* __attribute__(aligned(#)) is only supposed to increase alignment */ |
| 1145 |
ct->align_mask = max(align, ct->align_mask); |
| 1146 |
|
| 1147 |
} else if (IS_LITERAL(*tok, "packed") || IS_LITERAL(*tok, "__packed__")) { |
| 1148 |
ct->align_mask = 0; |
| 1149 |
ct->is_packed = 1; |
| 1150 |
|
| 1151 |
} else if (IS_LITERAL(*tok, "mode") || IS_LITERAL(*tok, "__mode__")) { |
| 1152 |
|
| 1153 |
check_token(L, P, TOK_OPEN_PAREN, NULL, "expected mode(MODE) on line %d", P->line); |
| 1154 |
|
| 1155 |
require_token(L, P, tok); |
| 1156 |
if (tok->type != TOK_TOKEN) { |
| 1157 |
luaL_error(L, "expected mode(MODE) on line %d", P->line); |
| 1158 |
} |
| 1159 |
|
| 1160 |
if (ct->type == FLOAT_TYPE || ct->type == DOUBLE_TYPE) { |
| 1161 |
struct {char ch; float v;} af; |
| 1162 |
struct {char ch; double v;} ad; |
| 1163 |
|
| 1164 |
if (IS_LITERAL(*tok, "SF") || IS_LITERAL(*tok, "__SF__")) { |
| 1165 |
ct->type = FLOAT_TYPE; |
| 1166 |
ct->base_size = sizeof(float); |
| 1167 |
ct->align_mask = ALIGNOF(af); |
| 1168 |
|
| 1169 |
} else if (IS_LITERAL(*tok, "DF") || IS_LITERAL(*tok, "__DF__")) { |
| 1170 |
ct->type = DOUBLE_TYPE; |
| 1171 |
ct->base_size = sizeof(double); |
| 1172 |
ct->align_mask = ALIGNOF(ad); |
| 1173 |
|
| 1174 |
} else { |
| 1175 |
luaL_error(L, "unexpected mode on line %d", P->line); |
| 1176 |
} |
| 1177 |
|
| 1178 |
} else { |
| 1179 |
struct {char ch; uint16_t v;} a16; |
| 1180 |
struct {char ch; uint32_t v;} a32; |
| 1181 |
struct {char ch; uint64_t v;} a64; |
| 1182 |
|
| 1183 |
if (IS_LITERAL(*tok, "QI") || IS_LITERAL(*tok, "__QI__") |
| 1184 |
|| IS_LITERAL(*tok, "byte") || IS_LITERAL(*tok, "__byte__") |
| 1185 |
) { |
| 1186 |
ct->type = INT8_TYPE; |
| 1187 |
ct->base_size = sizeof(uint8_t); |
| 1188 |
ct->align_mask = 0; |
| 1189 |
|
| 1190 |
} else if (IS_LITERAL(*tok, "HI") || IS_LITERAL(*tok, "__HI__")) { |
| 1191 |
ct->type = INT16_TYPE; |
| 1192 |
ct->base_size = sizeof(uint16_t); |
| 1193 |
ct->align_mask = ALIGNOF(a16); |
| 1194 |
|
| 1195 |
} else if (IS_LITERAL(*tok, "SI") || IS_LITERAL(*tok, "__SI__") |
| 1196 |
#if defined ARCH_X86 || defined ARCH_ARM |
| 1197 |
|| IS_LITERAL(*tok, "word") || IS_LITERAL(*tok, "__word__") |
| 1198 |
|| IS_LITERAL(*tok, "pointer") || IS_LITERAL(*tok, "__pointer__") |
| 1199 |
#endif |
| 1200 |
) { |
| 1201 |
ct->type = INT32_TYPE; |
| 1202 |
ct->base_size = sizeof(uint32_t); |
| 1203 |
ct->align_mask = ALIGNOF(a32); |
| 1204 |
|
| 1205 |
} else if (IS_LITERAL(*tok, "DI") || IS_LITERAL(*tok, "__DI__") |
| 1206 |
#if defined ARCH_X64 || defined ARCH_PPC64 || defined ARCH_ARM64 |
| 1207 |
|| IS_LITERAL(*tok, "word") || IS_LITERAL(*tok, "__word__") |
| 1208 |
|| IS_LITERAL(*tok, "pointer") || IS_LITERAL(*tok, "__pointer__") |
| 1209 |
#endif |
| 1210 |
) { |
| 1211 |
ct->type = INT64_TYPE; |
| 1212 |
ct->base_size = sizeof(uint64_t); |
| 1213 |
ct->align_mask = ALIGNOF(a64); |
| 1214 |
|
| 1215 |
} else { |
| 1216 |
luaL_error(L, "unexpected mode on line %d", P->line); |
| 1217 |
} |
| 1218 |
} |
| 1219 |
|
| 1220 |
check_token(L, P, TOK_CLOSE_PAREN, NULL, "expected mode(MODE) on line %d", P->line); |
| 1221 |
|
| 1222 |
} else if (IS_LITERAL(*tok, "cdecl") || IS_LITERAL(*tok, "__cdecl__")) { |
| 1223 |
ct->calling_convention = C_CALL; |
| 1224 |
|
| 1225 |
} else if (IS_LITERAL(*tok, "fastcall") || IS_LITERAL(*tok, "__fastcall__")) { |
| 1226 |
ct->calling_convention = FAST_CALL; |
| 1227 |
|
| 1228 |
} else if (IS_LITERAL(*tok, "stdcall") || IS_LITERAL(*tok, "__stdcall__")) { |
| 1229 |
ct->calling_convention = STD_CALL; |
| 1230 |
} |
| 1231 |
/* ignore unknown tokens within parentheses */ |
| 1232 |
} |
| 1233 |
return 1; |
| 1234 |
|
| 1235 |
} else if (IS_LITERAL(*tok, "__cdecl")) { |
| 1236 |
ct->calling_convention = C_CALL; |
| 1237 |
return 1; |
| 1238 |
|
| 1239 |
} else if (IS_LITERAL(*tok, "__fastcall")) { |
| 1240 |
ct->calling_convention = FAST_CALL; |
| 1241 |
return 1; |
| 1242 |
|
| 1243 |
} else if (IS_LITERAL(*tok, "__stdcall")) { |
| 1244 |
ct->calling_convention = STD_CALL; |
| 1245 |
return 1; |
| 1246 |
|
| 1247 |
} else if (IS_LITERAL(*tok, "__extension__") || IS_LITERAL(*tok, "extern")) { |
| 1248 |
/* ignore */ |
| 1249 |
return 1; |
| 1250 |
|
| 1251 |
} else { |
| 1252 |
return 0; |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
/* parses out the base type of a type expression in a function declaration, |
| 1257 |
* struct definition, typedef etc |
| 1258 |
* |
| 1259 |
* leaves the usr value of the type on the stack |
| 1260 |
*/ |
| 1261 |
int parse_type(lua_State* L, struct parser* P, struct ctype* ct) |
| 1262 |
{ |
| 1263 |
struct token tok; |
| 1264 |
int top = lua_gettop(L); |
| 1265 |
|
| 1266 |
memset(ct, 0, sizeof(struct ctype)); |
| 1267 |
|
| 1268 |
require_token(L, P, &tok); |
| 1269 |
|
| 1270 |
/* get const/volatile before the base type */ |
| 1271 |
for (;;) { |
| 1272 |
if (tok.type != TOK_TOKEN) { |
| 1273 |
return luaL_error(L, "unexpected value before type name on line %d", P->line); |
| 1274 |
|
| 1275 |
} else if (IS_CONST(tok)) { |
| 1276 |
ct->const_mask = 1; |
| 1277 |
require_token(L, P, &tok); |
| 1278 |
|
| 1279 |
} else if (IS_VOLATILE(tok) || |
| 1280 |
IS_RESTRICT(tok) || |
| 1281 |
IS_LITERAL(tok, "static") || |
| 1282 |
IS_INLINE(tok)) { |
| 1283 |
/* ignored for now */ |
| 1284 |
require_token(L, P, &tok); |
| 1285 |
} else if (parse_attribute(L, P, &tok, ct, NULL)) { |
| 1286 |
/* get function attributes before the return type */ |
| 1287 |
require_token(L, P, &tok); |
| 1288 |
|
| 1289 |
} else { |
| 1290 |
break; |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
/* get base type */ |
| 1295 |
if (tok.type != TOK_TOKEN) { |
| 1296 |
return luaL_error(L, "unexpected value before type name on line %d", P->line); |
| 1297 |
|
| 1298 |
} else if (IS_LITERAL(tok, "struct")) { |
| 1299 |
ct->type = STRUCT_TYPE; |
| 1300 |
parse_record(L, P, ct); |
| 1301 |
|
| 1302 |
} else if (IS_LITERAL(tok, "union")) { |
| 1303 |
ct->type = UNION_TYPE; |
| 1304 |
parse_record(L, P, ct); |
| 1305 |
|
| 1306 |
} else if (IS_LITERAL(tok, "enum")) { |
| 1307 |
ct->type = ENUM_TYPE; |
| 1308 |
parse_record(L, P, ct); |
| 1309 |
|
| 1310 |
} else { |
| 1311 |
put_back(P); |
| 1312 |
|
| 1313 |
/* lookup type */ |
| 1314 |
push_upval(L, &types_key); |
| 1315 |
parse_type_name(L, P); |
| 1316 |
//printf("%s:%d type name=[%s]\n", __FILE__, __LINE__, lua_tostring(L, -1)); |
| 1317 |
lua_rawget(L, -2); |
| 1318 |
lua_remove(L, -2); |
| 1319 |
|
| 1320 |
if (lua_isnil(L, -1)) { |
| 1321 |
lua_pushlstring(L, tok.str, tok.size); |
| 1322 |
return luaL_error(L, "unknown type %s on line %d", lua_tostring(L, -1), P->line); |
| 1323 |
} |
| 1324 |
|
| 1325 |
instantiate_typedef(P, ct, (const struct ctype*) lua_touserdata(L, -1)); |
| 1326 |
//debug_print_type(ct); |
| 1327 |
|
| 1328 |
/* we only want the usr tbl from the ctype in the types tbl */ |
| 1329 |
lua_getuservalue(L, -1); |
| 1330 |
lua_replace(L, -2); |
| 1331 |
} |
| 1332 |
|
| 1333 |
while (next_token(L, P, &tok)) { |
| 1334 |
if (tok.type != TOK_TOKEN) { |
| 1335 |
put_back(P); |
| 1336 |
break; |
| 1337 |
|
| 1338 |
} else if (IS_CONST(tok) || IS_VOLATILE(tok)) { |
| 1339 |
/* ignore for now */ |
| 1340 |
|
| 1341 |
} else { |
| 1342 |
put_back(P); |
| 1343 |
break; |
| 1344 |
} |
| 1345 |
} |
| 1346 |
|
| 1347 |
assert(lua_gettop(L) == top + 1 && (lua_istable(L, -1) || lua_isnil(L, -1))); |
| 1348 |
return 0; |
| 1349 |
} |
| 1350 |
|
| 1351 |
enum name_type { |
| 1352 |
BOTH, |
| 1353 |
FRONT, |
| 1354 |
BACK, |
| 1355 |
}; |
| 1356 |
|
| 1357 |
static void append_type_name(luaL_Buffer* B, int usr, const struct ctype* ct, enum name_type type) |
| 1358 |
{ |
| 1359 |
size_t i; |
| 1360 |
lua_State* L = B->L; |
| 1361 |
|
| 1362 |
usr = lua_absindex(L, usr); |
| 1363 |
|
| 1364 |
if (type == FRONT || type == BOTH) { |
| 1365 |
if (ct->type != FUNCTION_PTR_TYPE && (ct->const_mask & (1 << ct->pointers))) { |
| 1366 |
luaL_addstring(B, "const "); |
| 1367 |
} |
| 1368 |
|
| 1369 |
if (ct->is_unsigned) { |
| 1370 |
luaL_addstring(B, "unsigned "); |
| 1371 |
} |
| 1372 |
|
| 1373 |
switch (ct->type) { |
| 1374 |
case ENUM_TYPE: |
| 1375 |
luaL_addstring(B, "enum "); |
| 1376 |
goto get_name; |
| 1377 |
|
| 1378 |
case STRUCT_TYPE: |
| 1379 |
luaL_addstring(B, "struct "); |
| 1380 |
goto get_name; |
| 1381 |
|
| 1382 |
case UNION_TYPE: |
| 1383 |
luaL_addstring(B, "union "); |
| 1384 |
goto get_name; |
| 1385 |
|
| 1386 |
get_name: |
| 1387 |
lua_pushlightuserdata(L, &g_name_key); |
| 1388 |
lua_rawget(L, usr); |
| 1389 |
luaL_addvalue(B); |
| 1390 |
break; |
| 1391 |
|
| 1392 |
case FUNCTION_TYPE: |
| 1393 |
case FUNCTION_PTR_TYPE: |
| 1394 |
lua_pushlightuserdata(L, &g_front_name_key); |
| 1395 |
lua_rawget(L, usr); |
| 1396 |
luaL_addvalue(B); |
| 1397 |
break; |
| 1398 |
|
| 1399 |
case VOID_TYPE: |
| 1400 |
luaL_addstring(B, "void"); |
| 1401 |
break; |
| 1402 |
case BOOL_TYPE: |
| 1403 |
luaL_addstring(B, "bool"); |
| 1404 |
break; |
| 1405 |
case DOUBLE_TYPE: |
| 1406 |
luaL_addstring(B, "double"); |
| 1407 |
break; |
| 1408 |
case LONG_DOUBLE_TYPE: |
| 1409 |
luaL_addstring(B, "long double"); |
| 1410 |
break; |
| 1411 |
case FLOAT_TYPE: |
| 1412 |
luaL_addstring(B, "float"); |
| 1413 |
break; |
| 1414 |
case COMPLEX_LONG_DOUBLE_TYPE: |
| 1415 |
luaL_addstring(B, "long complex double"); |
| 1416 |
break; |
| 1417 |
case COMPLEX_DOUBLE_TYPE: |
| 1418 |
luaL_addstring(B, "complex double"); |
| 1419 |
break; |
| 1420 |
case COMPLEX_FLOAT_TYPE: |
| 1421 |
luaL_addstring(B, "complex float"); |
| 1422 |
break; |
| 1423 |
case INT8_TYPE: |
| 1424 |
luaL_addstring(B, "char"); |
| 1425 |
break; |
| 1426 |
case INT16_TYPE: |
| 1427 |
luaL_addstring(B, "short"); |
| 1428 |
break; |
| 1429 |
case INT32_TYPE: |
| 1430 |
luaL_addstring(B, "int"); |
| 1431 |
break; |
| 1432 |
case INT64_TYPE: |
| 1433 |
luaL_addstring(B, "long long"); |
| 1434 |
break; |
| 1435 |
|
| 1436 |
case INTPTR_TYPE: |
| 1437 |
if (sizeof(intptr_t) == sizeof(int32_t)) { |
| 1438 |
luaL_addstring(B, "long"); |
| 1439 |
} else if (sizeof(intptr_t) == sizeof(int64_t)) { |
| 1440 |
luaL_addstring(B, "long long"); |
| 1441 |
} else { |
| 1442 |
luaL_error(L, "internal error - bad type"); |
| 1443 |
} |
| 1444 |
break; |
| 1445 |
|
| 1446 |
default: |
| 1447 |
luaL_error(L, "internal error - bad type %d", ct->type); |
| 1448 |
} |
| 1449 |
|
| 1450 |
for (i = 0; i < ct->pointers - ct->is_array; i++) { |
| 1451 |
luaL_addchar(B, '*'); |
| 1452 |
if (ct->const_mask & (1 << (ct->pointers - i - 1))) { |
| 1453 |
luaL_addstring(B, " const"); |
| 1454 |
} |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
if (type == BOTH || type == BACK) { |
| 1459 |
if (ct->is_reference) { |
| 1460 |
luaL_addstring(B, " &"); |
| 1461 |
} |
| 1462 |
|
| 1463 |
if (ct->is_variable_array && !ct->variable_size_known) { |
| 1464 |
luaL_addstring(B, "[?]"); |
| 1465 |
} else if (ct->is_array) { |
| 1466 |
lua_pushfstring(L, "[%d]", (int) ct->array_size); |
| 1467 |
luaL_addvalue(B); |
| 1468 |
} |
| 1469 |
|
| 1470 |
if (ct->type == FUNCTION_PTR_TYPE || ct->type == FUNCTION_TYPE) { |
| 1471 |
lua_pushlightuserdata(L, &g_back_name_key); |
| 1472 |
lua_rawget(L, usr); |
| 1473 |
luaL_addvalue(B); |
| 1474 |
} |
| 1475 |
|
| 1476 |
if (ct->is_bitfield) { |
| 1477 |
lua_pushfstring(L, " : %d", (int) ct->bit_size); |
| 1478 |
luaL_addvalue(B); |
| 1479 |
} |
| 1480 |
} |
| 1481 |
} |
| 1482 |
|
| 1483 |
void push_type_name(lua_State* L, int usr, const struct ctype* ct) |
| 1484 |
{ |
| 1485 |
luaL_Buffer B; |
| 1486 |
usr = lua_absindex(L, usr); |
| 1487 |
luaL_buffinit(L, &B); |
| 1488 |
append_type_name(&B, usr, ct, BOTH); |
| 1489 |
luaL_pushresult(&B); |
| 1490 |
} |
| 1491 |
|
| 1492 |
static void push_function_type_strings(lua_State* L, int usr, const struct ctype* ct) |
| 1493 |
{ |
| 1494 |
size_t i, args; |
| 1495 |
luaL_Buffer B; |
| 1496 |
int top = lua_gettop(L); |
| 1497 |
const struct ctype* ret_ct; |
| 1498 |
|
| 1499 |
int arg_ct = top+3; |
| 1500 |
int arg_usr = top+4; |
| 1501 |
int ret_usr = top+6; |
| 1502 |
|
| 1503 |
usr = lua_absindex(L, usr); |
| 1504 |
|
| 1505 |
/* return type */ |
| 1506 |
lua_settop(L, top+4); /* room for two returns and two temp positions */ |
| 1507 |
lua_rawgeti(L, usr, 0); |
| 1508 |
lua_getuservalue(L, -1); |
| 1509 |
ret_ct = (const struct ctype*) lua_touserdata(L, -2); |
| 1510 |
|
| 1511 |
luaL_buffinit(L, &B); |
| 1512 |
append_type_name(&B, ret_usr, ret_ct, FRONT); |
| 1513 |
|
| 1514 |
if (ret_ct->type != FUNCTION_TYPE && ret_ct->type != FUNCTION_PTR_TYPE) { |
| 1515 |
luaL_addchar(&B, ' '); |
| 1516 |
} |
| 1517 |
|
| 1518 |
switch (ct->calling_convention) { |
| 1519 |
case STD_CALL: |
| 1520 |
luaL_addstring(&B, "(__stdcall *"); |
| 1521 |
break; |
| 1522 |
case FAST_CALL: |
| 1523 |
luaL_addstring(&B, "(__fastcall *"); |
| 1524 |
break; |
| 1525 |
case C_CALL: |
| 1526 |
luaL_addstring(&B, "(*"); |
| 1527 |
break; |
| 1528 |
default: |
| 1529 |
luaL_error(L, "internal error - unknown calling convention"); |
| 1530 |
} |
| 1531 |
|
| 1532 |
luaL_pushresult(&B); |
| 1533 |
lua_replace(L, top+1); |
| 1534 |
|
| 1535 |
luaL_buffinit(L, &B); |
| 1536 |
luaL_addstring(&B, ")("); |
| 1537 |
|
| 1538 |
/* arguments */ |
| 1539 |
args = lua_rawlen(L, usr); |
| 1540 |
for (i = 1; i <= args; i++) { |
| 1541 |
if (i > 1) { |
| 1542 |
luaL_addstring(&B, ", "); |
| 1543 |
} |
| 1544 |
|
| 1545 |
/* note push the arg and user value below the indexes used by the buffer |
| 1546 |
* and use indexes relative to top to avoid problems due to the buffer |
| 1547 |
* system pushing a variable number of arguments onto the stack */ |
| 1548 |
lua_rawgeti(L, usr, (int) i); |
| 1549 |
lua_replace(L, arg_ct); |
| 1550 |
lua_getuservalue(L, arg_ct); |
| 1551 |
lua_replace(L, arg_usr); |
| 1552 |
append_type_name(&B, arg_usr, (const struct ctype*) lua_touserdata(L, arg_ct), BOTH); |
| 1553 |
} |
| 1554 |
|
| 1555 |
luaL_addstring(&B, ")"); |
| 1556 |
append_type_name(&B, ret_usr, ret_ct, BACK); |
| 1557 |
luaL_pushresult(&B); |
| 1558 |
lua_replace(L, top+2); |
| 1559 |
|
| 1560 |
lua_settop(L, top+2); |
| 1561 |
assert(lua_isstring(L, top+1) && lua_isstring(L, top+2)); |
| 1562 |
} |
| 1563 |
|
| 1564 |
/* parses from after the opening paranthesis to after the closing parenthesis */ |
| 1565 |
static void parse_function_arguments(lua_State* L, struct parser* P, int ct_usr, struct ctype* ct) |
| 1566 |
{ |
| 1567 |
struct token tok; |
| 1568 |
int args = 0; |
| 1569 |
int top = lua_gettop(L); |
| 1570 |
|
| 1571 |
ct_usr = lua_absindex(L, ct_usr); |
| 1572 |
|
| 1573 |
for (;;) { |
| 1574 |
require_token(L, P, &tok); |
| 1575 |
|
| 1576 |
if (tok.type == TOK_CLOSE_PAREN) { |
| 1577 |
break; |
| 1578 |
} |
| 1579 |
|
| 1580 |
if (args) { |
| 1581 |
if (tok.type != TOK_COMMA) { |
| 1582 |
luaL_error(L, "unexpected token in function argument %d on line %d", args, P->line); |
| 1583 |
} |
| 1584 |
|
| 1585 |
require_token(L, P, &tok); |
| 1586 |
} |
| 1587 |
|
| 1588 |
if (tok.type == TOK_VA_ARG) { |
| 1589 |
ct->has_var_arg = true; |
| 1590 |
check_token(L, P, TOK_CLOSE_PAREN, "", "unexpected token after ... in function on line %d", P->line); |
| 1591 |
break; |
| 1592 |
|
| 1593 |
} else if (tok.type == TOK_TOKEN) { |
| 1594 |
struct ctype at; |
| 1595 |
|
| 1596 |
put_back(P); |
| 1597 |
parse_type(L, P, &at); |
| 1598 |
parse_argument(L, P, -1, &at, NULL, NULL); |
| 1599 |
|
| 1600 |
assert(lua_gettop(L) == top + 2); |
| 1601 |
|
| 1602 |
/* array arguments are just treated as their base pointer type */ |
| 1603 |
at.is_array = 0; |
| 1604 |
|
| 1605 |
/* check for the c style int func(void) and error on other uses of arguments of type void */ |
| 1606 |
if (at.type == VOID_TYPE && at.pointers == 0) { |
| 1607 |
if (args) { |
| 1608 |
luaL_error(L, "can't have argument of type void on line %d", P->line); |
| 1609 |
} |
| 1610 |
|
| 1611 |
check_token(L, P, TOK_CLOSE_PAREN, "", "unexpected void in function on line %d", P->line); |
| 1612 |
lua_pop(L, 2); |
| 1613 |
break; |
| 1614 |
} |
| 1615 |
|
| 1616 |
push_ctype(L, -1, &at); |
| 1617 |
lua_rawseti(L, ct_usr, ++args); |
| 1618 |
|
| 1619 |
lua_pop(L, 2); /* parse_type and parse_argument at_usr */ |
| 1620 |
|
| 1621 |
} else { |
| 1622 |
luaL_error(L, "unexpected token in function argument %d on line %d", args+1, P->line); |
| 1623 |
} |
| 1624 |
} |
| 1625 |
|
| 1626 |
assert(lua_gettop(L) == top); |
| 1627 |
} |
| 1628 |
|
| 1629 |
static int max_bitfield_size(int type) |
| 1630 |
{ |
| 1631 |
switch (type) { |
| 1632 |
case BOOL_TYPE: |
| 1633 |
return 1; |
| 1634 |
case INT8_TYPE: |
| 1635 |
return 8; |
| 1636 |
case INT16_TYPE: |
| 1637 |
return 16; |
| 1638 |
case INT32_TYPE: |
| 1639 |
case ENUM_TYPE: |
| 1640 |
return 32; |
| 1641 |
case INT64_TYPE: |
| 1642 |
return 64; |
| 1643 |
default: |
| 1644 |
return -1; |
| 1645 |
} |
| 1646 |
} |
| 1647 |
|
| 1648 |
static struct ctype* parse_argument2(lua_State* L, struct parser* P, int ct_usr, struct ctype* ct, struct token* name, struct parser* asmname); |
| 1649 |
|
| 1650 |
/* parses from after the first ( in a function declaration or function pointer |
| 1651 |
* can be one of: |
| 1652 |
* void foo(...) before ... |
| 1653 |
* void (foo)(...) before foo |
| 1654 |
* void (* <>)(...) before <> which is the inner type |
| 1655 |
*/ |
| 1656 |
static struct ctype* parse_function(lua_State* L, struct parser* P, int ct_usr, struct ctype* ct, struct token* name, struct parser* asmname) |
| 1657 |
{ |
| 1658 |
/* We have a function pointer or a function. The usr table will |
| 1659 |
* get replaced by the canonical one (if there is one) in |
| 1660 |
* find_canonical_usr after all the arguments and returns have |
| 1661 |
* been parsed. */ |
| 1662 |
struct token tok; |
| 1663 |
int top = lua_gettop(L); |
| 1664 |
struct ctype* ret; |
| 1665 |
|
| 1666 |
lua_newtable(L); |
| 1667 |
ret = push_ctype(L, ct_usr, ct); |
| 1668 |
lua_rawseti(L, -2, 0); |
| 1669 |
ct_usr = lua_gettop(L); |
| 1670 |
|
| 1671 |
memset(ct, 0, sizeof(*ct)); |
| 1672 |
ct->base_size = sizeof(void (*)()); |
| 1673 |
ct->align_mask = min(FUNCTION_ALIGN_MASK, P->align_mask); |
| 1674 |
ct->type = FUNCTION_TYPE; |
| 1675 |
ct->is_defined = 1; |
| 1676 |
|
| 1677 |
if (name->type == TOK_NIL) { |
| 1678 |
for (;;) { |
| 1679 |
require_token(L, P, &tok); |
| 1680 |
|
| 1681 |
if (tok.type == TOK_STAR) { |
| 1682 |
|
| 1683 |
if (ct->type == FUNCTION_TYPE) { |
| 1684 |
ct->type = FUNCTION_PTR_TYPE; |
| 1685 |
} else if (ct->pointers == POINTER_MAX) { |
| 1686 |
luaL_error(L, "maximum number of pointer derefs reached - use a struct to break up the pointers on line %d", P->line); |
| 1687 |
} else { |
| 1688 |
ct->pointers++; |
| 1689 |
ct->const_mask <<= 1; |
| 1690 |
} |
| 1691 |
|
| 1692 |
} else if (parse_attribute(L, P, &tok, ct, asmname)) { |
| 1693 |
/* parse_attribute sets the appropriate fields */ |
| 1694 |
|
| 1695 |
} else { |
| 1696 |
/* call parse_argument to handle the inner contents |
| 1697 |
* e.g. the <> in "void (* <>) (...)". Note that the |
| 1698 |
* inner contents can itself be a function, a function |
| 1699 |
* ptr, array, etc (e.g. "void (*signal(int sig, void |
| 1700 |
* (*func)(int)))(int)" ). |
| 1701 |
*/ |
| 1702 |
put_back(P); |
| 1703 |
ct = parse_argument2(L, P, ct_usr, ct, name, asmname); |
| 1704 |
break; |
| 1705 |
} |
| 1706 |
} |
| 1707 |
|
| 1708 |
check_token(L, P, TOK_CLOSE_PAREN, NULL, "unexpected token in function on line %d", P->line); |
| 1709 |
check_token(L, P, TOK_OPEN_PAREN, NULL, "unexpected token in function on line %d", P->line); |
| 1710 |
} |
| 1711 |
|
| 1712 |
parse_function_arguments(L, P, ct_usr, ct); |
| 1713 |
|
| 1714 |
/* if we have an inner function then set the outer function ptr as its |
| 1715 |
* return type and return the inner function |
| 1716 |
* e.g. for void (* <signal(int, void (*)(int))> )(int) inner is |
| 1717 |
* surrounded by <>, return type is void (*)(int) |
| 1718 |
*/ |
| 1719 |
if (lua_gettop(L) == ct_usr+1) { |
| 1720 |
lua_replace(L, ct_usr); |
| 1721 |
} |
| 1722 |
|
| 1723 |
assert(lua_gettop(L) == top + 1 && lua_istable(L, -1)); |
| 1724 |
return ret; |
| 1725 |
} |
| 1726 |
|
| 1727 |
static struct ctype* parse_argument2(lua_State* L, struct parser* P, int ct_usr, struct ctype* ct, struct token* name, struct parser* asmname) |
| 1728 |
{ |
| 1729 |
struct token tok; |
| 1730 |
int top = lua_gettop(L); |
| 1731 |
int ft_usr = 0; |
| 1732 |
|
| 1733 |
luaL_checkstack(L, 10, "function too complex"); |
| 1734 |
ct_usr = lua_absindex(L, ct_usr); |
| 1735 |
|
| 1736 |
for (;;) { |
| 1737 |
if (!next_token(L, P, &tok)) { |
| 1738 |
/* we've reached the end of the string */ |
| 1739 |
break; |
| 1740 |
|
| 1741 |
} else if (tok.type == TOK_STAR) { |
| 1742 |
if (ct->pointers == POINTER_MAX) { |
| 1743 |
luaL_error(L, "maximum number of pointer derefs reached - use a struct to break up the pointers on line %d", P->line); |
| 1744 |
} |
| 1745 |
|
| 1746 |
ct->pointers++; |
| 1747 |
ct->const_mask <<= 1; |
| 1748 |
|
| 1749 |
/* __declspec(align(#)) may come before the type in a member */ |
| 1750 |
if (!ct->is_packed) { |
| 1751 |
ct->align_mask = max(min(PTR_ALIGN_MASK, P->align_mask), ct->align_mask); |
| 1752 |
} |
| 1753 |
|
| 1754 |
} else if (tok.type == TOK_REFERENCE) { |
| 1755 |
ct->is_reference = 1; |
| 1756 |
|
| 1757 |
} else if (parse_attribute(L, P, &tok, ct, asmname)) { |
| 1758 |
/* parse attribute has filled out appropriate fields in type */ |
| 1759 |
|
| 1760 |
} else if (tok.type == TOK_OPEN_PAREN) { |
| 1761 |
//printf("%s:%d PARSE_FUNCTION {\n", __FILE__, __LINE__); |
| 1762 |
ct = parse_function(L, P, ct_usr, ct, name, asmname); |
| 1763 |
//printf("%s:%d PARSE_FUNCTION }\n", __FILE__, __LINE__); |
| 1764 |
ft_usr = lua_gettop(L); |
| 1765 |
|
| 1766 |
} else if (tok.type == TOK_OPEN_SQUARE) { |
| 1767 |
/* array */ |
| 1768 |
if (ct->pointers == POINTER_MAX) { |
| 1769 |
luaL_error(L, "maximum number of pointer derefs reached - use a struct to break up the pointers"); |
| 1770 |
} |
| 1771 |
ct->is_array = 1; |
| 1772 |
ct->pointers++; |
| 1773 |
ct->const_mask <<= 1; |
| 1774 |
require_token(L, P, &tok); |
| 1775 |
|
| 1776 |
if (ct->pointers == 1 && !ct->is_defined) { |
| 1777 |
luaL_error(L, "array of undefined type on line %d", P->line); |
| 1778 |
} |
| 1779 |
|
| 1780 |
if (ct->is_variable_struct || ct->is_variable_array) { |
| 1781 |
luaL_error(L, "can't have an array of a variably sized type on line %d", P->line); |
| 1782 |
} |
| 1783 |
|
| 1784 |
if (tok.type == TOK_QUESTION) { |
| 1785 |
ct->is_variable_array = 1; |
| 1786 |
ct->variable_increment = (ct->pointers > 1) ? sizeof(void*) : ct->base_size; |
| 1787 |
check_token(L, P, TOK_CLOSE_SQUARE, "", "invalid character in array on line %d", P->line); |
| 1788 |
|
| 1789 |
} else if (tok.type == TOK_CLOSE_SQUARE) { |
| 1790 |
ct->array_size = 0; |
| 1791 |
|
| 1792 |
} else if (tok.type == TOK_TOKEN && IS_RESTRICT(tok)) { |
| 1793 |
/* odd gcc extension foo[__restrict] for arguments */ |
| 1794 |
ct->array_size = 0; |
| 1795 |
check_token(L, P, TOK_CLOSE_SQUARE, "", "invalid character in array on line %d", P->line); |
| 1796 |
|
| 1797 |
} else { |
| 1798 |
int64_t asize; |
| 1799 |
put_back(P); |
| 1800 |
asize = calculate_constant(L, P); |
| 1801 |
if (asize < 0) { |
| 1802 |
luaL_error(L, "array size can not be negative on line %d", P->line); |
| 1803 |
} |
| 1804 |
ct->array_size = (size_t) asize; |
| 1805 |
check_token(L, P, TOK_CLOSE_SQUARE, "", "invalid character in array on line %d", P->line); |
| 1806 |
} |
| 1807 |
|
| 1808 |
} else if (tok.type == TOK_COLON) { |
| 1809 |
int64_t bsize = calculate_constant(L, P); |
| 1810 |
|
| 1811 |
if (ct->pointers || bsize < 0 || bsize > max_bitfield_size(ct->type)) { |
| 1812 |
luaL_error(L, "invalid bitfield on line %d", P->line); |
| 1813 |
} |
| 1814 |
|
| 1815 |
ct->is_bitfield = 1; |
| 1816 |
ct->bit_size = (unsigned) bsize; /* Potential overflow */ |
| 1817 |
|
| 1818 |
} else if (tok.type != TOK_TOKEN) { |
| 1819 |
/* we've reached the end of the declaration */ |
| 1820 |
put_back(P); |
| 1821 |
break; |
| 1822 |
|
| 1823 |
} else if (IS_CONST(tok)) { |
| 1824 |
ct->const_mask |= 1; |
| 1825 |
|
| 1826 |
} else if (IS_VOLATILE(tok) || IS_RESTRICT(tok)) { |
| 1827 |
/* ignored for now */ |
| 1828 |
|
| 1829 |
} else { |
| 1830 |
*name = tok; |
| 1831 |
} |
| 1832 |
} |
| 1833 |
|
| 1834 |
assert((ft_usr == 0 && lua_gettop(L) == top) || (lua_gettop(L) == top + 1 && ft_usr == top + 1 && (lua_istable(L, -1) || lua_isnil(L, -1)))); |
| 1835 |
return ct; |
| 1836 |
} |
| 1837 |
|
| 1838 |
static void find_canonical_usr(lua_State* L, int ct_usr, const struct ctype *ct) |
| 1839 |
{ |
| 1840 |
struct ctype rt; |
| 1841 |
int top = lua_gettop(L); |
| 1842 |
int types; |
| 1843 |
|
| 1844 |
if (ct->type != FUNCTION_PTR_TYPE && ct->type != FUNCTION_TYPE) { |
| 1845 |
return; |
| 1846 |
} |
| 1847 |
|
| 1848 |
luaL_checkstack(L, 10, "function too complex"); |
| 1849 |
ct_usr = lua_absindex(L, ct_usr); |
| 1850 |
|
| 1851 |
/* check to see if we already have the canonical usr table */ |
| 1852 |
lua_pushlightuserdata(L, &g_name_key); |
| 1853 |
lua_rawget(L, ct_usr); |
| 1854 |
if (!lua_isnil(L, -1)) { |
| 1855 |
lua_pop(L, 1); |
| 1856 |
assert(top == lua_gettop(L)); |
| 1857 |
return; |
| 1858 |
} |
| 1859 |
lua_pop(L, 1); |
| 1860 |
|
| 1861 |
assert(top == lua_gettop(L)); |
| 1862 |
|
| 1863 |
/* first canonize the return type */ |
| 1864 |
lua_rawgeti(L, ct_usr, 0); |
| 1865 |
//rt = *(struct ctype*) lua_touserdata(L, -1); |
| 1866 |
memcpy(&rt, (struct ctype*) lua_touserdata(L, -1), sizeof(struct ctype)); |
| 1867 |
lua_getuservalue(L, -1); |
| 1868 |
find_canonical_usr(L, -1, &rt); |
| 1869 |
push_ctype(L, -1, &rt); |
| 1870 |
lua_rawseti(L, ct_usr, 0); |
| 1871 |
lua_pop(L, 2); /* return ctype and usr */ |
| 1872 |
|
| 1873 |
assert(top == lua_gettop(L)); |
| 1874 |
|
| 1875 |
/* look up the type string in the types table */ |
| 1876 |
push_upval(L, &types_key); |
| 1877 |
types = lua_gettop(L); |
| 1878 |
|
| 1879 |
push_function_type_strings(L, ct_usr, ct); |
| 1880 |
lua_pushvalue(L, -2); |
| 1881 |
lua_pushvalue(L, -2); |
| 1882 |
lua_concat(L, 2); |
| 1883 |
|
| 1884 |
lua_pushvalue(L, -1); |
| 1885 |
lua_rawget(L, types); |
| 1886 |
|
| 1887 |
assert(lua_gettop(L) == types + 4 && types == top + 1); |
| 1888 |
/* stack: types, front, back, both, looked up value */ |
| 1889 |
|
| 1890 |
if (lua_isnil(L, -1)) { |
| 1891 |
lua_pop(L, 1); |
| 1892 |
|
| 1893 |
lua_pushlightuserdata(L, &g_front_name_key); |
| 1894 |
lua_pushvalue(L, -4); |
| 1895 |
lua_rawset(L, ct_usr); |
| 1896 |
|
| 1897 |
lua_pushlightuserdata(L, &g_back_name_key); |
| 1898 |
lua_pushvalue(L, -3); |
| 1899 |
lua_rawset(L, ct_usr); |
| 1900 |
|
| 1901 |
lua_pushlightuserdata(L, &g_name_key); |
| 1902 |
lua_pushvalue(L, -2); |
| 1903 |
lua_rawset(L, ct_usr); |
| 1904 |
|
| 1905 |
lua_pushvalue(L, -1); |
| 1906 |
push_ctype(L, ct_usr, ct); |
| 1907 |
lua_rawset(L, types); |
| 1908 |
} else { |
| 1909 |
lua_getuservalue(L, -1); |
| 1910 |
lua_replace(L, ct_usr); |
| 1911 |
lua_pop(L, 1); |
| 1912 |
} |
| 1913 |
|
| 1914 |
lua_pop(L, 4); |
| 1915 |
assert(top == lua_gettop(L) && types == top + 1); |
| 1916 |
} |
| 1917 |
|
| 1918 |
|
| 1919 |
/* parses after the main base type of a typedef, function argument or |
| 1920 |
* struct/union member |
| 1921 |
* eg for const void* bar[3] the base type is void with the subtype so far of |
| 1922 |
* const, this parses the "* bar[3]" and updates the type argument |
| 1923 |
* |
| 1924 |
* ct_usr and type must be as filled out by parse_type |
| 1925 |
* |
| 1926 |
* pushes the updated user value on the top of the stack |
| 1927 |
*/ |
| 1928 |
void parse_argument(lua_State* L, struct parser* P, int ct_usr, struct ctype* ct, struct token* pname, struct parser* asmname) |
| 1929 |
{ |
| 1930 |
struct token tok, name; |
| 1931 |
int top = lua_gettop(L); |
| 1932 |
|
| 1933 |
memset(&name, 0, sizeof(name)); |
| 1934 |
parse_argument2(L, P, ct_usr, ct, &name, asmname); |
| 1935 |
|
| 1936 |
for (;;) { |
| 1937 |
if (!next_token(L, P, &tok)) { |
| 1938 |
break; |
| 1939 |
} else if (parse_attribute(L, P, &tok, ct, asmname)) { |
| 1940 |
/* parse_attribute sets the appropriate fields */ |
| 1941 |
} else { |
| 1942 |
put_back(P); |
| 1943 |
break; |
| 1944 |
} |
| 1945 |
} |
| 1946 |
|
| 1947 |
if (lua_gettop(L) == top) { |
| 1948 |
lua_pushvalue(L, ct_usr); |
| 1949 |
} |
| 1950 |
|
| 1951 |
find_canonical_usr(L, -1, ct); |
| 1952 |
|
| 1953 |
if (pname) { |
| 1954 |
*pname = name; |
| 1955 |
} |
| 1956 |
} |
| 1957 |
|
| 1958 |
static void parse_typedef(lua_State* L, struct parser* P) |
| 1959 |
{ |
| 1960 |
struct token tok; |
| 1961 |
struct ctype base_type; |
| 1962 |
int top = lua_gettop(L); |
| 1963 |
|
| 1964 |
parse_type(L, P, &base_type); |
| 1965 |
|
| 1966 |
for (;;) { |
| 1967 |
struct ctype arg_type = base_type; |
| 1968 |
struct token name; |
| 1969 |
|
| 1970 |
memset(&name, 0, sizeof(name)); |
| 1971 |
|
| 1972 |
assert(lua_gettop(L) == top + 1); |
| 1973 |
parse_argument(L, P, -1, &arg_type, &name, NULL); |
| 1974 |
assert(lua_gettop(L) == top + 2); |
| 1975 |
|
| 1976 |
if (!name.size) { |
| 1977 |
luaL_error(L, "Can't have a typedef without a name on line %d", P->line); |
| 1978 |
} else if (arg_type.is_variable_array) { |
| 1979 |
luaL_error(L, "Can't typedef a variable length array on line %d", P->line); |
| 1980 |
} |
| 1981 |
|
| 1982 |
push_upval(L, &types_key); |
| 1983 |
lua_pushlstring(L, name.str, name.size); |
| 1984 |
push_ctype(L, -3, &arg_type); |
| 1985 |
lua_rawset(L, -3); |
| 1986 |
lua_pop(L, 2); /* types and parse_argument usr tbl */ |
| 1987 |
|
| 1988 |
require_token(L, P, &tok); |
| 1989 |
|
| 1990 |
if (tok.type == TOK_SEMICOLON) { |
| 1991 |
break; |
| 1992 |
} else if (tok.type != TOK_COMMA) { |
| 1993 |
luaL_error(L, "Unexpected character in typedef on line %d", P->line); |
| 1994 |
} |
| 1995 |
} |
| 1996 |
|
| 1997 |
lua_pop(L, 1); /* parse_type usr tbl */ |
| 1998 |
assert(lua_gettop(L) == top); |
| 1999 |
} |
| 2000 |
|
| 2001 |
static bool is_hex(char ch) |
| 2002 |
{ return ('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'f') || ('A' <= ch && ch <= 'F'); } |
| 2003 |
|
| 2004 |
static bool is_digit(char ch) |
| 2005 |
{ return '0' <= ch && ch <= '9'; } |
| 2006 |
|
| 2007 |
static int from_hex(char ch) |
| 2008 |
{ |
| 2009 |
if (ch >= 'a') { |
| 2010 |
return ch - 'a' + 10; |
| 2011 |
} else if (ch >= 'A') { |
| 2012 |
return ch - 'A' + 10; |
| 2013 |
} else { |
| 2014 |
return ch - '0'; |
| 2015 |
} |
| 2016 |
} |
| 2017 |
|
| 2018 |
static void push_strings(lua_State* L, struct parser* P) |
| 2019 |
{ |
| 2020 |
luaL_Buffer B; |
| 2021 |
luaL_buffinit(L, &B); |
| 2022 |
|
| 2023 |
for (;;) { |
| 2024 |
const char *p, *e; |
| 2025 |
char *t, *s; |
| 2026 |
struct token tok; |
| 2027 |
|
| 2028 |
require_token(L, P, &tok); |
| 2029 |
if (tok.type != TOK_STRING) { |
| 2030 |
break; |
| 2031 |
} |
| 2032 |
|
| 2033 |
p = tok.str; |
| 2034 |
e = p + tok.size; |
| 2035 |
|
| 2036 |
t = luaL_prepbuffsize(&B, tok.size); |
| 2037 |
s = t; |
| 2038 |
|
| 2039 |
while (p < e) { |
| 2040 |
if (*p == '\\') { |
| 2041 |
if (++p == e) { |
| 2042 |
luaL_error(L, "parse error in string"); |
| 2043 |
} |
| 2044 |
switch (*p) { |
| 2045 |
case '\\': *(t++) = '\\'; p++; break; |
| 2046 |
case '\"': *(t++) = '\"'; p++; break; |
| 2047 |
case '\'': *(t++) = '\''; p++; break; |
| 2048 |
case 'n': *(t++) = '\n'; p++; break; |
| 2049 |
case 'r': *(t++) = '\r'; p++; break; |
| 2050 |
case 'b': *(t++) = '\b'; p++; break; |
| 2051 |
case 't': *(t++) = '\t'; p++; break; |
| 2052 |
case 'f': *(t++) = '\f'; p++; break; |
| 2053 |
case 'a': *(t++) = '\a'; p++; break; |
| 2054 |
case 'v': *(t++) = '\v'; p++; break; |
| 2055 |
case 'e': *(t++) = 0x1B; p++; break; |
| 2056 |
case 'x': |
| 2057 |
{ |
| 2058 |
uint8_t u; |
| 2059 |
p++; |
| 2060 |
if (p + 2 > e || !is_hex(p[0]) || !is_hex(p[1])) { |
| 2061 |
luaL_error(L, "parse error in string"); |
| 2062 |
} |
| 2063 |
u = (from_hex(p[0]) << 4) | from_hex(p[1]); |
| 2064 |
*(t++) = *(char*) &u; |
| 2065 |
p += 2; |
| 2066 |
break; |
| 2067 |
} |
| 2068 |
default: |
| 2069 |
{ |
| 2070 |
uint8_t u; |
| 2071 |
const char* e2 = min(p + 3, e); |
| 2072 |
if (!is_digit(*p)) { |
| 2073 |
luaL_error(L, "parse error in string"); |
| 2074 |
} |
| 2075 |
u = *p - '0'; |
| 2076 |
p++; |
| 2077 |
while (is_digit(*p) && p < e2) { |
| 2078 |
u = 10*u + *p-'0'; |
| 2079 |
p++; |
| 2080 |
} |
| 2081 |
*(t++) = *(char*) &u; |
| 2082 |
break; |
| 2083 |
} |
| 2084 |
} |
| 2085 |
} else { |
| 2086 |
*(t++) = *(p++); |
| 2087 |
} |
| 2088 |
} |
| 2089 |
|
| 2090 |
luaL_addsize(&B, t-s); |
| 2091 |
} |
| 2092 |
|
| 2093 |
luaL_pushresult(&B); |
| 2094 |
} |
| 2095 |
|
| 2096 |
static void parse_constant_assignemnt(lua_State* L, |
| 2097 |
struct parser* P, |
| 2098 |
const struct ctype* type, |
| 2099 |
const struct token* name) |
| 2100 |
{ |
| 2101 |
int64_t val = calculate_constant(L, P); |
| 2102 |
|
| 2103 |
check_token(L, P, TOK_SEMICOLON, "", "expected ; after constant definition on line %d", P->line); |
| 2104 |
|
| 2105 |
push_upval(L, &constants_key); |
| 2106 |
lua_pushlstring(L, name->str, name->size); |
| 2107 |
|
| 2108 |
switch (type->type) { |
| 2109 |
case INT8_TYPE: |
| 2110 |
case INT16_TYPE: |
| 2111 |
case INT32_TYPE: |
| 2112 |
if (type->is_unsigned) |
| 2113 |
lua_pushinteger(L, (unsigned int) val); |
| 2114 |
else |
| 2115 |
lua_pushinteger(L, (int) val); |
| 2116 |
break; |
| 2117 |
|
| 2118 |
default: |
| 2119 |
luaL_error(L, "expected a valid 8-, 16-, or 32-bit signed or unsigned integer type after 'static const' on line %d", P->line); |
| 2120 |
} |
| 2121 |
|
| 2122 |
lua_rawset(L, -3); |
| 2123 |
lua_pop(L, 2); /*constants and type*/ |
| 2124 |
} |
| 2125 |
|
| 2126 |
#define END 0 |
| 2127 |
#define PRAGMA_POP 1 |
| 2128 |
|
| 2129 |
static int parse_root(lua_State* L, struct parser* P) |
| 2130 |
{ |
| 2131 |
int top = lua_gettop(L); |
| 2132 |
struct token tok; |
| 2133 |
|
| 2134 |
while (next_token(L, P, &tok)) { |
| 2135 |
/* we can have: |
| 2136 |
* struct definition |
| 2137 |
* enum definition |
| 2138 |
* union definition |
| 2139 |
* struct/enum/union declaration |
| 2140 |
* typedef |
| 2141 |
* function declaration |
| 2142 |
* pragma pack |
| 2143 |
*/ |
| 2144 |
|
| 2145 |
assert(lua_gettop(L) == top); |
| 2146 |
|
| 2147 |
if (tok.type == TOK_SEMICOLON) { |
| 2148 |
/* empty semicolon in root continue on */ |
| 2149 |
|
| 2150 |
} else if (tok.type == TOK_POUND) { |
| 2151 |
|
| 2152 |
check_token(L, P, TOK_TOKEN, "pragma", "unexpected pre processor directive on line %d", P->line); |
| 2153 |
check_token(L, P, TOK_TOKEN, "pack", "unexpected pre processor directive on line %d", P->line); |
| 2154 |
check_token(L, P, TOK_OPEN_PAREN, "", "invalid pack directive on line %d", P->line); |
| 2155 |
|
| 2156 |
require_token(L, P, &tok); |
| 2157 |
|
| 2158 |
if (tok.type == TOK_NUMBER) { |
| 2159 |
if (tok.integer != 1 && tok.integer != 2 && tok.integer != 4 && tok.integer != 8 && tok.integer != 16) { |
| 2160 |
luaL_error(L, "pack directive with invalid pack size on line %d", P->line); |
| 2161 |
} |
| 2162 |
|
| 2163 |
P->align_mask = (unsigned) (tok.integer - 1); |
| 2164 |
check_token(L, P, TOK_CLOSE_PAREN, "", "invalid pack directive on line %d", P->line); |
| 2165 |
|
| 2166 |
} else if (tok.type == TOK_TOKEN && IS_LITERAL(tok, "push")) { |
| 2167 |
int line = P->line; |
| 2168 |
unsigned previous_alignment = P->align_mask; |
| 2169 |
|
| 2170 |
check_token(L, P, TOK_CLOSE_PAREN, "", "invalid pack directive on line %d", P->line); |
| 2171 |
|
| 2172 |
if (parse_root(L, P) != PRAGMA_POP) { |
| 2173 |
luaL_error(L, "reached end of string without a pragma pop to match the push on line %d", line); |
| 2174 |
} |
| 2175 |
|
| 2176 |
P->align_mask = previous_alignment; |
| 2177 |
|
| 2178 |
} else if (tok.type == TOK_TOKEN && IS_LITERAL(tok, "pop")) { |
| 2179 |
check_token(L, P, TOK_CLOSE_PAREN, "", "invalid pack directive on line %d", P->line); |
| 2180 |
return PRAGMA_POP; |
| 2181 |
|
| 2182 |
} else { |
| 2183 |
luaL_error(L, "invalid pack directive on line %d", P->line); |
| 2184 |
} |
| 2185 |
|
| 2186 |
|
| 2187 |
} else if (tok.type != TOK_TOKEN) { |
| 2188 |
return luaL_error(L, "unexpected character on line %d", P->line); |
| 2189 |
|
| 2190 |
} else if (IS_LITERAL(tok, "__extension__")) { |
| 2191 |
/* ignore */ |
| 2192 |
continue; |
| 2193 |
|
| 2194 |
} else if (IS_LITERAL(tok, "extern")) { |
| 2195 |
/* ignore extern as data and functions can only be extern */ |
| 2196 |
continue; |
| 2197 |
|
| 2198 |
} else if (IS_LITERAL(tok, "typedef")) { |
| 2199 |
parse_typedef(L, P); |
| 2200 |
|
| 2201 |
} else { |
| 2202 |
/* type declaration, type definition, or function declaration */ |
| 2203 |
struct ctype type; |
| 2204 |
struct token name; |
| 2205 |
struct parser asmname; |
| 2206 |
|
| 2207 |
memset(&type, 0, sizeof(struct ctype)); |
| 2208 |
memset(&name, 0, sizeof(name)); |
| 2209 |
memset(&asmname, 0, sizeof(asmname)); |
| 2210 |
|
| 2211 |
put_back(P); |
| 2212 |
parse_type(L, P, &type); |
| 2213 |
|
| 2214 |
for (;;) { |
| 2215 |
parse_argument(L, P, -1, &type, &name, &asmname); |
| 2216 |
|
| 2217 |
if (name.size) { |
| 2218 |
/* global/function declaration */ |
| 2219 |
|
| 2220 |
/* set asmname_tbl[name] = asmname */ |
| 2221 |
if (asmname.next) { |
| 2222 |
push_upval(L, &asmname_key); |
| 2223 |
lua_pushlstring(L, name.str, name.size); |
| 2224 |
push_strings(L, &asmname); |
| 2225 |
lua_rawset(L, -3); |
| 2226 |
lua_pop(L, 1); /* asmname upval */ |
| 2227 |
} |
| 2228 |
|
| 2229 |
push_upval(L, &functions_key); |
| 2230 |
lua_pushlstring(L, name.str, name.size); |
| 2231 |
push_ctype(L, -3, &type); |
| 2232 |
lua_rawset(L, -3); |
| 2233 |
lua_pop(L, 1); /* functions upval */ |
| 2234 |
} else { |
| 2235 |
/* type declaration/definition - already been processed */ |
| 2236 |
} |
| 2237 |
|
| 2238 |
lua_pop(L, 1); |
| 2239 |
|
| 2240 |
if (!next_token(L, P, &tok)) { |
| 2241 |
break; |
| 2242 |
} |
| 2243 |
|
| 2244 |
if (tok.type == TOK_COMMA) { |
| 2245 |
continue; |
| 2246 |
} |
| 2247 |
|
| 2248 |
if (tok.type == TOK_OPEN_CURLY) { |
| 2249 |
int line = P->line; |
| 2250 |
int remaining = 1; |
| 2251 |
while (remaining > 0 && next_token(L, P, &tok)) { |
| 2252 |
if (tok.type == TOK_CLOSE_CURLY) { |
| 2253 |
remaining--; |
| 2254 |
} else if (tok.type == TOK_OPEN_CURLY) { |
| 2255 |
remaining++; |
| 2256 |
} |
| 2257 |
} |
| 2258 |
if (remaining > 0) { |
| 2259 |
luaL_error(L, "missing closing bracket for line %d", line); |
| 2260 |
} |
| 2261 |
} else if (tok.type == TOK_ASSIGN) { |
| 2262 |
parse_constant_assignemnt(L, P, &type, &name); |
| 2263 |
} else if (tok.type != TOK_SEMICOLON) { |
| 2264 |
luaL_error(L, "missing semicolon on line %d", P->line); |
| 2265 |
} |
| 2266 |
break; |
| 2267 |
} |
| 2268 |
|
| 2269 |
lua_pop(L, 1); |
| 2270 |
} |
| 2271 |
} |
| 2272 |
|
| 2273 |
return END; |
| 2274 |
} |
| 2275 |
|
| 2276 |
int ffi_cdef(lua_State* L) |
| 2277 |
{ |
| 2278 |
//printf("%s:%d [START CDEF]\n", __FILE__, __LINE__); |
| 2279 |
struct parser P; |
| 2280 |
|
| 2281 |
P.line = 1; |
| 2282 |
P.prev = P.next = luaL_checkstring(L, 1); |
| 2283 |
P.align_mask = DEFAULT_ALIGN_MASK; |
| 2284 |
|
| 2285 |
if (parse_root(L, &P) == PRAGMA_POP) { |
| 2286 |
luaL_error(L, "pragma pop without an associated push on line %d", P.line); |
| 2287 |
} |
| 2288 |
//printf("%s:%d [END CDEF]\n", __FILE__, __LINE__); |
| 2289 |
|
| 2290 |
return 0; |
| 2291 |
} |
| 2292 |
|
| 2293 |
/* calculate_constant handles operator precedence by having a number of |
| 2294 |
* recursive commands each of which computes the result at that level of |
| 2295 |
* precedence and above. calculate_constant1 is the highest precedence |
| 2296 |
*/ |
| 2297 |
|
| 2298 |
static int64_t string_to_int(const char* str, size_t size) |
| 2299 |
{ |
| 2300 |
const char* end = str + size; |
| 2301 |
char c = *str++; |
| 2302 |
if (str < end) |
| 2303 |
{ |
| 2304 |
if (c == '\\') { |
| 2305 |
c = *str++; |
| 2306 |
switch (c) { |
| 2307 |
case '\'': c = '\''; break; |
| 2308 |
case '\"': c = '\"'; break; |
| 2309 |
case '\?': c = '\?'; break; |
| 2310 |
case '\\': c = '\\'; break; |
| 2311 |
case 'a': c = '\a'; break; |
| 2312 |
case 'b': c = '\b'; break; |
| 2313 |
case 'f': c = '\f'; break; |
| 2314 |
case 'n': c = '\n'; break; |
| 2315 |
case 'r': c = '\r'; break; |
| 2316 |
case 't': c = '\t'; break; |
| 2317 |
case 'v': c = '\v'; break; |
| 2318 |
case 'e': c = 27; break; |
| 2319 |
case 'x': |
| 2320 |
c = 0; |
| 2321 |
while (str < end) { |
| 2322 |
char d = *str++; |
| 2323 |
c *= 16; |
| 2324 |
if (d >= '0' && d <= '9') c += (d - '0'); |
| 2325 |
else c += (d - 'a' + 10); |
| 2326 |
} |
| 2327 |
break; |
| 2328 |
default: |
| 2329 |
c = c - '0'; |
| 2330 |
while (str < end) { |
| 2331 |
char d = *str++; |
| 2332 |
c = c*8 + (d - '0'); |
| 2333 |
} |
| 2334 |
break; |
| 2335 |
} |
| 2336 |
} |
| 2337 |
} |
| 2338 |
return c; |
| 2339 |
} |
| 2340 |
|
| 2341 |
static int try_cast(lua_State* L) |
| 2342 |
{ |
| 2343 |
struct parser* P = (struct parser*) lua_touserdata(L, 1); |
| 2344 |
struct ctype ct; |
| 2345 |
struct token name, tok; |
| 2346 |
memset(&name, 0, sizeof(name)); |
| 2347 |
|
| 2348 |
parse_type(L, P, &ct); |
| 2349 |
parse_argument(L, P, -1, &ct, &name, NULL); |
| 2350 |
|
| 2351 |
require_token(L, P, &tok); |
| 2352 |
if (tok.type != TOK_CLOSE_PAREN || name.size) { |
| 2353 |
return luaL_error(L, "invalid cast"); |
| 2354 |
} |
| 2355 |
|
| 2356 |
if (ct.pointers/* || ct.type != INT32_TYPE*/) { |
| 2357 |
return luaL_error(L, "unsupported cast on line %d", P->line); |
| 2358 |
} |
| 2359 |
|
| 2360 |
return 0; |
| 2361 |
} |
| 2362 |
|
| 2363 |
static int64_t calculate_constant2(lua_State* L, struct parser* P, struct token* tok); |
| 2364 |
|
| 2365 |
/* () */ |
| 2366 |
static int64_t calculate_constant1(lua_State* L, struct parser* P, struct token* tok) |
| 2367 |
{ |
| 2368 |
int64_t ret; |
| 2369 |
|
| 2370 |
if (tok->type == TOK_NUMBER) { |
| 2371 |
ret = tok->integer; |
| 2372 |
next_token(L, P, tok); |
| 2373 |
return ret; |
| 2374 |
|
| 2375 |
} else if (tok->type == TOK_TOKEN) { |
| 2376 |
/* look up name in constants table */ |
| 2377 |
push_upval(L, &constants_key); |
| 2378 |
lua_pushlstring(L, tok->str, tok->size); |
| 2379 |
lua_rawget(L, -2); |
| 2380 |
lua_remove(L, -2); /* constants table */ |
| 2381 |
|
| 2382 |
if (!lua_isnumber(L, -1)) { |
| 2383 |
lua_pushlstring(L, tok->str, tok->size); |
| 2384 |
luaL_error(L, "use of undefined constant %s on line %d", lua_tostring(L, -1), P->line); |
| 2385 |
} |
| 2386 |
|
| 2387 |
ret = (int64_t) lua_tonumber(L, -1); |
| 2388 |
lua_pop(L, 1); |
| 2389 |
next_token(L, P, tok); |
| 2390 |
return ret; |
| 2391 |
|
| 2392 |
} else if (tok->type == TOK_OPEN_PAREN) { |
| 2393 |
struct parser before_cast = *P; |
| 2394 |
int top = lua_gettop(L); |
| 2395 |
|
| 2396 |
/* see if this is a numeric cast, which we ignore */ |
| 2397 |
lua_pushcfunction(L, &try_cast); |
| 2398 |
lua_pushlightuserdata(L, P); |
| 2399 |
if (!lua_pcall(L, 1, 0, 0)) { |
| 2400 |
next_token(L, P, tok); |
| 2401 |
return calculate_constant2(L, P, tok); |
| 2402 |
} |
| 2403 |
lua_settop(L, top); |
| 2404 |
|
| 2405 |
*P = before_cast; |
| 2406 |
ret = calculate_constant(L, P); |
| 2407 |
|
| 2408 |
require_token(L, P, tok); |
| 2409 |
if (tok->type != TOK_CLOSE_PAREN) { |
| 2410 |
luaL_error(L, "error whilst parsing constant at line %d", P->line); |
| 2411 |
} |
| 2412 |
|
| 2413 |
next_token(L, P, tok); |
| 2414 |
return ret; |
| 2415 |
|
| 2416 |
} else if (tok->type == TOK_STRING) { |
| 2417 |
ret = string_to_int(tok->str, tok->size); |
| 2418 |
|
| 2419 |
next_token(L, P, tok); |
| 2420 |
return ret; |
| 2421 |
|
| 2422 |
} else { |
| 2423 |
return luaL_error(L, "unexpected token whilst parsing constant at line %d", P->line); |
| 2424 |
} |
| 2425 |
} |
| 2426 |
|
| 2427 |
/* ! and ~, unary + and -, and sizeof */ |
| 2428 |
static int64_t calculate_constant2(lua_State* L, struct parser* P, struct token* tok) |
| 2429 |
{ |
| 2430 |
if (tok->type == TOK_LOGICAL_NOT) { |
| 2431 |
require_token(L, P, tok); |
| 2432 |
return !calculate_constant2(L, P, tok); |
| 2433 |
|
| 2434 |
} else if (tok->type == TOK_BITWISE_NOT) { |
| 2435 |
require_token(L, P, tok); |
| 2436 |
return ~calculate_constant2(L, P, tok); |
| 2437 |
|
| 2438 |
} else if (tok->type == TOK_PLUS) { |
| 2439 |
require_token(L, P, tok); |
| 2440 |
return calculate_constant2(L, P, tok); |
| 2441 |
|
| 2442 |
} else if (tok->type == TOK_MINUS) { |
| 2443 |
require_token(L, P, tok); |
| 2444 |
return -calculate_constant2(L, P, tok); |
| 2445 |
|
| 2446 |
} else if (tok->type == TOK_TOKEN && |
| 2447 |
(IS_LITERAL(*tok, "sizeof") |
| 2448 |
|| IS_LITERAL(*tok, "alignof") |
| 2449 |
|| IS_LITERAL(*tok, "__alignof__") |
| 2450 |
|| IS_LITERAL(*tok, "__alignof"))) { |
| 2451 |
|
| 2452 |
bool issize = IS_LITERAL(*tok, "sizeof"); |
| 2453 |
struct ctype type; |
| 2454 |
|
| 2455 |
require_token(L, P, tok); |
| 2456 |
if (tok->type != TOK_OPEN_PAREN) { |
| 2457 |
luaL_error(L, "invalid sizeof at line %d", P->line); |
| 2458 |
} |
| 2459 |
|
| 2460 |
parse_type(L, P, &type); |
| 2461 |
parse_argument(L, P, -1, &type, NULL, NULL); |
| 2462 |
lua_pop(L, 2); |
| 2463 |
|
| 2464 |
require_token(L, P, tok); |
| 2465 |
if (tok->type != TOK_CLOSE_PAREN) { |
| 2466 |
luaL_error(L, "invalid sizeof at line %d", P->line); |
| 2467 |
} |
| 2468 |
|
| 2469 |
next_token(L, P, tok); |
| 2470 |
|
| 2471 |
return issize ? ctype_size(L, &type) : type.align_mask + 1; |
| 2472 |
|
| 2473 |
} else { |
| 2474 |
return calculate_constant1(L, P, tok); |
| 2475 |
} |
| 2476 |
} |
| 2477 |
|
| 2478 |
/* binary * / and % (left associative) */ |
| 2479 |
static int64_t calculate_constant3(lua_State* L, struct parser* P, struct token* tok) |
| 2480 |
{ |
| 2481 |
int64_t left = calculate_constant2(L, P, tok); |
| 2482 |
|
| 2483 |
for (;;) { |
| 2484 |
if (tok->type == TOK_MULTIPLY) { |
| 2485 |
require_token(L, P, tok); |
| 2486 |
left *= calculate_constant2(L, P, tok); |
| 2487 |
|
| 2488 |
} else if (tok->type == TOK_DIVIDE) { |
| 2489 |
require_token(L, P, tok); |
| 2490 |
left /= calculate_constant2(L, P, tok); |
| 2491 |
|
| 2492 |
} else if (tok->type == TOK_MODULUS) { |
| 2493 |
require_token(L, P, tok); |
| 2494 |
left %= calculate_constant2(L, P, tok); |
| 2495 |
|
| 2496 |
} else { |
| 2497 |
return left; |
| 2498 |
} |
| 2499 |
} |
| 2500 |
} |
| 2501 |
|
| 2502 |
/* binary + and - (left associative) */ |
| 2503 |
static int64_t calculate_constant4(lua_State* L, struct parser* P, struct token* tok) |
| 2504 |
{ |
| 2505 |
int64_t left = calculate_constant3(L, P, tok); |
| 2506 |
|
| 2507 |
for (;;) { |
| 2508 |
if (tok->type == TOK_PLUS) { |
| 2509 |
require_token(L, P, tok); |
| 2510 |
left += calculate_constant3(L, P, tok); |
| 2511 |
|
| 2512 |
} else if (tok->type == TOK_MINUS) { |
| 2513 |
require_token(L, P, tok); |
| 2514 |
left -= calculate_constant3(L, P, tok); |
| 2515 |
|
| 2516 |
} else { |
| 2517 |
return left; |
| 2518 |
} |
| 2519 |
} |
| 2520 |
} |
| 2521 |
|
| 2522 |
/* binary << and >> (left associative) */ |
| 2523 |
static int64_t calculate_constant5(lua_State* L, struct parser* P, struct token* tok) |
| 2524 |
{ |
| 2525 |
int64_t left = calculate_constant4(L, P, tok); |
| 2526 |
|
| 2527 |
for (;;) { |
| 2528 |
if (tok->type == TOK_LEFT_SHIFT) { |
| 2529 |
require_token(L, P, tok); |
| 2530 |
left <<= calculate_constant4(L, P, tok); |
| 2531 |
|
| 2532 |
} else if (tok->type == TOK_RIGHT_SHIFT) { |
| 2533 |
require_token(L, P, tok); |
| 2534 |
left >>= calculate_constant4(L, P, tok); |
| 2535 |
|
| 2536 |
} else { |
| 2537 |
return left; |
| 2538 |
} |
| 2539 |
} |
| 2540 |
} |
| 2541 |
|
| 2542 |
/* binary <, <=, >, and >= (left associative) */ |
| 2543 |
static int64_t calculate_constant6(lua_State* L, struct parser* P, struct token* tok) |
| 2544 |
{ |
| 2545 |
int64_t left = calculate_constant5(L, P, tok); |
| 2546 |
|
| 2547 |
for (;;) { |
| 2548 |
if (tok->type == TOK_LESS) { |
| 2549 |
require_token(L, P, tok); |
| 2550 |
left = (left < calculate_constant5(L, P, tok)); |
| 2551 |
|
| 2552 |
} else if (tok->type == TOK_LESS_EQUAL) { |
| 2553 |
require_token(L, P, tok); |
| 2554 |
left = (left <= calculate_constant5(L, P, tok)); |
| 2555 |
|
| 2556 |
} else if (tok->type == TOK_GREATER) { |
| 2557 |
require_token(L, P, tok); |
| 2558 |
left = (left > calculate_constant5(L, P, tok)); |
| 2559 |
|
| 2560 |
} else if (tok->type == TOK_GREATER_EQUAL) { |
| 2561 |
require_token(L, P, tok); |
| 2562 |
left = (left >= calculate_constant5(L, P, tok)); |
| 2563 |
|
| 2564 |
} else { |
| 2565 |
return left; |
| 2566 |
} |
| 2567 |
} |
| 2568 |
} |
| 2569 |
|
| 2570 |
/* binary ==, != (left associative) */ |
| 2571 |
static int64_t calculate_constant7(lua_State* L, struct parser* P, struct token* tok) |
| 2572 |
{ |
| 2573 |
int64_t left = calculate_constant6(L, P, tok); |
| 2574 |
|
| 2575 |
for (;;) { |
| 2576 |
if (tok->type == TOK_EQUAL) { |
| 2577 |
require_token(L, P, tok); |
| 2578 |
left = (left == calculate_constant6(L, P, tok)); |
| 2579 |
|
| 2580 |
} else if (tok->type == TOK_NOT_EQUAL) { |
| 2581 |
require_token(L, P, tok); |
| 2582 |
left = (left != calculate_constant6(L, P, tok)); |
| 2583 |
|
| 2584 |
} else { |
| 2585 |
return left; |
| 2586 |
} |
| 2587 |
} |
| 2588 |
} |
| 2589 |
|
| 2590 |
/* binary & (left associative) */ |
| 2591 |
static int64_t calculate_constant8(lua_State* L, struct parser* P, struct token* tok) |
| 2592 |
{ |
| 2593 |
int64_t left = calculate_constant7(L, P, tok); |
| 2594 |
|
| 2595 |
for (;;) { |
| 2596 |
if (tok->type == TOK_BITWISE_AND) { |
| 2597 |
require_token(L, P, tok); |
| 2598 |
left = (left & calculate_constant7(L, P, tok)); |
| 2599 |
|
| 2600 |
} else { |
| 2601 |
return left; |
| 2602 |
} |
| 2603 |
} |
| 2604 |
} |
| 2605 |
|
| 2606 |
/* binary ^ (left associative) */ |
| 2607 |
static int64_t calculate_constant9(lua_State* L, struct parser* P, struct token* tok) |
| 2608 |
{ |
| 2609 |
int64_t left = calculate_constant8(L, P, tok); |
| 2610 |
|
| 2611 |
for (;;) { |
| 2612 |
if (tok->type == TOK_BITWISE_XOR) { |
| 2613 |
require_token(L, P, tok); |
| 2614 |
left = (left ^ calculate_constant8(L, P, tok)); |
| 2615 |
|
| 2616 |
} else { |
| 2617 |
return left; |
| 2618 |
} |
| 2619 |
} |
| 2620 |
} |
| 2621 |
|
| 2622 |
/* binary | (left associative) */ |
| 2623 |
static int64_t calculate_constant10(lua_State* L, struct parser* P, struct token* tok) |
| 2624 |
{ |
| 2625 |
int64_t left = calculate_constant9(L, P, tok); |
| 2626 |
|
| 2627 |
for (;;) { |
| 2628 |
if (tok->type == TOK_BITWISE_OR) { |
| 2629 |
require_token(L, P, tok); |
| 2630 |
left = (left | calculate_constant9(L, P, tok)); |
| 2631 |
|
| 2632 |
} else { |
| 2633 |
return left; |
| 2634 |
} |
| 2635 |
} |
| 2636 |
} |
| 2637 |
|
| 2638 |
/* binary && (left associative) */ |
| 2639 |
static int64_t calculate_constant11(lua_State* L, struct parser* P, struct token* tok) |
| 2640 |
{ |
| 2641 |
int64_t left = calculate_constant10(L, P, tok); |
| 2642 |
|
| 2643 |
for (;;) { |
| 2644 |
if (tok->type == TOK_LOGICAL_AND) { |
| 2645 |
require_token(L, P, tok); |
| 2646 |
left = (left && calculate_constant10(L, P, tok)); |
| 2647 |
|
| 2648 |
} else { |
| 2649 |
return left; |
| 2650 |
} |
| 2651 |
} |
| 2652 |
} |
| 2653 |
|
| 2654 |
/* binary || (left associative) */ |
| 2655 |
static int64_t calculate_constant12(lua_State* L, struct parser* P, struct token* tok) |
| 2656 |
{ |
| 2657 |
int64_t left = calculate_constant11(L, P, tok); |
| 2658 |
|
| 2659 |
for (;;) { |
| 2660 |
if (tok->type == TOK_LOGICAL_OR) { |
| 2661 |
require_token(L, P, tok); |
| 2662 |
left = (left || calculate_constant11(L, P, tok)); |
| 2663 |
|
| 2664 |
} else { |
| 2665 |
return left; |
| 2666 |
} |
| 2667 |
} |
| 2668 |
} |
| 2669 |
|
| 2670 |
/* ternary ?: (right associative) */ |
| 2671 |
static int64_t calculate_constant13(lua_State* L, struct parser* P, struct token* tok) |
| 2672 |
{ |
| 2673 |
int64_t left = calculate_constant12(L, P, tok); |
| 2674 |
|
| 2675 |
if (tok->type == TOK_QUESTION) { |
| 2676 |
int64_t middle, right; |
| 2677 |
require_token(L, P, tok); |
| 2678 |
middle = calculate_constant13(L, P, tok); |
| 2679 |
if (tok->type != TOK_COLON) { |
| 2680 |
luaL_error(L, "invalid ternery (? :) in constant on line %d", P->line); |
| 2681 |
} |
| 2682 |
require_token(L, P, tok); |
| 2683 |
right = calculate_constant13(L, P, tok); |
| 2684 |
return left ? middle : right; |
| 2685 |
|
| 2686 |
} else { |
| 2687 |
return left; |
| 2688 |
} |
| 2689 |
} |
| 2690 |
|
| 2691 |
int64_t calculate_constant(lua_State* L, struct parser* P) |
| 2692 |
{ |
| 2693 |
struct token tok; |
| 2694 |
int64_t ret; |
| 2695 |
require_token(L, P, &tok); |
| 2696 |
ret = calculate_constant13(L, P, &tok); |
| 2697 |
|
| 2698 |
if (tok.type != TOK_NIL) { |
| 2699 |
put_back(P); |
| 2700 |
} |
| 2701 |
|
| 2702 |
return ret; |
| 2703 |
} |
| 2704 |
|
| 2705 |
|
| 2706 |
|
| 2707 |
|