cgobj.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. unit cgobj;
  2. interface
  3. pcg = ^tcg;
  4. tcg = object
  5. procedure a_call_name_ext(list : paasmoutput;const s : string;
  6. offset : longint;m : texternaltyp);
  7. procedure g_entrycode(list : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
  8. stackframe:longint;
  9. var parasize:longint;var nostackframe:boolean;
  10. inlined : boolean);
  11. { these methods must be overriden: }
  12. procedure a_push_reg(list : paasmoutput;r : tregister);virtual;
  13. procedure a_call_name(list : paasmoutput;const s : string;
  14. offset : longint);virtual;
  15. procedure a_load_const8_ref(list : paasmoutput;b : byte;ref : treference);virtual;
  16. procedure a_load_const16_ref(list : paasmoutput;w : word;ref : treference);virtual;
  17. procedure a_load_const32_ref(list : paasmoutput;l : longint;ref : treference);virtual;
  18. procedure a_load_const64_ref(list : paasmoutput;{ q : qword; }ref : treference);virtual;
  19. procedure g_stackframe_entry(list : paasmoutput;localsize : longint);
  20. { these methods can be overriden for extra functionality }
  21. { the following do nothing: }
  22. procedure g_interrupt_stackframe_entry(list : paasmoutput);virtual;
  23. procedure g_interrupt_stackframe_exit(list : paasmoutput);virtual;
  24. procedure g_profilecode(list : paasmoutput);virtual;
  25. procedure g_stackcheck(list : paasmoutput);virtual;
  26. { passing parameters, per default the parameter is pushed }
  27. { nr gives the number of the parameter (enumerated from }
  28. { left to right), this allows to move the parameter to }
  29. { register, if the cpu supports register calling }
  30. { conventions }
  31. procedure a_param_reg(list : paasmoutput;r : tregister;nr : longint);virtual;
  32. end;
  33. implementation
  34. procedure tcg.g_interrupt_stackframe_entry(list : paasmoutput);
  35. begin
  36. end;
  37. procedure tcg.g_interrupt_stackframe_exit(list : paasmoutput);
  38. begin
  39. end;
  40. procedure tcg.a_param_reg(list : paasmoutput;r : tregister;nr : longint);
  41. begin
  42. a_push_reg(list,r);
  43. end;
  44. procedure tcg.g_stackcheck(list : paasmoutput);
  45. begin
  46. a_param_reg(list,stackframe,1);
  47. a_call_name(list,'FPC_STACKCHECK',0,EXT_NEAR);
  48. end;
  49. procedure tcg.a_call_name_ext(list : paasmoutput;const s : string;
  50. offset : longint;m : texternaltyp);
  51. begin
  52. a_call_name(list,s,offset);
  53. concat_external(s,m);
  54. end;
  55. { generates the entry code for a procedure }
  56. procedure tcg.g_entrycode(list : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
  57. stackframe:longint;var parasize:longint;var nostackframe:boolean;
  58. inlined : boolean);
  59. var
  60. hs : string;
  61. hp : Pused_unit;
  62. unitinits,initcode : taasmoutput;
  63. {$ifdef GDB}
  64. stab_function_name : Pai_stab_function_name;
  65. {$endif GDB}
  66. hr : preference;
  67. begin
  68. { Align }
  69. if (not inlined) then
  70. begin
  71. { gprof uses 16 byte granularity !! }
  72. if (cs_profile in aktmoduleswitches) then
  73. list^.insert(new(pai_align,init_op(16,$90)))
  74. else
  75. if not(cs_littlesize in aktglobalswitches) then
  76. list^.insert(new(pai_align,init(4)));
  77. end;
  78. curlist:=list;
  79. if (not inlined) and ((aktprocsym^.definition^.options and poproginit)<>0) then
  80. begin
  81. { needs the target a console flags ? }
  82. if tf_needs_isconsole in target_info.flags then
  83. begin
  84. new(hr);
  85. reset_reference(hr^);
  86. hr^.symbol:=stringdup('U_'+target_info.system_unit+'_ISCONSOLE');
  87. if apptype=at_cui then
  88. a_load_const8_ref(list,1,hr);
  89. else
  90. a_load_const8_ref(list,0,hr);
  91. end;
  92. { Call the unit init procedures }
  93. unitinits.init;
  94. hp:=pused_unit(usedunits.first);
  95. while assigned(hp) do
  96. begin
  97. { call the unit init code and make it external }
  98. if (hp^.u^.flags and uf_init)<>0 then
  99. a_call_name_ext(@unitinits,
  100. 'INIT$$'+hp^.u^.modulename^,0,EXT_NEAR);
  101. hp:=Pused_unit(hp^.next);
  102. end;
  103. list^.insertlist(@unitinits);
  104. unitinits.done;
  105. end;
  106. { a constructor needs a help procedure }
  107. if (aktprocsym^.definition^.options and poconstructor)<>0 then
  108. begin
  109. if procinfo._class^.isclass then
  110. begin
  111. list^.insert(new(pai_labeled,init(A_JZ,quickexitlabel)));
  112. list^.insert(new(pai386,op_csymbol(A_CALL,S_NO,
  113. newcsymbol('FPC_NEW_CLASS',0))));
  114. concat_external('FPC_NEW_CLASS',EXT_NEAR);
  115. end
  116. else
  117. begin
  118. list^.insert(new(pai_labeled,init(A_JZ,quickexitlabel)));
  119. list^.insert(new(pai386,op_csymbol(A_CALL,S_NO,
  120. newcsymbol('FPC_HELP_CONSTRUCTOR',0))));
  121. list^.insert(new(pai386,op_const_reg(A_MOV,S_L,procinfo._class^.vmt_offset,R_EDI)));
  122. concat_external('FPC_HELP_CONSTRUCTOR',EXT_NEAR);
  123. end;
  124. end;
  125. {$ifdef GDB}
  126. if (cs_debuginfo in aktmoduleswitches) then
  127. list^.insert(new(pai_force_line,init));
  128. {$endif GDB}
  129. { save registers on cdecl }
  130. if ((aktprocsym^.definition^.options and pocdecl)<>0) then
  131. begin
  132. for r:=firstregister to lastregister do
  133. begin
  134. if (r in registers_saved_on_cdecl) then
  135. if (r in general_registers) then
  136. begin
  137. if (r in usedregisters) then
  138. a_push_reg(list,r)
  139. end
  140. else
  141. a_push_reg(list,r);
  142. end;
  143. end;
  144. { omit stack frame ? }
  145. if not inlined then
  146. if procinfo.framepointer=stack_pointer then
  147. begin
  148. CGMessage(cg_d_stackframe_omited);
  149. nostackframe:=true;
  150. if (aktprocsym^.definition^.options and (pounitinit or poproginit or pounitfinalize)<>0) then
  151. parasize:=0
  152. else
  153. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo.call_offset-4;
  154. end
  155. else
  156. begin
  157. if (aktprocsym^.definition^.options and (pounitinit or poproginit or pounitfinalize)<>0) then
  158. parasize:=0
  159. else
  160. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo.call_offset-8;
  161. nostackframe:=false;
  162. if (aktprocsym^.definition^.options and pointerrupt)<>0 then
  163. generate_interrupt_stackframe_entry;
  164. g_stackframe_entry(list,stackframe);
  165. if (cs_check_stack in aktlocalswitches) and
  166. (target_info.flags in tf_supports_stack_check) then
  167. g_stackcheck(@initcode);
  168. end;
  169. if cs_profile in aktmoduleswitches then
  170. g_profilecode(@initcode);
  171. { initialize return value }
  172. if is_ansistring(procinfo.retdef) or
  173. is_widestring(procinfo.retdef) then
  174. begin
  175. new(hr);
  176. reset_reference(hr^);
  177. hr^.offset:=procinfo.retoffset;
  178. hr^.base:=procinfo.framepointer;
  179. curlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,0,hr)));
  180. end;
  181. { generate copies of call by value parameters }
  182. if (aktprocsym^.definition^.options and poassembler=0) then
  183. begin
  184. {$ifndef VALUEPARA}
  185. aktprocsym^.definition^.parast^.foreach(copyopenarrays);
  186. {$else}
  187. aktprocsym^.definition^.parast^.foreach(copyvalueparas);
  188. {$endif}
  189. end;
  190. { initialisizes local data }
  191. aktprocsym^.definition^.localst^.foreach(initialize_data);
  192. { add a reference to all call by value/const parameters }
  193. aktprocsym^.definition^.parast^.foreach(incr_data);
  194. if (cs_profile in aktmoduleswitches) or
  195. (aktprocsym^.definition^.owner^.symtabletype=globalsymtable) or
  196. (assigned(procinfo._class) and (procinfo._class^.owner^.symtabletype=globalsymtable)) then
  197. make_global:=true;
  198. if not inlined then
  199. begin
  200. hs:=proc_names.get;
  201. {$ifdef GDB}
  202. if (cs_debuginfo in aktmoduleswitches) and target_os.use_function_relative_addresses then
  203. stab_function_name := new(pai_stab_function_name,init(strpnew(hs)));
  204. {$endif GDB}
  205. { insert the names for the procedure }
  206. while hs<>'' do
  207. begin
  208. if make_global then
  209. list^.insert(new(pai_symbol,init_global(hs)))
  210. else
  211. list^.insert(new(pai_symbol,init(hs)));
  212. {$ifdef GDB}
  213. if (cs_debuginfo in aktmoduleswitches) then
  214. begin
  215. if target_os.use_function_relative_addresses then
  216. list^.insert(new(pai_stab_function_name,init(strpnew(hs))));
  217. end;
  218. {$endif GDB}
  219. hs:=proc_names.get;
  220. end;
  221. end;
  222. {$ifdef GDB}
  223. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  224. begin
  225. if target_os.use_function_relative_addresses then
  226. list^.insert(stab_function_name);
  227. if make_global or ((procinfo.flags and pi_is_global) <> 0) then
  228. aktprocsym^.is_global := True;
  229. list^.insert(new(pai_stabs,init(aktprocsym^.stabstring)));
  230. aktprocsym^.isstabwritten:=true;
  231. end;
  232. {$endif GDB}
  233. curlist:=nil;
  234. end;
  235. if stackframe<>0 then
  236. begin
  237. if (cs_littlesize in aktglobalswitches) and (stackframe<=65535) then
  238. begin
  239. list^.insert(new(pai386,op_const_const(A_ENTER,S_NO,stackframe,0)))
  240. end
  241. else
  242. begin
  243. g_stackframe_entry(list,stackframe);
  244. list^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EBP)));
  245. list^.insert(new(pai386,op_reg_reg(A_MOV,S_L,R_ESP,R_EBP)));
  246. list^.insert(new(pai386,op_const_reg(A_SUB,S_L,stackframe,R_ESP)));
  247. if (cs_check_stack in aktlocalswitches) and
  248. (target_info.flags in tf_supports_stack_check) then
  249. emit_stackcheck(@initcode);
  250. end;
  251. if (cs_check_stack in aktlocalswitches) and
  252. (target_info.flags in tf_supports_stack_check) then
  253. g_stackcheck(@initcode);
  254. end { endif stackframe <> 0 }
  255. else
  256. begin
  257. list^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EBP)));
  258. list^.insert(new(pai386,op_reg_reg(A_MOV,S_L,R_ESP,R_EBP)));
  259. end;
  260. end;
  261. begin
  262. unitinits.concat(new(pai386,op_csymbol(A_CALL,S_NO,newcsymbol())));
  263. concat_external('INIT$$'+hp^.u^.modulename^,EXT_NEAR);
  264. end;
  265. end.
  266. {
  267. $Log$
  268. Revision 1.1 1998-12-15 16:32:58 florian
  269. + first version, derived from old routines
  270. }