navrutil.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. {
  2. Copyright (c) 2015 by Jeppe Johansen
  3. AVR version of some node tree helper routines
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit navrutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nbas,
  22. ngenutil,
  23. symtype,symconst,symsym,symdef;
  24. type
  25. tavrnodeutils = class(tnodeutils)
  26. class procedure InsertInitFinalTable; override;
  27. end;
  28. implementation
  29. uses
  30. verbose,cutils,globtype,globals,constexp,fmodule,
  31. cclasses,
  32. aasmdata,aasmtai,aasmcpu,aasmcnst,aasmbase,
  33. cpubase,
  34. symbase,symcpu,symtable,defutil,
  35. ncnv,ncon,ninl,ncal,nld,nmem,
  36. systems,
  37. CPUInfo,
  38. ppu,
  39. pass_1;
  40. procedure AddToStructInits(p:TObject;arg:pointer);
  41. var
  42. StructList: TFPList absolute arg;
  43. begin
  44. if (tdef(p).typ in [objectdef,recorddef]) and
  45. not (df_generic in tdef(p).defoptions) then
  46. begin
  47. { first add the class... }
  48. if ([oo_has_class_constructor,oo_has_class_destructor] * tabstractrecorddef(p).objectoptions <> []) then
  49. StructList.Add(p);
  50. { ... and then also add all subclasses }
  51. tabstractrecorddef(p).symtable.deflist.foreachcall(@AddToStructInits,arg);
  52. end;
  53. end;
  54. class procedure tavrnodeutils.InsertInitFinalTable;
  55. var
  56. hp : tused_unit;
  57. op: TAsmOp;
  58. initCount, finalCount: longint;
  59. procedure write_struct_inits(InitList, FinalizeList: TAsmList; u: tmodule);
  60. var
  61. i: integer;
  62. structlist: TFPList;
  63. pd: tprocdef;
  64. begin
  65. structlist := TFPList.Create;
  66. if assigned(u.globalsymtable) then
  67. u.globalsymtable.DefList.ForEachCall(@AddToStructInits,structlist);
  68. u.localsymtable.DefList.ForEachCall(@AddToStructInits,structlist);
  69. { write structures }
  70. for i:=0 to structlist.Count-1 do
  71. begin
  72. pd:=tabstractrecorddef(structlist[i]).find_procdef_bytype(potype_class_constructor);
  73. if assigned(pd) then
  74. begin
  75. InitList.Concat(taicpu.op_sym(op,current_asmdata.RefAsmSymbol(pd.mangledname)));
  76. inc(initCount);
  77. end;
  78. pd := tabstractrecorddef(structlist[i]).find_procdef_bytype(potype_class_destructor);
  79. if assigned(pd) then
  80. begin
  81. FinalizeList.Concat(taicpu.op_sym(op,current_asmdata.RefAsmSymbol(pd.mangledname)));
  82. inc(finalCount);
  83. end;
  84. end;
  85. structlist.free;
  86. end;
  87. var
  88. initList, finalList, header: TAsmList;
  89. begin
  90. initList:=TAsmList.create;
  91. finalList:=TAsmList.create;
  92. initCount:=0;
  93. finalCount:=0;
  94. if CPUAVR_HAS_JMP_CALL in cpu_capabilities[current_settings.cputype] then
  95. op:=A_CALL
  96. else
  97. op:=A_RCALL;
  98. hp:=tused_unit(usedunits.first);
  99. while assigned(hp) do
  100. begin
  101. if (hp.u.flags and uf_classinits) <> 0 then
  102. write_struct_inits(initList, finalList, hp.u);
  103. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  104. begin
  105. if (hp.u.flags and uf_init)<>0 then
  106. begin
  107. initList.Concat(taicpu.op_sym(op,current_asmdata.RefAsmSymbol(make_mangledname('INIT$',hp.u.globalsymtable,''))));
  108. inc(initCount);
  109. end;
  110. if (hp.u.flags and uf_finalize)<>0 then
  111. begin
  112. finalList.Concat(taicpu.op_sym(op,current_asmdata.RefAsmSymbol(make_mangledname('FINALIZE$',hp.u.globalsymtable,''))));
  113. inc(finalCount);
  114. end;
  115. end;
  116. hp:=tused_unit(hp.next);
  117. end;
  118. { insert class constructors/destructor of the program }
  119. if (current_module.flags and uf_classinits) <> 0 then
  120. write_struct_inits(initList, finalList, current_module);
  121. { Insert initialization/finalization of the program }
  122. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  123. begin
  124. if (current_module.flags and uf_init)<>0 then
  125. begin
  126. initList.Concat(taicpu.op_sym(op,current_asmdata.RefAsmSymbol(make_mangledname('INIT$',current_module.localsymtable,''))));
  127. inc(initCount);
  128. end;
  129. if (current_module.flags and uf_finalize)<>0 then
  130. begin
  131. finalList.Concat(taicpu.op_sym(op,current_asmdata.RefAsmSymbol(make_mangledname('FINALIZE$',current_module.localsymtable,''))));
  132. inc(finalCount);
  133. end;
  134. end;
  135. initList.Concat(taicpu.op_none(A_RET));
  136. finalList.Concat(taicpu.op_none(A_RET));
  137. begin
  138. header:=TAsmList.create;
  139. new_section(header, sec_code, 'FPC_INIT_FUNC_TABLE', 1);
  140. header.concat(tai_symbol.Createname_global('FPC_INIT_FUNC_TABLE',AT_FUNCTION,0));
  141. initList.insertList(header);
  142. header.free;
  143. current_asmdata.AsmLists[al_procedures].concatList(initList);
  144. end;
  145. begin
  146. header:=TAsmList.create;
  147. new_section(header, sec_code, 'FPC_FINALIZE_FUNC_TABLE', 1);
  148. header.concat(tai_symbol.Createname_global('FPC_FINALIZE_FUNC_TABLE',AT_FUNCTION,0));
  149. finalList.insertList(header);
  150. header.free;
  151. current_asmdata.AsmLists[al_procedures].concatList(finalList);
  152. end;
  153. initList.Free;
  154. finalList.Free;
  155. inherited InsertInitFinalTable;
  156. end;
  157. begin
  158. cnodeutils:=tavrnodeutils;
  159. end.