summaryrefslogtreecommitdiff
path: root/compiler/llvmdef.pas
blob: 87d4944cbdfdfeba3bd2c5b9b7a250d0b7208d34 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
{
    Copyright (c) 2008 by Peter Vreman, Florian Klaempfl and Jonas Maebe

    This units contains support for generating LLVM type info

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 ****************************************************************************
}
{
  This units contains support for LLVM type info generation.
  
  It's based on the debug info system, since it's quite similar
}
unit llvmdef;

{$i fpcdefs.inc}
{$h+}

interface

    uses
      cclasses,globtype,
      aasmbase,aasmtai,aasmdata,
      symbase,symtype,symdef,symsym,
      finput,
      dbgbase;


      { TLLVMDefInfo }
    type
      TLLVMDefInfo = class(TDebugInfo)
        function def_llvm_name(def:tdef) : tasmsymbol;
        function def_llvm_pointer_name(def: tdef): tasmsymbol;
        function def_llvm_class_struct_name(def:tobjectdef) : tasmsymbol;
        function def_llvm_vmt_name(def:tobjectdef) : tasmsymbol;
      protected
        vardatadef: trecorddef;

        procedure record_def(def:tdef);

        procedure beforeappenddef(list:TAsmList;def:tdef);override;
        procedure afterappenddef(list:TAsmList;def:tdef);override;
        procedure appenddef_ord(list:TAsmList;def:torddef);override;
        procedure appenddef_float(list:TAsmList;def:tfloatdef);override;
        procedure appenddef_enum(list:TAsmList;def:tenumdef);override;
        procedure appenddef_array(list:TAsmList;def:tarraydef);override;
        procedure appenddef_abstractrecord(list:TAsmList;def:tabstractrecorddef);
        procedure appenddef_record(list:TAsmList;def:trecorddef);override;
        procedure appenddef_pointer(list:TAsmList;def:tpointerdef);override;
        procedure appenddef_string(list:TAsmList;def:tstringdef);override;
        procedure appenddef_procvar(list:TAsmList;def:tprocvardef);override;
        procedure appendprocdef(list:TAsmList;def:tprocdef);override;
        procedure appendprocdef_implicit(list:TAsmList;def:tprocdef);override;
        procedure appenddef_formal(list:TAsmList;def: tformaldef);override;
        procedure appenddef_object(list:TAsmList;def: tobjectdef);override;
        procedure appenddef_set(list:TAsmList;def: tsetdef);override;
        procedure appenddef_undefined(list:TAsmList;def: tundefineddef);override;
        procedure appenddef_variant(list:TAsmList;def: tvariantdef);override;

        procedure appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
        procedure beforeappendsym(list:TAsmList;sym:tsym);override;
        procedure appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);override;
        procedure appendsym_paravar(list:TAsmList;sym:tparavarsym);override;
        procedure appendsym_localvar(list:TAsmList;sym:tlocalvarsym);override;
        procedure appendsym_fieldvar(list:TAsmList;sym:tfieldvarsym);override;
        procedure appendsym_const(list:TAsmList;sym:tconstsym);override;
        procedure appendsym_type(list:TAsmList;sym:ttypesym);override;
        procedure appendsym_label(list:TAsmList;sym:tlabelsym);override;
        procedure appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);override;
        procedure appendsym_property(list:TAsmList;sym:tpropertysym);override;

        function getabstractprocdefstr(def:tabstractprocdef): ansistring;
        function symname(sym:tsym): String;

        procedure enum_membersyms_callback(p:TObject;arg:pointer);

      public
        constructor Create;override;
        destructor Destroy;override;
        procedure insertmoduleinfo;override;
        procedure inserttypeinfo;override;
      end;

implementation

    uses
      sysutils,cutils,cfileutl,constexp,
      version,globals,verbose,systems,
      cpubase,cgbase,paramgr,
      fmodule,nobj,
      defutil,symconst,symtable,
      llvmbase, aasmllvm;

