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
|
%{
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include "demangler-int.h"
#include "itanium.h"
#include "../../common/extstr.h"
/**
* cf. http://www.codesourcery.com/cxx-abi/abi.html#mangling
*/
/* Taille du bond pour les allocations */
#define ITANIUM_ALLOC_CLUSTER 10
/* Décodeur de nom d'éléments selon la définition Intel */
typedef struct _itanium_demangler
{
name_demangler common; /* A laisser en premier */
char *identifier; /* Identifiant en construction */
size_t id_allocated; /* Taille allouée en mémoire */
size_t id_used; /* Longueur de la chaîne */
bool use_templates; /* Utilisation des gabarits */
GSList *routines; /* Routines en cours de maj */
} itanium_demangler;
/* Indique si une chaîne peut être traitée par le décodeur. */
bool can_be_itanium_demangled(itanium_demangler *, const char *);
/* Procède au décodage d'une chaîne de caractères. */
GBinRoutine *demangle_itanium_routine(itanium_demangler *, const char *);
/* Réinitialise le constructeur d'identifiants. */
void reset_itanium_identifier_building(itanium_demangler *);
/* Construit à la volée un identifiant. */
void build_itanium_identifier(itanium_demangler *, char);
/* Place sous le feu des projecteurs une nouvelle routine. */
static GBinRoutine *push_routine(itanium_demangler *, GBinRoutine *);
/* Remet une ancienne routine sous le feu des projecteurs. */
static GBinRoutine *pop_routine(itanium_demangler *);
/* Liste de substitutions */
typedef GSList substi_list;
/* Met en place une liste pour recueillir les substitutions. */
static substi_list *create_substitution_list(void);
/* Ajoute un élément dans une liste de substitutions. */
static void add_substitution_item(substi_list *, GOpenidaType *);
/* Fournit le nième élément d'une liste de substitutions. */
static GOpenidaType *get_substitution_item(substi_list *, guint);
/* Borne la longueur d'une chaîne à lire. */
extern void set_itanium_text_length(unsigned int);
char *strmerge(char *str1, const char *sep, char *str2);
char *strmerge(char *str1, const char *sep, char *str2)
{
char *result;
if (str1 == NULL) result = str2;
else if (str2 == NULL) result = str1;
else
{
result = (char *)calloc(strlen(str1) + strlen(sep) + strlen(str2) + 1, sizeof(char));
strcpy(result, str1);
strcat(result, sep);
strcat(result, str2);
free(str1);
free(str2);
}
return result;
}
%}
%union {
struct
{
union
{
char *str;
void/*GOpenidaType*/ *type;
};
int/*bool*/ is_string;
} str_or_type;
char car;
char *text;
unsigned int val;
short/*s16_t*/ s16_val;
void /*GOpenidaType*/ *type;
void /*GTemplateType*/ *templtype;
void/*simple_variable*/ *simple;
void/*complex_variable*/ *complex;
void/*variable*/ *var;
int/*VariableQualifier*/ qual;
void/*GSList*/ *slist;
}
%parse-param { itanium_demangler *demangler }
%parse-param { substi_list *substitutions }
%parse-param { GBinRoutine *routine }
%token ITANIUM_SIGNATURE
%token EE NN II FF LL
%token C1 C2 C3 D1 D2 D3
%token V W B C A H S T I J L M X Y N O F D E G Z DD DE DF DH DI DS U
%token TP TR TO TC TG
%token QR QV QK
%token ST SA SB SS SI SO SD
%token OPER_NEW OPER_NEW_ARRAY OPER_DELETE OPER_DELETE_ARRAY OPER_PLUS_UNARY OPER_NEG_UNARY
%token OPER_AND_UNARY OPER_DE_UNARY OPER_COMPL OPER_PLUS OPER_MINUS OPER_MUL OPER_DIV OPER_MOD
%token OPER_AND OPER_OR OPER_EXCL_OR OPER_AS OPER_PLUS_EQ OPER_MINUS_EQ OPER_MUL_EQ OPER_DIV_EQ
%token OPER_MOD_EQ OPER_AND_EQ OPER_OR_EQ OPER_EXCL_OR_EQ OPER_LEFT_SHIFT OPER_RIGHT_SHIFT
%token OPER_LEFT_SHIFT_EQ OPER_RIGHT_SHIFT_EQ OPER_EQUAL OPER_NOT_EQ OPER_LESS OPER_GREATER
%token OPER_LESS_EQ OPER_GREATER_EQ OPER_NOT OPER_AND_AND OPER_OR_OR OPER_PLUS_PLUS OPER_MINUS_MINUS
%token OPER_COMMA OPER_PRIV_MEMB OPER_POINTER_TO OPER_CLASS OPER_INDEX
%token SUBSTI_FIRST SUBSTI_N
%token TPARAM_FIRST TPARAM_N
%token NUMBER CHAR
%token LPART MPART RPART
%token POSITION POSITION_ABS IMAGE
%token RECT
%token O_BRACKET C_BRACKET
%token PATH
%token DEC_16
%token TEXT
%type <text> TEXT PATH
%type <s16_val> DEC_16
%type <str_or_type> name
%type <text> unscoped_name
%type <type> nested_name
%type <templtype> unscoped_template_name
%type <text> unqualified_name operator_name
%type <text> prefix template_prefix source_name
%type <type> template_param template_template_param
%type <type> function_type type
%type <qual> qualifiers
%type <simple> builtin_type
%type <type> class_enum_type
%type <slist> template_args template_arg_list
%type <type> template_arg expr_primary
%type <text> substitution
%type <car> CHAR
%type <val> NUMBER SUBSTI_N TPARAM_N
%{
/* De lexi.c... */
/*int yylex(YYSTYPE *, YYLTYPE *);*/
void yyrestart(FILE *);
typedef struct yy_buffer_state *YY_BUFFER_STATE;
extern YY_BUFFER_STATE yy_scan_string(const char *);
extern void yy_delete_buffer(YY_BUFFER_STATE);
/* Affiche un message d'erreur concernant l'analyse. */
/*int yyerror(const YYLTYPE *, bool, char **, unsigned char *, char *);*/
%}
%%
input:
ITANIUM_SIGNATURE encoding
;
encoding:
name { $1.is_string ? g_binary_routine_set_name(routine, $1.str) : g_binary_routine_set_name_from_type(routine, $1.type); }
bare_function_type { ; }
;
name:
nested_name { $$.type = $1; $$.is_string = false; }
| unscoped_name { $$.str = $1; $$.is_string = true; }
| unscoped_template_name template_args { $$.type = $1; $$.is_string = false; g_template_type_add_params($1, $2); }
;
unscoped_name:
unqualified_name { $$ = $1; }
| ST unqualified_name { $$ = strprep($2, "std::"); }
;
unscoped_template_name:
unscoped_name { $$ = g_template_type_new($1, NULL); add_substitution_item(substitutions, $$); }
| substitution {
$$ = NULL;/*g_openida_type_to_string($1)*/;
/*printf("unscoped sub name :: %s\n", $$);
g_object_unref(G_OBJECT($1));*/
}
;
nested_name:
NN prefix unqualified_name EE {
$$ = ($3 != NULL ? strmerge($2, "::", $3) : $2);
$$ = g_class_enum_type_new(CET_UNKNOWN, $$);
}
| NN template_prefix template_args EE { $$ = g_template_type_new($2, $3); }
;
prefix:
/* vide */ { $$ = NULL; /*printf("passage E\n")*/; }
| prefix unqualified_name { $$ = ($2 != NULL ? strmerge($1, "::", $2) : $1); }
| template_prefix template_args { $$ = g_template_type_new($1, $2); $$ = g_openida_type_to_string($$); /* FIXME : retourner un type ! */}
| substitution {
$$ = g_openida_type_to_string($1);
printf("prefix substi :: %s\n", $$);
g_object_unref(G_OBJECT($1));
}
;
template_prefix:
prefix unqualified_name { $$ = ($2 != NULL ? strmerge($1, "::", $2) : $1); }
| template_param {
$$ = g_openida_type_to_string($1);
g_object_unref(G_OBJECT($1));
}
;
unqualified_name:
operator_name { printf("dup :: '%s'\n", $1); fflush(NULL) ;$$ = strdup($1) ; }
| ctor_dtor_name { printf("passage C\n"); $$ = NULL; }
| source_name { $$ = $1; }
;
source_name:
NUMBER { set_itanium_text_length($1); reset_itanium_identifier_building(demangler); }
identifier { $$ = strdup(demangler->identifier); printf("==source name== :: %s\n", $$); }
;
identifier:
identifier CHAR { build_itanium_identifier(demangler, $2); }
| CHAR { build_itanium_identifier(demangler, $1); }
;
operator_name:
OPER_NEW { $$ = "new"; }
| OPER_NEW_ARRAY { $$ = "new[]"; }
| OPER_DELETE { $$ = "delete"; }
| OPER_DELETE_ARRAY { $$ = "delete[]"; }
| OPER_PLUS_UNARY { $$ = "+"; }
| OPER_NEG_UNARY { $$ = "-"; }
| OPER_AND_UNARY { $$ = "&"; }
| OPER_DE_UNARY { $$ = "*"; }
| OPER_COMPL { $$ = "~"; }
| OPER_PLUS { $$ = "+"; }
| OPER_MINUS { $$ = "-"; }
| OPER_MUL { $$ = "*"; }
| OPER_DIV { $$ = "/"; }
| OPER_MOD { $$ = "%"; }
| OPER_AND { $$ = "&"; }
| OPER_OR { $$ = "|"; }
| OPER_EXCL_OR { $$ = "^"; }
| OPER_AS { $$ = "="; }
| OPER_PLUS_EQ { $$ = "+="; }
| OPER_MINUS_EQ { $$ = "-="; }
| OPER_MUL_EQ { $$ = "*="; }
| OPER_DIV_EQ { $$ = "/="; }
| OPER_MOD_EQ { $$ = "%)"; }
| OPER_AND_EQ { $$ = "&="; }
| OPER_OR_EQ { $$ = "|="; }
| OPER_EXCL_OR_EQ { $$ = "^="; }
| OPER_LEFT_SHIFT { $$ = "<<"; }
| OPER_RIGHT_SHIFT { $$ = ">>"; }
| OPER_LEFT_SHIFT_EQ { $$ = "<<="; }
| OPER_RIGHT_SHIFT_EQ { $$ = ">>="; }
| OPER_EQUAL { $$ = "=="; }
| OPER_NOT_EQ { $$ = "!="; }
| OPER_LESS { $$ = "<"; }
| OPER_GREATER { $$ = ">"; }
| OPER_LESS_EQ { $$ = "<="; }
| OPER_GREATER_EQ { $$ = ">="; }
| OPER_NOT { $$ = "!"; }
| OPER_AND_AND { $$ = "&&"; }
| OPER_OR_OR { $$ = "||"; }
| OPER_PLUS_PLUS { $$ = "++"; }
| OPER_MINUS_MINUS { $$ = "--"; }
| OPER_COMMA { $$ = ","; }
| OPER_PRIV_MEMB { $$ = "->*"; }
| OPER_POINTER_TO { $$ = "->"; }
| OPER_CLASS { $$ = "()"; }
| OPER_INDEX { $$ = "[]"; }
;
ctor_dtor_name:
C1 { g_binary_routine_set_type(routine, RTT_CONSTRUCTOR); }
| C2 { g_binary_routine_set_type(routine, RTT_CONSTRUCTOR); }
| C3 { g_binary_routine_set_type(routine, RTT_CONSTRUCTOR); }
| D1 { g_binary_routine_set_type(routine, RTT_DESTRUCTOR); }
| D2 { g_binary_routine_set_type(routine, RTT_DESTRUCTOR); }
| D3 { g_binary_routine_set_type(routine, RTT_DESTRUCTOR); }
;
function_type:
FF { routine = push_routine(demangler, routine); }
bare_function_type EE {
$$ = g_encapsulated_type_new(ECT_ROUTINE, routine);
routine = pop_routine(demangler);
if (routine == NULL) YYERROR;
}
;
type:
builtin_type { $$ = $1; printf("builtin '%s'\n", g_openida_type_to_string($1)); }
| class_enum_type { $$ = $1; add_substitution_item(substitutions, $1); }
| substitution { $$ = $1; }
| template_param { $$ = $1; }
| template_template_param template_args {
$$ = g_template_type_new(g_openida_type_to_string($1), NULL);
g_object_unref($1);
g_template_type_add_params($$, $2);
}
| function_type { $$ = $1; }
| qualifiers type {
$$ = g_openida_type_dup($2);
g_openida_type_add_qualifier($$, $1);
add_substitution_item(substitutions, $$);
}
| TP type { $$ = g_encapsulated_type_new(ECT_POINTER, $2); add_substitution_item(substitutions, $$); }
| TR type { $$ = g_encapsulated_type_new(ECT_REFERENCE, $2); add_substitution_item(substitutions, $$); }
| TO type { $$ = g_encapsulated_type_new(ECT_RVALUE_REF, $2); add_substitution_item(substitutions, $$); }
| TC type { $$ = g_encapsulated_type_new(ECT_COMPLEX, $2); add_substitution_item(substitutions, $$); }
| TG type { $$ = g_encapsulated_type_new(ECT_IMAGINARY, $2); add_substitution_item(substitutions, $$); }
;
qualifiers:
QR { $$ = TQF_RESTRICT; }
| QV { $$ = TQF_VOLATILE; }
| QK { $$ = TQF_CONST; }
;
builtin_type:
V { $$ = g_basic_type_new(BTP_VOID); }
| W { $$ = g_basic_type_new(BTP_WCHAR_T); }
| B { $$ = g_basic_type_new(BTP_BOOL); }
| C { $$ = g_basic_type_new(BTP_CHAR); }
| A { $$ = g_basic_type_new(BTP_SCHAR); }
| H { $$ = g_basic_type_new(BTP_UCHAR); }
| S { $$ = g_basic_type_new(BTP_SHORT); }
| T { $$ = g_basic_type_new(BTP_USHORT); }
| I { $$ = g_basic_type_new(BTP_INT); }
| J { $$ = g_basic_type_new(BTP_UINT); }
| L { $$ = g_basic_type_new(BTP_LONG); }
| M { $$ = g_basic_type_new(BTP_ULONG); }
| X { $$ = g_basic_type_new(BTP_LONG_LONG); }
| Y { $$ = g_basic_type_new(BTP_ULONG_LONG); }
| N { $$ = g_basic_type_new(BTP_INT128); }
| O { $$ = g_basic_type_new(BTP_UINT128); }
| F { $$ = g_basic_type_new(BTP_FLOAT); }
| D { $$ = g_basic_type_new(BTP_DOUBLE); }
| E { $$ = g_basic_type_new(BTP_LONG_DOUBLE); }
| G { $$ = g_basic_type_new(BTP_FLOAT128); }
| Z { $$ = g_basic_type_new(BTP_ELLIPSIS); }
| DD { $$ = g_basic_type_new(BTP_754R_64); }
| DE { $$ = g_basic_type_new(BTP_754R_128); }
| DF { $$ = g_basic_type_new(BTP_754R_32); }
| DH { $$ = g_basic_type_new(BTP_754R_16); }
| DI { $$ = g_basic_type_new(BTP_CHAR32_T); }
| DS { $$ = g_basic_type_new(BTP_CHAR16_T); }
| U source_name { $$ = g_basic_type_new(BTP_OTHER); /* TODO */ ; free($2); }
;
bare_function_type:
bare_function_type type { g_binary_routine_add_arg(routine, g_binary_variable_new($2)); }
| type {
if (demangler->use_templates)
g_binary_routine_set_return_type(routine, $1);
else
g_binary_routine_add_arg(routine, g_binary_variable_new($1));
}
;
class_enum_type:
name { $$ = $1.is_string ? g_class_enum_type_new(CET_UNKNOWN, $1.str) : $1.type; }
;
template_param:
TPARAM_FIRST {
const GOpenidaType *type = g_binary_routine_get_type_from_name(routine);
if (G_IS_TEMPLATE_TYPE(type))
$$ = g_template_type_get_param(G_TEMPLATE_TYPE(type), 0);
else $$ = NULL;
if ($$ == NULL) YYERROR;
}
| TPARAM_N {
const GOpenidaType *type = g_binary_routine_get_type_from_name(routine);
if (G_IS_TEMPLATE_TYPE(type))
$$ = g_template_type_get_param(G_TEMPLATE_TYPE(type), $1 + 1);
else $$ = NULL;
if ($$ == NULL) YYERROR;
}
;
template_template_param:
template_param { $$ = $1; }
| substitution { $$ = $1; }
;
template_args:
use_templates template_arg_list EE { $$ = $2; }
;
use_templates:
II { printf("new template !!!\n"); demangler->use_templates = true; }
;
template_arg_list:
template_arg_list template_arg { $$ = g_slist_prepend($1, $2); }
| template_arg { $$ = g_slist_prepend(NULL, $1); }
| template_args { g_class_enum_type_new(CET_UNKNOWN, "template params"); }
;
template_arg:
type { $$ = $1; }
| expr_primary { $$ = $1; }
;
expr_primary:
LL type NUMBER EE { $$ = $2; }
;
substitution:
ST { $$ = g_class_enum_type_new(CET_CLASS, "std"); }
| SA { $$ = g_class_enum_type_new(CET_CLASS, "std::allocator"); }
| SB { $$ = g_class_enum_type_new(CET_CLASS, "std::basic_string"); }
| SS { $$ = g_class_enum_type_new(CET_CLASS, "std::string"); }
| SI { $$ = g_class_enum_type_new(CET_CLASS, "std::istream"); }
| SO { $$ = g_class_enum_type_new(CET_CLASS, "std::ostream"); }
| SD { $$ = g_class_enum_type_new(CET_CLASS, "std::iostream"); }
| SUBSTI_FIRST { $$ = get_substitution_item(substitutions, 0); if ($$ == NULL) YYERROR; }
| SUBSTI_N { $$ = get_substitution_item(substitutions, $1 + 1); if ($$ == NULL) YYERROR; }
;
%%
/**
* Affiche un message d'erreur concernant l'analyse.
* @param yyloc informations concernant les coordonnées du soucis.
* @param hunt indique le type de passe réalisée.
* @param ucode code résultant compilé.
* @param index indice de commande à mettre à jour.
* @param msg indications humaines sur l'événement.
* @return 0.
*/
int yyerror(/*const YYLTYPE *yyloc, bool hunt, char **ucode, unsigned char *index, */char *msg)
{
fprintf(stderr, "ERREUR !\n");
fprintf(stderr, "%s\n", msg);
return -1;
}
/******************************************************************************
* *
* Paramètres : - *
* *
* Description : Définit un décodeur répondant à la norme d'Intel. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
name_demangler *create_itanium_demangler(void)
{
itanium_demangler *result; /* Structure à retourner */
result = (itanium_demangler *)calloc(1, sizeof(itanium_demangler));
NAME_DEMANGLER(result)->can_be_demangled = (can_be_demangled_fc)can_be_itanium_demangled;
NAME_DEMANGLER(result)->demangle_routine = (demangle_routine_fc)demangle_itanium_routine;
return NAME_DEMANGLER(result);
}
/******************************************************************************
* *
* Paramètres : demangler = décodeur à utiliser. *
* name = chaîne de caractères à analyser. *
* *
* Description : Indique si une chaîne peut être traitée par le décodeur. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool can_be_itanium_demangled(itanium_demangler *itanium, const char *name)
{
return (strncmp(name, "_Z", 2) == 0);
}
/******************************************************************************
* *
* Paramètres : demangler = décodeur à utiliser. *
* name = chaîne de caractères à décoder. *
* *
* Description : Procède au décodage d'une chaîne de caractères. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
GBinRoutine *demangle_itanium_routine(itanium_demangler *demangler, const char *name)
{
GBinRoutine *result; /* Construction à retourner */
substi_list *substitutions; /* Liste de substitutions */
YY_BUFFER_STATE buffer; /* Tampon pour bison */
int ret; /* Bilan de l'appel */
/* TODO : reset ! */
demangler->use_templates = false;
result = g_binary_routine_new();
substitutions = create_substitution_list();
buffer = yy_scan_string(name);
ret = yyparse(demangler, substitutions, result);
yy_delete_buffer(buffer);
//delete(substi_list *substitutions);
if (ret != 0)
{
/*delete_binary_routine(result); FIXME */
result = NULL;
}
return result;
}
/******************************************************************************
* *
* Paramètres : demangler = décodeur à mettre à jour. *
* *
* Description : Réinitialise le constructeur d'identifiants. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
void reset_itanium_identifier_building(itanium_demangler *demangler)
{
demangler->id_used = 0;
}
/******************************************************************************
* *
* Paramètres : demangler = décodeur à mettre à jour. *
* value = caractère d'identifiant à mémoriser. *
* *
* Description : Construit à la volée un identifiant. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
void build_itanium_identifier(itanium_demangler *demangler, char value)
{
if ((demangler->id_used + 2) > demangler->id_allocated)
{
demangler->id_allocated += ITANIUM_ALLOC_CLUSTER;
demangler->identifier = (char *)realloc(demangler->identifier,
demangler->id_allocated * sizeof(char));
}
demangler->identifier[demangler->id_used++] = value;
demangler->identifier[demangler->id_used] = 0;
}
/******************************************************************************
* *
* Paramètres : demangler = décodeur à mettre à jour. *
* previous = routine à sauvegarder. *
* *
* Description : Place sous le feu des projecteurs une nouvelle routine. *
* *
* Retour : Nouvelle routine à compléter. *
* *
* Remarques : - *
* *
******************************************************************************/
static GBinRoutine *push_routine(itanium_demangler *demangler, GBinRoutine *previous)
{
GBinRoutine *result; /* Routine nouvelle à renvoyer */
result = g_binary_routine_new();
demangler->routines = g_slist_append(demangler->routines, previous);
return result;
}
/******************************************************************************
* *
* Paramètres : demangler = décodeur à mettre à jour. *
* *
* Description : Remet une ancienne routine sous le feu des projecteurs. *
* *
* Retour : Routine sauvegardée. *
* *
* Remarques : - *
* *
******************************************************************************/
static GBinRoutine *pop_routine(itanium_demangler *demangler)
{
GBinRoutine *result; /* Routine nouvelle à renvoyer */
GSList *last; /* Dernier élément */
last = g_slist_last(demangler->routines);
if (last == NULL) result = NULL;
else
{
result = G_BIN_ROUTINE(last->data);
demangler->routines = g_slist_remove(demangler->routines, result);
}
return result;
}
/******************************************************************************
* *
* Paramètres : - *
* *
* Description : Met en place une liste pour recueillir les substitutions. *
* *
* Retour : Nouvelle liste vierge pour les substitutions. *
* *
* Remarques : - *
* *
******************************************************************************/
static substi_list *create_substitution_list(void)
{
return g_slist_alloc();
}
/******************************************************************************
* *
* Paramètres : list = ensemble des substitions enregistrées. *
* type = nouvel élément à placer dans la liste des substitut°. *
* *
* Description : Ajoute un élément dans une liste de substitutions. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void add_substitution_item(substi_list *list, GOpenidaType *type)
{
g_object_ref(G_OBJECT(type));
printf("push %p\n", type);
g_slist_append(list, type);
}
/******************************************************************************
* *
* Paramètres : list = ensemble des substitions enregistrées. *
* n = indice de l'élément à fournir. *
* *
* Description : Fournit le nième élément d'une liste de substitutions. *
* *
* Retour : Type prêt à emploi. *
* *
* Remarques : - *
* *
******************************************************************************/
static GOpenidaType *get_substitution_item(substi_list *list, guint n)
{
printf("get [%u] == %p\n", n, g_slist_nth_data(list, n + 1));
return G_OPENIDA_TYPE(g_slist_nth_data(list, n + 1));
}
/******************************************************************************
* *
* Paramètres : demangler = décodeur à utiliser. *
* *
* Description : Procède au test de décodages de chaînes de caractères. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
#ifdef DEBUG
void test_itanium_demangling(itanium_demangler *demangler)
{
GBinRoutine *routine;
char *human;
char stop;
#define TEST_ITANIUM_MANGLING(name, ref) \
do \
{ \
printf(" >> %s\n", name); \
routine = demangle_itanium_routine(demangler, name); \
if (routine == NULL) \
{ \
printf("Error with %s\n", name); \
stop = true; \
} \
else \
{ \
human = g_binary_routine_to_string(routine); \
printf(" >> %s\n", human); \
stop = (strcmp(ref, human) != 0); \
printf(" >> ok ? %s\n", (!stop ? "oui" : "non")); \
if (stop) printf(" >> expected '%s'\n", ref); \
free(human); \
} \
if (stop) goto end_of_test; \
else printf("\n"); \
} \
while (0)
/**
* Tests de :
* http://www.codesourcery.com/public/cxx-abi/abi-examples.html#mangling
*/
TEST_ITANIUM_MANGLING("_Z1fv", "??? f(void)");
TEST_ITANIUM_MANGLING("_Z1fi", "??? f(int)");
TEST_ITANIUM_MANGLING("_Z3foo3bar", "??? foo(bar)");
TEST_ITANIUM_MANGLING("_Zrm1XS_", "??? %(X, X)");
TEST_ITANIUM_MANGLING("_ZplR1XS0_", "??? +(X &, X &)");
TEST_ITANIUM_MANGLING("_ZlsRK1XS1_", "??? <<(const X &, const X &)");
TEST_ITANIUM_MANGLING("_Z1fIiEvi", "void f<int>(int)");
TEST_ITANIUM_MANGLING("_Z5firstI3DuoEvS0_", "void first<Duo>(Duo)");
TEST_ITANIUM_MANGLING("_Z5firstI3DuoEvT_", "void first<Duo>(Duo)");
TEST_ITANIUM_MANGLING("_Z3fooIiPFidEiEvv", "void foo<int, int (*)(double), int>(void)");
TEST_ITANIUM_MANGLING("_ZN6System5Sound4beepEv", "??? System::Sound::beep(void)");
TEST_ITANIUM_MANGLING("_Z1fI1XE vPV N1AIT_E1TE", "void f<X>(volatile A<X>::T *)");
//// TODO :: TEST_ITANIUM_MANGLING("_ZngILi42EE v N1A I XplT_Li2EE E 1TE", "");
TEST_ITANIUM_MANGLING("_Z4makeI7FactoryiE T_IT0_E v", "Factory<int> make<Factory, int>(void)");
TEST_ITANIUM_MANGLING("_ZlsRSoRKSs", "??? <<(std::ostream &, const std::string &)");
//TEST_ITANIUM_MANGLING("", "");
end_of_test:
;
}
#endif
|