-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
2707 lines (2195 loc) · 81.4 KB
/
parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim: ts=4 sw=4 sts=4 et tw=78
* Portions copyright (c) 2015-present, Facebook, Inc. All rights reserved.
* Portions copyright (c) 2011 James R. McKaskill.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include "ffi.h"
#define IS_CONST(tok) (IS_LITERAL(tok, "const") || IS_LITERAL(tok, "__const") || IS_LITERAL(tok, "__const__"))
#define IS_VOLATILE(tok) (IS_LITERAL(tok, "volatile") || IS_LITERAL(tok, "__volatile") || IS_LITERAL(tok, "__volatile__"))
#define IS_RESTRICT(tok) (IS_LITERAL(tok, "restrict") || IS_LITERAL(tok, "__restrict") || IS_LITERAL(tok, "__restrict__"))
#define IS_INLINE(tok) (IS_LITERAL(tok, "inline") || IS_LITERAL(tok, "__inline") || IS_LITERAL(tok, "__inline__"))
enum etoken {
TOK_NIL,
TOK_NUMBER,
TOK_STRING,
TOK_TOKEN,
/* the order of these values must match the token strings in lex.c */
TOK_3_BEGIN,
TOK_VA_ARG,
TOK_2_BEGIN,
TOK_LEFT_SHIFT, TOK_RIGHT_SHIFT, TOK_LOGICAL_AND, TOK_LOGICAL_OR, TOK_LESS_EQUAL,
TOK_GREATER_EQUAL, TOK_EQUAL, TOK_NOT_EQUAL,
TOK_1_BEGIN,
TOK_OPEN_CURLY, TOK_CLOSE_CURLY, TOK_SEMICOLON, TOK_COMMA, TOK_COLON,
TOK_ASSIGN, TOK_OPEN_PAREN, TOK_CLOSE_PAREN, TOK_OPEN_SQUARE, TOK_CLOSE_SQUARE,
TOK_DOT, TOK_AMPERSAND, TOK_LOGICAL_NOT, TOK_BITWISE_NOT, TOK_MINUS,
TOK_PLUS, TOK_STAR, TOK_DIVIDE, TOK_MODULUS, TOK_LESS,
TOK_GREATER, TOK_BITWISE_XOR, TOK_BITWISE_OR, TOK_QUESTION, TOK_POUND,
TOK_REFERENCE = TOK_AMPERSAND,
TOK_MULTIPLY = TOK_STAR,
TOK_BITWISE_AND = TOK_AMPERSAND,
};
struct token {
enum etoken type;
int64_t integer;
const char* str;
size_t size;
};
#define IS_LITERAL(TOK, STR) \
(((TOK).size == sizeof(STR) - 1) && 0 == memcmp((TOK).str, STR, sizeof(STR) - 1))
/* the order of tokens _must_ match the order of the enum etoken enum */
static char tok3[][4] = {
"...", /* unused ">>=", "<<=", */
};
static char tok2[][3] = {
"<<", ">>", "&&", "||", "<=",
">=", "==", "!=",
/* unused "+=", "-=", "*=", "/=", "%=", "&=", "^=", "|=", "++", "--", "->", "::", */
};
static char tok1[] = {
'{', '}', ';', ',', ':',
'=', '(', ')', '[', ']',
'.', '&', '!', '~', '-',
'+', '*', '/', '%', '<',
'>', '^', '|', '?', '#'
};
static const char* etype_tostring(int type);
static const char* etype_tostring(int type)
{
switch (type) {
case VOID_TYPE: return "void";
case DOUBLE_TYPE: return "double";
case FLOAT_TYPE: return "float";
case COMPLEX_DOUBLE_TYPE: return "complex double";
case COMPLEX_FLOAT_TYPE: return "complex float";
case BOOL_TYPE: return "bool";
case INT8_TYPE: return "int8";
case INT16_TYPE: return "int16";
case INT32_TYPE: return "int32";
case INT64_TYPE: return "int64";
case INTPTR_TYPE: return "intptr";
case ENUM_TYPE: return "enum";
case UNION_TYPE: return "union";
case STRUCT_TYPE: return "struct";
case FUNCTION_PTR_TYPE: return "function ptr";
case FUNCTION_TYPE: return "function";
default: return "invalid";
}
}
static void debug_print_type(const struct ctype* ct)
{
printf("%s:%d is reference %d \n", __FILE__, __LINE__, ct->is_reference);
printf("%s:%d is pointers %d \n", __FILE__, __LINE__, ct->pointers);
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",
/* sz */
ct->base_size,
ct->array_size,
ct->offset,
/* align */
ct->align_mask,
/* ptr */
ct->is_array,
ct->pointers,
ct->const_mask,
/* type */
ct->is_unsigned ? "u" : "",
etype_tostring(ct->type),
ct->is_reference,
ct->is_defined,
ct->is_null,
/* name */
ct->has_member_name,
/* call */
ct->calling_convention,
ct->has_var_arg,
/* var */
ct->is_variable_array,
ct->is_variable_struct,
ct->variable_size_known,
/* bit */
ct->is_bitfield,
ct->has_bitfield,
ct->bit_offset,
ct->bit_size,
/* jit */
ct->is_jitted);
}
static int next_token(lua_State* L, struct parser* P, struct token* tok)
{
size_t i;
const char* s = P->next;
/* UTF8 BOM */
if (s[0] == '\xEF' && s[1] == '\xBB' && s[2] == '\xBF') {
s += 3;
}
/* consume whitespace and comments */
for (;;) {
/* consume whitespace */
while(*s == '\t' || *s == '\n' || *s == ' ' || *s == '\v' || *s == '\r') {
if (*s == '\n') {
P->line++;
}
s++;
}
/* consume comments */
if (*s == '/' && *(s+1) == '/') {
s = strchr(s, '\n');
if (!s) {
luaL_error(L, "non-terminated comment");
}
} else if (*s == '/' && *(s+1) == '*') {
s += 2;
for (;;) {
if (s[0] == '\0') {
luaL_error(L, "non-terminated comment");
} else if (s[0] == '*' && s[1] == '/') {
s += 2;
break;
} else if (s[0] == '\n') {
P->line++;
}
s++;
}
} else if (*s == '\0') {
tok->type = TOK_NIL;
return 0;
} else {
break;
}
}
P->prev = s;
for (i = 0; i < sizeof(tok3) / sizeof(tok3[0]); i++) {
if (s[0] == tok3[i][0] && s[1] == tok3[i][1] && s[2] == tok3[i][2]) {
tok->type = (enum etoken) (TOK_3_BEGIN + 1 + i);
P->next = s + 3;
goto end;
}
}
for (i = 0; i < sizeof(tok2) / sizeof(tok2[0]); i++) {
if (s[0] == tok2[i][0] && s[1] == tok2[i][1]) {
tok->type = (enum etoken) (TOK_2_BEGIN + 1 + i);
P->next = s + 2;
goto end;
}
}
for (i = 0; i < sizeof(tok1) / sizeof(tok1[0]); i++) {
if (s[0] == tok1[i]) {
tok->type = (enum etoken) (TOK_1_BEGIN + 1 + i);
P->next = s + 1;
goto end;
}
}
if (*s == '.' || *s == '-' || ('0' <= *s && *s <= '9')) {
/* number */
tok->type = TOK_NUMBER;
/* split out the negative case so we get the full range of bits for
* unsigned (eg to support 0xFFFFFFFF where sizeof(long) == 4)
*/
if (*s == '-') {
tok->integer = strtol(s, (char**) &s, 0);
} else {
tok->integer = strtoul(s, (char**) &s, 0);
}
while (*s == 'u' || *s == 'U' || *s == 'l' || *s == 'L') {
s++;
}
P->next = s;
goto end;
} else if (*s == '\'' || *s == '\"') {
/* "..." or '...' */
char quote = *s;
s++; /* jump over " */
tok->type = TOK_STRING;
tok->str = s;
while (*s != quote) {
if (*s == '\0' || (*s == '\\' && *(s+1) == '\0')) {
return luaL_error(L, "string not finished");
}
if (*s == '\\') {
s++;
}
s++;
}
tok->size = s - tok->str;
s++; /* jump over " */
P->next = s;
goto end;
} else if (('a' <= *s && *s <= 'z') || ('A' <= *s && *s <= 'Z') || *s == '_') {
/* tokens */
tok->type = TOK_TOKEN;
tok->str = s;
while (('a' <= *s && *s <= 'z') || ('A' <= *s && *s <= 'Z') || *s == '_' || ('0' <= *s && *s <= '9')) {
s++;
}
tok->size = s - tok->str;
P->next = s;
goto end;
} else {
return luaL_error(L, "invalid character %d", P->line);
}
end:
/*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);*/
return 1;
}
#define require_token(L, P, tok) require_token_line(L, P, tok, __FILE__, __LINE__)
static void require_token_line(lua_State* L, struct parser* P, struct token* tok, const char* file, int line)
{
if (!next_token(L, P, tok)) {
luaL_error(L, "unexpected end on line %s:%d", file, line);
}
}
static void check_token(lua_State* L, struct parser* P, int type, const char* str, const char* err, ...)
{
struct token tok;
if (!next_token(L, P, &tok) || tok.type != type || (tok.type == TOK_TOKEN && (tok.size != strlen(str) || memcmp(tok.str, str, tok.size) != 0))) {
va_list ap;
va_start(ap, err);
lua_pushvfstring(L, err, ap);
lua_error(L);
}
}
static void put_back(struct parser* P)
{ P->next = P->prev; }
int64_t calculate_constant(lua_State* L, struct parser* P);
static int g_name_key;
static int g_front_name_key;
static int g_back_name_key;
#ifndef max
#define max(a,b) ((a) < (b) ? (b) : (a))
#endif
#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
enum test {TEST};
/* Parses an enum definition from after the open curly through to the close
* curly. Expects the user table to be on the top of the stack
*/
static int parse_enum(lua_State* L, struct parser* P, struct ctype* type)
{
struct token tok;
int value = -1;
int ct_usr = lua_gettop(L);
for (;;) {
require_token(L, P, &tok);
assert(lua_gettop(L) == ct_usr);
if (tok.type == TOK_CLOSE_CURLY) {
break;
} else if (tok.type != TOK_TOKEN) {
return luaL_error(L, "unexpected token in enum at line %d", P->line);
}
lua_pushlstring(L, tok.str, tok.size);
require_token(L, P, &tok);
if (tok.type == TOK_COMMA || tok.type == TOK_CLOSE_CURLY) {
/* we have an auto calculated enum value */
value++;
} else if (tok.type == TOK_ASSIGN) {
/* we have an explicit enum value */
value = (int) calculate_constant(L, P);
require_token(L, P, &tok);
} else {
return luaL_error(L, "unexpected token in enum at line %d", P->line);
}
assert(lua_gettop(L) == ct_usr + 1);
/* add the enum value to the constants table */
push_upval(L, &constants_key);
lua_pushvalue(L, -2);
lua_pushinteger(L, value);
lua_rawset(L, -3);
lua_pop(L, 1);
assert(lua_gettop(L) == ct_usr + 1);
/* add the enum value to the enum usr value table */
lua_pushinteger(L, value);
lua_rawset(L, ct_usr);
if (tok.type == TOK_CLOSE_CURLY) {
break;
} else if (tok.type != TOK_COMMA) {
return luaL_error(L, "unexpected token in enum at line %d", P->line);
}
}
type->base_size = sizeof(enum test);
type->align_mask = sizeof(enum test) - 1;
assert(lua_gettop(L) == ct_usr);
return 0;
}
static void calculate_member_position(lua_State* L, struct parser* P, struct ctype* ct, struct ctype* mt, int* pbit_offset, int* pbitfield_type)
{
int bit_offset = *pbit_offset;
if (ct->type == UNION_TYPE) {
size_t msize;
if (mt->is_variable_struct || mt->is_variable_array) {
luaL_error(L, "NYI: variable sized members in unions");
return;
} else if (mt->is_bitfield) {
msize = (mt->align_mask + 1);
#ifdef _WIN32
/* MSVC has a bug where it doesn't update the alignment of
* a union for bitfield members. */
mt->align_mask = 0;
#endif
} else if (mt->is_array) {
msize = mt->array_size * (mt->pointers > 1 ? sizeof(void*) : mt->base_size);
} else {
msize = mt->pointers ? sizeof(void*) : mt->base_size;
}
ct->base_size = max(ct->base_size, msize);
} else if (mt->is_bitfield) {
if (mt->has_member_name && mt->bit_size == 0) {
luaL_error(L, "zero length bitfields must be unnamed on line %d", P->line);
}
#if defined _WIN32
/* MSVC uses a seperate storage unit for each size. This is aligned
* before the first bitfield. :0 finishes up the storage unit using
* the greater alignment of the storage unit or the type used with the
* :0. This is equivalent to the :0 always creating a new storage
* unit, but not necesserily using it yet.
*/
if (*pbitfield_type == -1 && mt->bit_size == 0) {
/* :0 not after a bitfield are ignored */
return;
}
{
int different_storage = mt->align_mask != *pbitfield_type;
int no_room_left = bit_offset + mt->bit_size > (mt->align_mask + 1) * CHAR_BIT;
if (different_storage || no_room_left || !mt->bit_size) {
ct->base_size += (bit_offset + CHAR_BIT - 1) / CHAR_BIT;
bit_offset = 0;
if (*pbitfield_type >= 0) {
ct->base_size = ALIGN_UP(ct->base_size, *pbitfield_type);
}
ct->base_size = ALIGN_UP(ct->base_size, mt->align_mask);
}
}
mt->bit_offset = bit_offset;
mt->offset = ct->base_size;
*pbitfield_type = mt->align_mask;
bit_offset += mt->bit_size;
// #elif defined OS_OSX
// /* OSX doesn't use containers and bitfields are not aligned. So
// * bitfields never add any padding, except for :0 which still forces
// * an alignment based off the type used with the :0 */
// if (mt->bit_size) {
// mt->offset = ct->base_size;
// mt->bit_offset = bit_offset;
// bit_offset += mt->bit_size;
// ct->base_size += bit_offset / CHAR_BIT;
// bit_offset = bit_offset % CHAR_BIT;
// } else {
// ct->base_size += (bit_offset + CHAR_BIT - 1) / CHAR_BIT;
// ct->base_size = ALIGN_UP(ct->base_size, mt->align_mask);
// bit_offset = 0;
// }
// if (!mt->has_member_name) {
// /* unnamed bitfields don't update the struct alignment */
// mt->align_mask = 0;
// }
#elif defined __GNUC__
/* GCC tries to pack bitfields in as close as much as possible, but
* still making sure that they don't cross alignment boundaries.
* :0 forces an alignment based off the type used with the :0
*/
int bits_used = (ct->base_size - ALIGN_DOWN(ct->base_size, mt->align_mask)) * CHAR_BIT + bit_offset;
int need_to_realign = bits_used + mt->bit_size > mt->base_size * CHAR_BIT;
if (!mt->is_packed && (!mt->bit_size || need_to_realign)) {
ct->base_size += (bit_offset + CHAR_BIT - 1) / CHAR_BIT;
ct->base_size = ALIGN_UP(ct->base_size, mt->align_mask);
bit_offset = 0;
}
mt->bit_offset = bit_offset;
mt->offset = ct->base_size;
bit_offset += mt->bit_size;
ct->base_size += bit_offset / CHAR_BIT;
bit_offset = bit_offset % CHAR_BIT;
if (!mt->has_member_name) {
#ifndef ARCH_ARM64
/* unnamed bitfields don't update the struct alignment in X86-64
* however it seem to have an effect in ARMv8 */
mt->align_mask = 0;
#else
#if defined OS_OSX
/* unnamed bitfields don't update the struct alignment in X86-64
* and in ARMv* Apple Mac M1
* however it seem to have an effect in ubuntu linux ARMv8 */
mt->align_mask = 0;
#endif
#endif
}
#else
#error
#endif
} else {
/* finish up the current bitfield storage unit */
ct->base_size += (bit_offset + CHAR_BIT - 1) / CHAR_BIT;
bit_offset = 0;
if (*pbitfield_type >= 0) {
ct->base_size = ALIGN_UP(ct->base_size, *pbitfield_type);
}
*pbitfield_type = -1;
ct->base_size = ALIGN_UP(ct->base_size, mt->align_mask);
mt->offset = ct->base_size;
if (mt->is_variable_array) {
ct->is_variable_struct = 1;
ct->variable_increment = mt->pointers > 1 ? sizeof(void*) : mt->base_size;
} else if (mt->is_variable_struct) {
assert(!mt->variable_size_known && !mt->is_array && !mt->pointers);
ct->base_size += mt->base_size;
ct->is_variable_struct = 1;
ct->variable_increment = mt->variable_increment;
} else if (mt->is_array) {
ct->base_size += mt->array_size * (mt->pointers > 1 ? sizeof(void*) : mt->base_size);
} else {
ct->base_size += mt->pointers ? sizeof(void*) : mt->base_size;
}
}
/* increase the outer struct/union alignment if needed */
if (mt->align_mask > (int) ct->align_mask) {
ct->align_mask = mt->align_mask;
}
if (mt->has_bitfield || mt->is_bitfield) {
ct->has_bitfield = 1;
}
*pbit_offset = bit_offset;
}
static int copy_submembers(lua_State* L, int to_usr, int from_usr, const struct ctype* ft, int* midx)
{
struct ctype ct;
int i, sublen;
from_usr = lua_absindex(L, from_usr);
to_usr = lua_absindex(L, to_usr);
/* integer keys */
sublen = (int) lua_rawlen(L, from_usr);
for (i = 1; i <= sublen; i++) {
lua_rawgeti(L, from_usr, i);
//ct = *(const struct ctype*) lua_touserdata(L, -1);
memcpy(&ct, (const struct ctype*) lua_touserdata(L, -1), sizeof(struct ctype));
ct.offset += ft->offset;
lua_getuservalue(L, -1);
push_ctype(L, -1, &ct);
lua_rawseti(L, to_usr, (*midx)++);
lua_pop(L, 2); /* ctype, user value */
}
/* string keys */
lua_pushnil(L);
while (lua_next(L, from_usr)) {
if (lua_type(L, -2) == LUA_TSTRING) {
struct ctype ct;
//struct ctype ct = *(const struct ctype*) lua_touserdata(L, -1);
memcpy(&ct, (const struct ctype*) lua_touserdata(L, -1), sizeof(struct ctype));
ct.offset += ft->offset;
lua_getuservalue(L, -1);
/* uservalue[sub_mname] = new_sub_mtype */
lua_pushvalue(L, -3);
push_ctype(L, -2, &ct);
lua_rawset(L, to_usr);
lua_pop(L, 1); /* remove submember user value */
}
lua_pop(L, 1);
}
return 0;
}
static int add_member(lua_State* L, int ct_usr, int mname, int mbr_usr, const struct ctype* mt, int* midx)
{
ct_usr = lua_absindex(L, ct_usr);
mname = lua_absindex(L, mname);
push_ctype(L, mbr_usr, mt);
/* usrvalue[mbr index] = pushed mtype */
lua_pushvalue(L, -1);
lua_rawseti(L, ct_usr, (*midx)++);
/* set usrvalue[mname] = pushed mtype */
lua_pushvalue(L, mname);
lua_pushvalue(L, -2);
lua_rawset(L, ct_usr);
/* set usrvalue[mtype] = mname */
lua_pushvalue(L, -1);
lua_pushvalue(L, mname);
lua_rawset(L, ct_usr);
lua_pop(L, 1);
return 0;
}
/* Parses a struct from after the open curly through to the close curly.
*/
static int parse_struct(lua_State* L, struct parser* P, int tmp_usr, const struct ctype* ct)
{
struct token tok;
int midx = 1;
int top = lua_gettop(L);
tmp_usr = lua_absindex(L, tmp_usr);
/* parse members */
for (;;) {
struct ctype mbase;
assert(lua_gettop(L) == top);
/* see if we're at the end of the struct */
require_token(L, P, &tok);
if (tok.type == TOK_CLOSE_CURLY) {
break;
} else if (ct->is_variable_struct) {
return luaL_error(L, "can't have members after a variable sized member on line %d", P->line);
} else {
put_back(P);
}
/* members are of the form
* <base type> <arg>, <arg>, <arg>;
* eg struct foo bar, *bar2[2];
* mbase is 'struct foo'
* mtype is '' then '*[2]'
* mname is 'bar' then 'bar2'
*/
parse_type(L, P, &mbase);
for (;;) {
struct token mname;
struct ctype mt = mbase;
memset(&mname, 0, sizeof(mname));
if (ct->is_variable_struct) {
return luaL_error(L, "can't have members after a variable sized member on line %d", P->line);
}
assert(lua_gettop(L) == top + 1);
parse_argument(L, P, -1, &mt, &mname, NULL);
assert(lua_gettop(L) == top + 2);
if (!mt.is_defined && (mt.pointers - mt.is_array) == 0) {
return luaL_error(L, "member type is undefined on line %d", P->line);
}
if (mt.type == VOID_TYPE && (mt.pointers - mt.is_array) == 0) {
return luaL_error(L, "member type can not be void on line %d", P->line);
}
mt.has_member_name = (mname.size > 0);
lua_pushlstring(L, mname.str, mname.size);
add_member(L, tmp_usr, -1, -2, &mt, &midx);
/* pop the usr value from push_argument and the member name */
lua_pop(L, 2);
assert(lua_gettop(L) == top + 1);
require_token(L, P, &tok);
if (tok.type == TOK_SEMICOLON) {
break;
} else if (tok.type != TOK_COMMA) {
luaL_error(L, "unexpected token in struct definition on line %d", P->line);
}
}
/* pop the usr value from push_type */
lua_pop(L, 1);
}
assert(lua_gettop(L) == top);
return 0;
}
static int calculate_struct_offsets(lua_State* L, struct parser* P, int ct_usr, struct ctype* ct, int tmp_usr)
{
int i;
int midx = 1;
int sz = (int) lua_rawlen(L, tmp_usr);
int bit_offset = 0;
int bitfield_type = -1;
ct_usr = lua_absindex(L, ct_usr);
tmp_usr = lua_absindex(L, tmp_usr);
for (i = 1; i <= sz; i++) {
struct ctype mt;
/* get the member type */
lua_rawgeti(L, tmp_usr, i);
//mt = *(const struct ctype*) lua_touserdata(L, -1);
memcpy(&mt, (const struct ctype*) lua_touserdata(L, -1), sizeof(struct ctype));
//debug_print_type(&mt);
/* get the member user table */
lua_getuservalue(L, -1);
/* get the member name */
lua_pushvalue(L, -2);
lua_rawget(L, tmp_usr);
calculate_member_position(L, P, ct, &mt, &bit_offset, &bitfield_type);
if (mt.has_member_name) {
assert(!lua_isnil(L, -1));
add_member(L, ct_usr, -1, -2, &mt, &midx);
} else if (mt.type == STRUCT_TYPE || mt.type == UNION_TYPE) {
/* With an unnamed member we copy all of the submembers into our
* usr value adjusting the offset as necessary. Note ctypes are
* immutable so need to push a new ctype to update the offset.
*/
copy_submembers(L, ct_usr, -2, &mt, &midx);
} else {
/* We ignore unnamed members that aren't structs or unions. These
* are there just to change the padding */
}
//debug_print_type(&mt);
lua_pop(L, 3);
}
/* finish up the current bitfield storage unit */
ct->base_size += (bit_offset + CHAR_BIT - 1) / CHAR_BIT;
/* only void is allowed 0 size */
if (ct->base_size == 0) {
ct->base_size = 1;
}
ct->base_size = ALIGN_UP(ct->base_size, ct->align_mask);
return 0;
}
/* copy over attributes that could be specified before the typedef eg
* __attribute__(packed) const type_t */
static void instantiate_typedef(struct parser* P, struct ctype* tt, const struct ctype* ft)
{
struct ctype pt = *tt;
*tt = *ft;
tt->const_mask |= pt.const_mask;
tt->is_packed = pt.is_packed;
if (tt->is_packed) {
tt->align_mask = 0;
} else {
/* Instantiate the typedef in the current packing. This may be
* further updated if a pointer is added or another alignment
* attribute is applied. If pt.align_mask is already non-zero than an
* increased alignment via __declspec(aligned(#)) has been set. */
tt->align_mask = max(min(P->align_mask, tt->align_mask), pt.align_mask);
}
}
/* this parses a struct or union starting with the optional
* name before the opening brace
* leaves the type usr value on the stack
*/
static int parse_record(lua_State* L, struct parser* P, struct ctype* ct)
{
struct token tok;
int top = lua_gettop(L);
require_token(L, P, &tok);
/* name is optional */
if (tok.type == TOK_TOKEN) {
/* declaration */
lua_pushlstring(L, tok.str, tok.size);
assert(lua_gettop(L) == top+1);
/* lookup the name to see if we've seen this type before */
push_upval(L, &types_key);
lua_pushvalue(L, -2);
lua_rawget(L, top+2);
assert(lua_gettop(L) == top+3);
if (lua_isnil(L, -1)) {
lua_pop(L, 1); /* pop the nil usr value */
lua_newtable(L); /* the new usr table */
/* stack layout is:
* top+1: record name
* top+2: types table
* top+3: new usr table
*/
lua_pushlightuserdata(L, &g_name_key);
lua_pushvalue(L, top+1);
lua_rawset(L, top+3); /* usr[name_key] = name */
lua_pushvalue(L, top+1);
push_ctype(L, top+3, ct);
lua_rawset(L, top+2); /* types[name] = new_ctype */
} else {
/* get the exsting declared type */
const struct ctype* prevt = (const struct ctype*) lua_touserdata(L, top+3);
if (prevt->type != ct->type) {
lua_getuservalue(L, top+3);
push_type_name(L, -1, ct);
push_type_name(L, top+3, prevt);
luaL_error(L, "type '%s' previously declared as '%s'", lua_tostring(L, -2), lua_tostring(L, -1));
}
instantiate_typedef(P, ct, prevt);
/* replace the ctype with its usr value */
lua_getuservalue(L, -1);
lua_replace(L, -2);
}
/* remove the extra name and types table */
lua_replace(L, -3);
lua_pop(L, 1);
assert(lua_gettop(L) == top + 1 && lua_istable(L, -1));
/* if a name is given then we may be at the end of the string
* eg for ffi.new('struct foo')
*/
if (!next_token(L, P, &tok)) {
return 0;
}
} else {
/* create a new unnamed record */
int num;
/* get the next unnamed number */
push_upval(L, &next_unnamed_key);
num = lua_tointeger(L, -1);
lua_pop(L, 1);
/* increment the unnamed upval */
lua_pushinteger(L, num + 1);
set_upval(L, &next_unnamed_key);
lua_newtable(L); /* the new usr table - leave on stack */
/* usr[name_key] = num */
lua_pushlightuserdata(L, &g_name_key);
lua_pushfstring(L, "%d", num);
lua_rawset(L, -3);
}
if (tok.type != TOK_OPEN_CURLY) {
/* this may just be a declaration or use of the type as an argument or
* member */
put_back(P);
return 0;
}
if (ct->is_defined) {
return luaL_error(L, "redefinition in line %d", P->line);
}
assert(lua_gettop(L) == top + 1 && lua_istable(L, -1));
if (ct->type == ENUM_TYPE) {
parse_enum(L, P, ct);
} else {
/* we do a two stage parse, where we parse the content first and build up
* the temp user table. We then iterate over that to calculate the offsets
* and fill out ct_usr. This is so we can handle out of order members
* (eg vtable) and attributes specified at the end of the struct.
*/
lua_newtable(L);
parse_struct(L, P, -1, ct);
//debug_print_type(ct);
calculate_struct_offsets(L, P, -2, ct, -1);
//debug_print_type(ct);
assert(lua_gettop(L) == top + 2 && lua_istable(L, -1));
lua_pop(L, 1);
}
assert(lua_gettop(L) == top + 1 && lua_istable(L, -1));
set_defined(L, -1, ct);
assert(lua_gettop(L) == top + 1);
return 0;
}
/* parses single or multi work built in types, and pushes it onto the stack */
static int parse_type_name(lua_State* L, struct parser* P)
{
struct token tok;
int flags = 0;
enum {
UNSIGNED = 0x01,
SIGNED = 0x02,
LONG = 0x04,
SHORT = 0x08,
INT = 0x10,
CHAR = 0x20,
LONG_LONG = 0x40,
INT8 = 0x80,
INT16 = 0x100,
INT32 = 0x200,
INT64 = 0x400,
DOUBLE = 0x800,
FLOAT = 0x1000,
COMPLEX = 0x2000,
};
require_token(L, P, &tok);
/* we have to manually decode the builtin types since they can take up
* more then one token
*/
for (;;) {
if (tok.type != TOK_TOKEN) {
break;
} else if (IS_LITERAL(tok, "unsigned")) {
flags |= UNSIGNED;
} else if (IS_LITERAL(tok, "signed")) {
flags |= SIGNED;
} else if (IS_LITERAL(tok, "short")) {
flags |= SHORT;
} else if (IS_LITERAL(tok, "char")) {
flags |= CHAR;
} else if (IS_LITERAL(tok, "long")) {
flags |= (flags & LONG) ? LONG_LONG : LONG;
} else if (IS_LITERAL(tok, "int")) {
flags |= INT;
} else if (IS_LITERAL(tok, "__int8")) {
flags |= INT8;
} else if (IS_LITERAL(tok, "__int16")) {
flags |= INT16;
} else if (IS_LITERAL(tok, "__int32")) {
flags |= INT32;
} else if (IS_LITERAL(tok, "__int64")) {
flags |= INT64;
} else if (IS_LITERAL(tok, "double")) {
flags |= DOUBLE;
} else if (IS_LITERAL(tok, "float")) {
flags |= FLOAT;
} else if (IS_LITERAL(tok, "complex") || IS_LITERAL(tok, "_Complex")) {
flags |= COMPLEX;
} else if (IS_LITERAL(tok, "register")) {
/* ignore */
} else {
break;
}
if (!next_token(L, P, &tok)) {
break;
}
}
if (flags) {
put_back(P);
}
if (flags & CHAR) {
if (flags & SIGNED) {
lua_pushliteral(L, "int8_t");