{****************************************************************************
                              TDebugInfoDwarf
****************************************************************************}


    procedure TLLVMDefInfo.record_def(def:tdef);
      begin
        if (def.dbg_state <> dbg_state_unused) then
          exit;
        { the name syms are set automatically when requested }
        def.dbg_state:=dbg_state_used;
        deftowritelist.Add(def);
        defnumberlist.Add(def);
      end;


    function TLLVMDefInfo.def_llvm_name(def: tdef): tasmsymbol;
      begin
        record_def(def);
        result:=def.llvm_name_sym;
      end;


    function TLLVMDefInfo.def_llvm_pointer_name(def: tdef): tasmsymbol;
      begin
        record_def(def);
        result:=def.llvm_pointername_sym;
      end;


    function TLLVMDefInfo.def_llvm_class_struct_name(def: tobjectdef): tasmsymbol;
      begin
        record_def(def);
        result:=def.llvm_class_struct_name_sym;
      end;


    function TLLVMDefInfo.def_llvm_vmt_name(def:tobjectdef) : tasmsymbol;
      begin
        record_def(def);
        result:=def.llvm_vmt_name_sym;
      end;


    constructor TLLVMDefInfo.Create;
      begin
        inherited Create;
      end;


    destructor TLLVMDefInfo.Destroy;
      begin
        inherited destroy;
      end;


    procedure TLLVMDefInfo.enum_membersyms_callback(p:TObject; arg: pointer);
      begin
        case tsym(p).typ of
          fieldvarsym:
            appendsym_fieldvar(TAsmList(arg),tfieldvarsym(p));
        end;
      end;



    procedure TLLVMDefInfo.appenddef_ord(list:TAsmList;def:torddef);
      begin
        case def.ordtype of
          s8bit,
          s16bit,
          s32bit,
          s64bit,
          u8bit,
          u16bit,
          u32bit,
          u64bit,
          uchar,
          uwidechar,
          pasbool,
          bool8bit,
          bool16bit,
          bool32bit,
          bool64bit,
          scurrency:
            begin
              list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'i'+tostr(def.size*8)));
            end;
          uvoid :
            begin
              list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'void'));
            end
          else
            internalerror(2008032901);
        end;
      end;


    procedure TLLVMDefInfo.appenddef_float(list:TAsmList;def:tfloatdef);
      begin
        case def.floattype of
          s32real:
            list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'float'));
          s64real:
            list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'double'));
          sc80real,
          s80real:
            list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'x86_fp80'));
          s64currency,
          s64comp:
            list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'i64'));
          else
            internalerror(200601289);
        end;
      end;


    procedure TLLVMDefInfo.appenddef_enum(list:TAsmList;def:tenumdef);
      begin
        list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'i'+tostr(def.size*8)));
      end;


    procedure TLLVMDefInfo.appenddef_array(list:TAsmList;def:tarraydef);
      var
        typename: ansistring;
        endstr: ansistring;
        indexrange: aint;
{$ifndef llvm_has_packed_arrays}
      begin
        if is_packed_array(def) then
          begin
            { have to use an array of byte of the appropriate size, }
            { since llvm doesn't support packed arrays yet natively }
            typename:=def_llvm_name(s8inttype).name;
            indexrange:=def.size;
          end
        else
          begin
            typename:=def_llvm_name(def.elementdef).name;
            if not is_open_array(def) and
               not is_dynamic_array(def) then
              indexrange:=def.highrange-def.lowrange+1
            else
              indexrange:=0;
          end;
        if not is_dynamic_array(def) then
          endstr:=']'
        else
          endstr:=']*';
        list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'['+tostr(indexrange)+' x '+typename+endstr))
{$else not llvm_has_packed_arrays}
      var
        arrstart, arrend, typename: ansistring;
      begin
        typename:='';
        if not is_packed_array(def) then
          begin
            { regular array: '[' nritems 'x' type ']' }
            arrstart:='[';
            arrend:=']'
          end
        else
          begin
            { packed array: '<' nritems 'x' type '>' }
            arrstart:='< [';
            arrend:='] >';
            if is_ordinal(def.elementdef) then
              typename:='i'+tostr(def.elepackedbitsize);
          end;
        if (typename='') then
          typename:=def_llvm_name(def.elementdef).name

        if is_open_array(def) then
          list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),arrstart+'0 x '+typename+arrend))
        else if is_dynamic_array(def) then
          list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'[0 x '+typename+']'+'*'))
        else
          list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),arrstart+tostr(def.highrange-def.lowrange+1)+' x '+typename+arrend))
{$endif not llvm_has_packed_arrays}
      end;


    procedure TLLVMDefInfo.appenddef_abstractrecord(list:TAsmList;def:tabstractrecorddef);
      var
        defstr, endstr: ansistring;
        symdeflist: tfpobjectlist;
        i: longint;
      begin
        if (tabstractrecordsymtable(def.symtable).usefieldalignment<>C_alignment) then
          begin
            { we handle the alignment/padding ourselves }
            defstr:='< ';
            endstr:=' >'
          end
        else
          begin
            { let llvm do everything }
            defstr:= '{ ';
            endstr:= ' }'
          end;
        if not assigned(tabstractrecordsymtable(def.symtable).llvmst) then
          tabstractrecordsymtable(def.symtable).llvmst:=tllvmshadowsymtable.create(tabstractrecordsymtable(def.symtable));
        symdeflist:=tabstractrecordsymtable(def.symtable).llvmst.symdeflist;

        if symdeflist.count>0 then
          begin
            i:=0;
            if (def.typ=objectdef) and
               assigned(tobjectdef(def).childof) and
               is_class_or_interface_or_dispinterface(tllvmshadowsymtableentry(symdeflist[0]).def) then
              begin
                writeln(def.typename,' in ',def.owner.name^);
                { insert the struct for the class rather than a pointer to the struct }
                if (tllvmshadowsymtableentry(symdeflist[0]).def.typ<>objectdef) then
                  internalerror(2008070601);
                defstr:=defstr+def_llvm_class_struct_name(tobjectdef(tllvmshadowsymtableentry(symdeflist[0]).def)).name+', ';
                inc(i);
              end;
            while i<symdeflist.count do
              begin
                defstr:=defstr+def_llvm_name(tllvmshadowsymtableentry(symdeflist[i]).def).name+', ';
                inc(i);
              end;
            { remove last ', ' }
            setlength(defstr,length(defstr)-2);
          end;
        defstr:=defstr+endstr;
        if (def.typ<>objectdef) or
           not(tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_class,odt_objcclass,odt_objccategory]) then
          list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),defstr))
        else
          list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_class_struct_name(tobjectdef(def)),defstr))
      end;


    procedure TLLVMDefInfo.appenddef_record(list:TAsmList;def:trecorddef);
      begin
        appenddef_abstractrecord(list,def);
      end;


    procedure TLLVMDefInfo.appenddef_pointer(list:TAsmList;def:tpointerdef);
      begin
        { to avoid cluttering the source with pointer types,     }
        { pointer type names directly are "pointeddef.name+'*'". }
        { So only register the pointeddef so it gets printed     }
        record_def(def.pointeddef);
      end;


    procedure TLLVMDefInfo.appenddef_string(list:TAsmList;def:tstringdef);

      procedure addnormalstringdef(lendef: tdef);
        var
          defstr: ansistring;
        begin
          { record with length and array [maxlen x i8 ] }
          { (also ok for openstrings, as [0 x i8] means }
          {  "array of unspecified size" in llvm)       }
          defstr:='< '+def_llvm_name(lendef).name+', ['+tostr(def.len)+' x i8] >';
          list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),defstr));
        end;

      begin
        case def.stringtype of
          st_shortstring:
            begin
              addnormalstringdef(u8inttype);
            end;
          st_longstring:
            begin
{$ifdef cpu64bitaddr}
              addnormalstringdef(u64inttype);
{$else cpu64bitaddr}
              addnormalstringdef(u32inttype);
{$endif cpu64bitaddr}
           end;
         st_ansistring:
           begin
             { looks like a pchar }
             list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'i8*'));
           end;
         st_unicodestring,
         st_widestring:
           begin
             { looks like a pwidechar }
             list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'i16*'));
           end;
        end;
      end;

    procedure TLLVMDefInfo.appenddef_procvar(list:TAsmList;def:tprocvardef);
      begin
        if def.is_addressonly then
          list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),getabstractprocdefstr(def)+'*'))
        else
          list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'{ '+getabstractprocdefstr(def)+'*, i8* }'))
      end;


    procedure TLLVMDefInfo.beforeappenddef(list:TAsmList;def:tdef);
      var
        labsym : tasmsymbol;
      begin
        list.concat(tai_comment.Create(strpnew('LLVM definition '+def.typename)));
      end;


    procedure TLLVMDefInfo.afterappenddef(list:TAsmList;def:tdef);
      begin
      end;


    function TLLVMDefInfo.getabstractprocdefstr(def:tabstractprocdef): ansistring;
      var
        i      : longint;
      begin
        { function result-by-reference is handled as a parameter }
        if (def.proctypeoption in [potype_constructor,potype_destructor]) or
           not paramanager.ret_in_param(def.returndef,def.proccalloption) then
          result:=def_llvm_name(def.returndef).name
        else
          result:='void';
        result:=result+' (  ';

        for i:=0 to def.paras.count-1 do
          with tparavarsym(def.paras[i]) do
            begin
              result:=result+def_llvm_name(vardef).name;
              if paramanager.push_addr_param(varspez,vardef,def.proccalloption) then
                begin
                  result:=result+'*';
                  if (vo_is_funcret in varoptions) then
                    result:=result+' sret';
                end
              else
                begin
                  { 'byval' means that the parameter is copied onto the stack at the }
                  { right location at the caller side rather than that the calling   }
                  { conventions are used to determine whether the address or value   }
                  { of the parameter is passed                                       }
                  { I don't think we need this for something right now               }
                  // result:=result+' byval'
                end;
              result:=result+', '
            end;
        result[length(result)-1]:=')';
      end;


    procedure TLLVMDefInfo.appendprocdef_implicit(list:TAsmList;def:tprocdef);
      begin
        list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),getabstractprocdefstr(def)));
      end;
      

    procedure TLLVMDefInfo.appendprocdef(list:TAsmList;def:tprocdef);
      var
        defstr : ansistring;
        i      : longint;
      begin
        { the procdef itself is already written by appendprocdef_implicit }
      
        { last write the types from this procdef }
        if assigned(def.parast) then
          write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],def.parast);
        if assigned(def.localst) and
           (def.localst.symtabletype=localsymtable) then
          write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],def.localst);
      end;


    procedure TLLVMDefInfo.appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
      var
        templist : TAsmList;
        blocksize : longint;
        dreg : byte;
      begin      
        { external symbols can't be resolved at link time, so we
          can't generate stabs for them

          not sure if this applies to dwarf as well (FK)
        }
        if vo_is_external in sym.varoptions then
          exit;

        def_llvm_name(sym.vardef);

        { There is no space allocated for not referenced locals }
        if (sym.owner.symtabletype=localsymtable) and (sym.refs=0) then
          exit;
(*
        templist:=TAsmList.create;

        case sym.localloc.loc of
          LOC_REGISTER,
          LOC_CREGISTER,
          LOC_MMREGISTER,
          LOC_CMMREGISTER,
          LOC_FPUREGISTER,
          LOC_CFPUREGISTER :
            begin
              templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
              dreg:=dwarf_reg(sym.localloc.register);
              templist.concat(tai_const.create_uleb128bit(dreg));
              blocksize:=1+Lengthuleb128(dreg);
            end;
          else
            begin
              case sym.typ of
                staticvarsym:
                  begin
                    if (vo_is_thread_var in sym.varoptions) then
                      begin
{$warning !!! FIXME: dwarf for thread vars !!!
}
                        blocksize:=0;
                      end
                    else
                      begin
                        templist.concat(tai_const.create_8bit(3));
                        templist.concat(tai_const.createname(sym.mangledname,0));
                        blocksize:=1+sizeof(puint);
                      end;
                  end;
                paravarsym,
                localvarsym:
                  begin
                    dreg:=dwarf_reg(sym.localloc.reference.base);
                    templist.concat(tai_const.create_8bit(ord(DW_OP_breg0)+dreg));
                    templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset));
                    blocksize:=1+Lengthsleb128(sym.localloc.reference.offset);
                  end
                else
                  internalerror(200601288);
              end;
            end;
        end;

        if sym.typ=paravarsym then
          tag:=DW_TAG_formal_parameter
        else
          tag:=DW_TAG_variable;

        if not(sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
                                 LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
           ((sym.owner.symtabletype = globalsymtable) or
            (sp_static in sym.symoptions) or
            (vo_is_public in sym.varoptions)) then
          append_entry(tag,false,[
            DW_AT_name,DW_FORM_string,symname(sym)+#0,
            {
            DW_AT_decl_file,DW_FORM_data1,0,
            DW_AT_decl_line,DW_FORM_data1,
            }
            DW_AT_external,DW_FORM_flag,true,
            { data continues below }
            DW_AT_location,DW_FORM_block1,blocksize
            ])
{$ifdef gdb_supports_DW_AT_variable_parameter}
        else if (sym.typ=paravarsym) and
            paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
            not(vo_has_local_copy in sym.varoptions) and
            not is_open_string(sym.vardef) then
          append_entry(tag,false,[
            DW_AT_name,DW_FORM_string,symname(sym)+#0,
            DW_AT_variable_parameter,DW_FORM_flag,true,
            {
            DW_AT_decl_file,DW_FORM_data1,0,
            DW_AT_decl_line,DW_FORM_data1,
            }
            { data continues below }
            DW_AT_location,DW_FORM_block1,blocksize
            ])
{$endif gdb_supports_DW_AT_variable_parameter}
        else
          append_entry(tag,false,[
            DW_AT_name,DW_FORM_string,symname(sym)+#0,
            {
            DW_AT_decl_file,DW_FORM_data1,0,
            DW_AT_decl_line,DW_FORM_data1,
            }
            { data continues below }
            DW_AT_location,DW_FORM_block1,blocksize
            ]);
        { append block data }
        current_asmdata.asmlists[al_dwarf_info].concatlist(templist);
{$ifndef gdb_supports_DW_AT_variable_parameter}
        if (sym.typ=paravarsym) and
            paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
            not(vo_has_local_copy in sym.varoptions) and
            not is_open_string(sym.vardef) then
          append_labelentry_ref(DW_AT_type,def_dwarf_ref_lab(sym.vardef))
        else
{$endif not gdb_supports_DW_AT_variable_parameter}
          append_labelentry_ref(DW_AT_type,def_dwarf_lab(sym.vardef));

        templist.free;

        finish_entry;
*)
      end;


    procedure TLLVMDefInfo.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
      begin
        appendsym_var(list,sym);
      end;


    procedure TLLVMDefInfo.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
      begin
        appendsym_var(list,sym);
      end;


    procedure TLLVMDefInfo.appendsym_paravar(list:TAsmList;sym:tparavarsym);
      begin
        appendsym_var(list,sym);
      end;


    procedure TLLVMDefInfo.appendsym_fieldvar(list:TAsmList;sym: tfieldvarsym);
      var
        bitoffset,
        fieldoffset,
        fieldnatsize: aint;
      begin
//        list.concat(taillvm.op_ressym_string(LA_TYPE),'fieldvasym');
(*
        if sp_static in sym.symoptions then
          exit;

        if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) or
           { only ordinals are bitpacked }
           not is_ordinal(sym.vardef) then
          begin
            { other kinds of fields can however also appear in a bitpacked   }
            { record, and then their offset is also specified in bits rather }
            { than in bytes                                                  }
            if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) then
              fieldoffset:=sym.fieldoffset
            else
              fieldoffset:=sym.fieldoffset div 8;
            append_entry(DW_TAG_member,false,[
              DW_AT_name,DW_FORM_string,symname(sym)+#0,
              DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
              ]);
          end
        else
          begin
            if (sym.vardef.packedbitsize > 255) then
              internalerror(2007061201);

            { we don't bitpack according to the ABI, but as close as }
            { possible, i.e., equivalent to gcc's                    }
            { __attribute__((__packed__)), which is also what gpc    }
            { does.                                                  }
            fieldnatsize:=max(sizeof(pint),sym.vardef.size);
            fieldoffset:=(sym.fieldoffset div (fieldnatsize*8)) * fieldnatsize;
            bitoffset:=sym.fieldoffset mod (fieldnatsize*8);
            if (target_info.endian=endian_little) then
              bitoffset:=(fieldnatsize*8)-bitoffset-sym.vardef.packedbitsize;
            append_entry(DW_TAG_member,false,[
              DW_AT_name,DW_FORM_string,symname(sym)+#0,
              { gcc also generates both a bit and byte size attribute }
              { we don't support ordinals >= 256 bits }
              DW_AT_byte_size,DW_FORM_data1,fieldnatsize,
              { nor >= 256 bits (not yet, anyway, see IE above) }
              DW_AT_bit_size,DW_FORM_data1,sym.vardef.packedbitsize,
              { data1 and data2 are unsigned, bitoffset can also be negative }
              DW_AT_bit_offset,DW_FORM_data4,bitoffset,
              DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
              ]);
          end;
        current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
        current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(fieldoffset));

        append_labelentry_ref(DW_AT_type,def_dwarf_lab(sym.vardef));
        finish_entry;
*)
      end;


    procedure TLLVMDefInfo.appendsym_const(list:TAsmList;sym:tconstsym);
      begin
//        list.concat(taillvm.op_ressym_string(LA_TYPE),'constsym');
(*
        append_entry(DW_TAG_constant,false,[
          DW_AT_name,DW_FORM_string,symname(sym)+#0
          ]);
        { for string constants, constdef isn't set because they have no real type }
        if not(sym.consttyp in [conststring,constresourcestring,constwstring]) then
          append_labelentry_ref(DW_AT_type,def_dwarf_lab(sym.constdef));
        current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_AT_const_value)));
        case sym.consttyp of
          conststring:
            begin
              current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_string)));
              current_asmdata.asmlists[al_dwarf_info].concat(tai_string.create(strpas(pchar(sym.value.valueptr))));
              current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(0));
            end;
          constset,
          constwstring,
          constguid,
          constresourcestring:
            begin
              { write dummy for now }
              current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_string)));
              current_asmdata.asmlists[al_dwarf_info].concat(tai_string.create(''));
              current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(0));
            end;
          constord:
            begin
              current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_sdata)));
              current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_sleb128bit(sym.value.valueord.svalue));
            end;
          constnil:
            begin
{$ifdef cpu64bitaddr}
              current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data8)));
              current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit(0));
{$else cpu64bitaddr}
              current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data4)));
              current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit(0));
{$endif cpu64bitaddr}
            end;
          constpointer:
            begin
{$ifdef cpu64bitaddr}
              current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data8)));
              current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit(int64(sym.value.valueordptr)));
{$else cpu64bitaddr}
              current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_data4)));
              current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit(sym.value.valueordptr));
{$endif cpu64bitaddr}
            end;
          constreal:
            begin
              current_asmdata.asmlists[al_dwarf_abbrev].concat(tai_const.create_uleb128bit(ord(DW_FORM_block1)));
              case tfloatdef(sym.constdef).floattype of
                s32real:
                  begin
                    current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(4));
                    current_asmdata.asmlists[al_dwarf_info].concat(tai_real_32bit.create(psingle(sym.value.valueptr)^));
                  end;
                s64comp,
                s64currency,
                s64real:
                  begin
                    current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(8));
                    current_asmdata.asmlists[al_dwarf_info].concat(tai_real_64bit.create(pdouble(sym.value.valueptr)^));
                  end;
                s80real:
                  begin
                    current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(10));
                    current_asmdata.asmlists[al_dwarf_info].concat(tai_real_80bit.create(pextended(sym.value.valueptr)^));
                  end;
                else
                  internalerror(200601291);
              end;
            end;
          else
            internalerror(200601292);
        end;
        finish_entry;
*)
      end;


    procedure TLLVMDefInfo.appendsym_label(list:TAsmList;sym: tlabelsym);
      begin
        { ignore label syms for now, the problem is that a label sym
          can have more than one label associated e.g. in case of
          an inline procedure expansion }
      end;


    procedure TLLVMDefInfo.appendsym_property(list:TAsmList;sym: tpropertysym);
      begin
        { ignored for now }
      end;


    procedure TLLVMDefInfo.appendsym_type(list:TAsmList;sym: ttypesym);
      begin
//        list.concat(taillvm.op_ressym_string(LA_TYPE,'typesym');
        record_def(sym.typedef);
      end;


    procedure TLLVMDefInfo.appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);
      var
        templist : TAsmList;
        blocksize : longint;
        symlist : ppropaccesslistitem;
      begin
//        list.concat(taillvm.op_ressym_string(LA_TYPE),'absolutesym'));
      end;


    procedure TLLVMDefInfo.beforeappendsym(list:TAsmList;sym:tsym);
      begin
      end;


    procedure TLLVMDefInfo.insertmoduleinfo;
      begin
      end;


    procedure TLLVMDefInfo.inserttypeinfo;

      procedure write_defs_to_write;
        var
          n       : integer;
          looplist,
          templist: TFPObjectList;
          def     : tdef;
        begin
          templist := TFPObjectList.Create(False);
          looplist := deftowritelist;
          while looplist.count > 0 do
            begin
              deftowritelist := templist;
              for n := 0 to looplist.count - 1 do
                begin
                  def := tdef(looplist[n]);
                  case def.dbg_state of
                    dbg_state_written:
                      continue;
                    dbg_state_writing:
                      internalerror(200610052);
                    dbg_state_unused:
                      internalerror(200610053);
                    dbg_state_used:
                      appenddef(current_asmdata.asmlists[al_dwarf_info],def)
                  else
                    internalerror(200610054);
                  end;
                end;
              looplist.clear;
              templist := looplist;
              looplist := deftowritelist;
            end;
          templist.free;
        end;


      var
        storefilepos  : tfileposinfo;
        lenstartlabel : tasmlabel;
        i : longint;
        def: tdef;
      begin
        storefilepos:=current_filepos;
        current_filepos:=current_module.mainfilepos;

        defnumberlist:=TFPObjectList.create(false);
        deftowritelist:=TFPObjectList.create(false);

        { not exported (FK)
            FILEREC
            TEXTREC
        }
        vardatadef:=trecorddef(search_system_type('TVARDATA').typedef);

        { write all global/local variables. This will flag all required tdefs  }
        if assigned(current_module.globalsymtable) then
          write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
        if assigned(current_module.localsymtable) then
          write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);

        { write all procedures and methods. This will flag all required tdefs }
        if assigned(current_module.globalsymtable) then
          write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
        if assigned(current_module.localsymtable) then
          write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);

        { reset unit type info flag }
        reset_unit_type_info;

        { write used types from the used units }
        write_used_unit_type_info(current_asmdata.asmlists[al_dwarf_info],current_module);

        { last write the types from this unit }
        if assigned(current_module.globalsymtable) then
          write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
        if assigned(current_module.localsymtable) then
          write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);

        { write defs not written yet }
        write_defs_to_write;

        { reset all def labels }
        for i:=0 to defnumberlist.count-1 do
          begin
            def := tdef(defnumberlist[i]);
            if assigned(def) then
              begin
                def.dwarf_lab:=nil;
                def.dbg_state:=dbg_state_unused;
{$ifdef support_llvm}
                def.fllvm_name_sym:=nil;
                def.fllvm_pointer_name_sym:=nil;
                if def.typ=objectdef then
                  begin
                    tobjectdef(def).fllvm_class_struct_name_sym:=nil;
                    tobjectdef(def).fllvm_vmt_name_sym:=nil;
                  end;
{$endif support_llvm}
              end;
          end;

        defnumberlist.free;
        defnumberlist:=nil;
        deftowritelist.free;
        deftowritelist:=nil;

        current_filepos:=storefilepos;
      end;


    function TLLVMDefInfo.symname(sym: tsym): String;
      begin
        if (sym.typ=paravarsym) and
           (vo_is_self in tparavarsym(sym).varoptions) then
          result:='this'
        else
          result := sym.Name;
      end;



    procedure TLLVMDefInfo.appenddef_formal(list:TAsmList;def: tformaldef);
      begin
        list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'undef*'));
      end;


    procedure TLLVMDefInfo.appenddef_object(list:TAsmList;def: tobjectdef);
      procedure doappend;
        begin
          appenddef_abstractrecord(list,def);
        end;

      procedure doappend_classvmt;
        var
          defstr: ansistring;
          i: longint;
        begin
          { a pointer to the VMT. Structure of the VMT: }
          {   InstanceSize  : ptrint  }
          {   -InstanceSize : ptrint  }
          {   Parent        : ^parent }
          {   ClassName     : pointer }
          {   DynamicTable  : pointer }
          {   MethodTable   : pointer }
          {   FieldTable    : pointer }
          {   TypeInfo      : pointer }
          {   InitTable     : pointer }
          {   AutoTable     : pointer }
          {   IntfTable     : pointer }
          {   MsgStrTable   : pointer }
          {   Methods       : X times procvar }
          defstr:=def_llvm_name(ptrsinttype).name+', ';
          defstr:='< '+defstr+defstr;
          if assigned(def.childof) then
            defstr:=defstr+def_llvm_vmt_name(def.childof).name+'*, '
          else
            defstr:=defstr+'i8*, ';
          { class name (length+string) }
          defstr:=defstr+'['+tostr(length(def.objrealname^)+1)+' x i8]*, ';
          { the other fields }
          for i:=1 to 8 do
            defstr:=defstr+'i8*, ';
          for i:= 0 to def.VMTEntries.Count-1 do
            defstr:=defstr+def_llvm_pointer_name(pvmtentry(def.VMTEntries[i])^.procdef).name+', ';
          setlength(defstr,length(defstr)-2);
          defstr:=defstr+' >*';
          list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_vmt_name(def),defstr));
        end;

      begin
        case def.objecttype of
          odt_cppclass,
          odt_object:
            doappend;
          odt_interfacecom,
          odt_interfacecorba,
          odt_dispinterface,
          odt_class,
          odt_objcclass,
          odt_objcprotocol:
            begin
              { implicit pointer }
              list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),def_llvm_class_struct_name(def).name+'*'));
              doappend;
              if not (def.objecttype in [odt_objcclass,odt_objcprotocol]) then
                doappend_classvmt;
            end;
          else
            internalerror(200602041);
        end;
      end;

    procedure TLLVMDefInfo.appenddef_set(list:TAsmList;def: tsetdef);
      begin
        list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'[ '+tostr(def.size)+ 'x i8 ]'));
      end;

    procedure TLLVMDefInfo.appenddef_undefined(list:TAsmList;def: tundefineddef);
      begin
        list.concat(taillvm.op_ressym_string(LA_TYPE,def_llvm_name(def),'undef'));
      end;

    procedure TLLVMDefInfo.appenddef_variant(list:TAsmList;def: tvariantdef);
      begin
        appenddef_record(list,trecorddef(vardatadef));
      end;

end.