hlcgcpu.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. {
  2. Copyright (c) 1998-2010 by Florian Klaempfl and Jonas Maebe
  3. Member of the Free Pascal development team
  4. This unit contains routines to create a pass-through high-level code
  5. generator. This is used by most regular code generators.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit hlcgcpu;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. aasmdata,
  24. symtype,symdef,parabase,
  25. cgbase,cgutils,
  26. hlcgobj, hlcgx86;
  27. type
  28. thlcgcpu = class(thlcgx86)
  29. protected
  30. procedure gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint); override;
  31. public
  32. function a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara; override;
  33. procedure g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister); override;
  34. procedure g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation); override;
  35. end;
  36. procedure create_hlcodegen;
  37. implementation
  38. uses
  39. globals, procinfo,
  40. globtype,verbose,
  41. fmodule,systems,
  42. aasmbase,aasmtai,
  43. paramgr,
  44. cpubase,tgobj,cgobj,cgcpu;
  45. { thlcgcpu }
  46. procedure thlcgcpu.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  47. var
  48. locsize : tcgsize;
  49. tmploc : tlocation;
  50. href : treference;
  51. stacksize : longint;
  52. begin
  53. if not(l.size in [OS_32,OS_S32,OS_64,OS_S64,OS_128,OS_S128]) then
  54. locsize:=l.size
  55. else
  56. locsize:=int_float_cgsize(tcgsize2size[l.size]);
  57. case l.loc of
  58. LOC_FPUREGISTER,
  59. LOC_CFPUREGISTER:
  60. begin
  61. case cgpara.location^.loc of
  62. LOC_REFERENCE:
  63. begin
  64. stacksize:=align(locintsize,cgpara.alignment);
  65. if (not paramanager.use_fixed_stack) and
  66. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  67. begin
  68. cg.g_stackpointer_alloc(list,stacksize);
  69. reference_reset_base(href,voidstackpointertype,NR_STACK_POINTER_REG,0,voidstackpointertype.size);
  70. end
  71. else
  72. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  73. cg.a_loadfpu_reg_ref(list,locsize,locsize,l.register,href);
  74. end;
  75. LOC_FPUREGISTER:
  76. begin
  77. cg.a_loadfpu_reg_reg(list,locsize,cgpara.location^.size,l.register,cgpara.location^.register);
  78. end;
  79. { can happen if a record with only 1 "single field" is
  80. returned in a floating point register and then is directly
  81. passed to a regcall parameter }
  82. LOC_REGISTER:
  83. begin
  84. tmploc:=l;
  85. location_force_mem(list,tmploc,size);
  86. case locsize of
  87. OS_F32:
  88. tmploc.size:=OS_32;
  89. OS_F64:
  90. tmploc.size:=OS_64;
  91. else
  92. internalerror(2010053116);
  93. end;
  94. cg.a_load_loc_cgpara(list,tmploc,cgpara);
  95. location_freetemp(list,tmploc);
  96. end
  97. else
  98. internalerror(2010053003);
  99. end;
  100. end;
  101. LOC_MMREGISTER,
  102. LOC_CMMREGISTER:
  103. begin
  104. case cgpara.location^.loc of
  105. LOC_REFERENCE:
  106. begin
  107. { can't use TCGSize2Size[l.size], because the size of an
  108. 80 bit extended parameter can be either 10 or 12 bytes }
  109. stacksize:=align(locintsize,cgpara.alignment);
  110. if (not paramanager.use_fixed_stack) and
  111. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  112. begin
  113. cg.g_stackpointer_alloc(list,stacksize);
  114. reference_reset_base(href,voidstackpointertype,NR_STACK_POINTER_REG,0,voidstackpointertype.size);
  115. end
  116. else
  117. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  118. cg.a_loadmm_reg_ref(list,locsize,locsize,l.register,href,mms_movescalar);
  119. end;
  120. LOC_FPUREGISTER:
  121. begin
  122. tmploc:=l;
  123. location_force_mem(list,tmploc,size);
  124. cg.a_loadfpu_ref_cgpara(list,tmploc.size,tmploc.reference,cgpara);
  125. location_freetemp(list,tmploc);
  126. end;
  127. else
  128. internalerror(2010053004);
  129. end;
  130. end;
  131. LOC_REFERENCE,
  132. LOC_CREFERENCE :
  133. begin
  134. case cgpara.location^.loc of
  135. LOC_REFERENCE:
  136. begin
  137. stacksize:=align(locintsize,cgpara.alignment);
  138. if (not paramanager.use_fixed_stack) and
  139. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  140. cg.a_load_ref_cgpara(list,locsize,l.reference,cgpara)
  141. else
  142. begin
  143. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  144. cg.g_concatcopy(list,l.reference,href,stacksize);
  145. end;
  146. end;
  147. LOC_FPUREGISTER:
  148. begin
  149. cg.a_loadfpu_ref_cgpara(list,locsize,l.reference,cgpara);
  150. end;
  151. else
  152. internalerror(2010053005);
  153. end;
  154. end;
  155. else
  156. internalerror(2002042430);
  157. end;
  158. end;
  159. function thlcgcpu.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;
  160. var
  161. need_got_load: boolean;
  162. begin
  163. { Load GOT address to EBX before calling an external function.
  164. It is needed because GOT stubs for external function calls
  165. generated by a linker expect EBX as a GOT register. }
  166. need_got_load:=not (target_info.system in systems_darwin) and
  167. (cs_create_pic in current_settings.moduleswitches) and
  168. (tf_pic_uses_got in target_info.flags) and
  169. (po_external in pd.procoptions);
  170. if need_got_load then
  171. begin
  172. { Alloc EBX }
  173. getcpuregister(list, NR_PIC_OFFSET_REG);
  174. list.concat(taicpu.op_reg_reg(A_MOV,S_L,current_procinfo.got,NR_PIC_OFFSET_REG));
  175. end;
  176. Result:=inherited a_call_name(list, pd, s, paras, forceresdef, weak);
  177. { Free EBX }
  178. if need_got_load then
  179. ungetcpuregister(list, NR_PIC_OFFSET_REG);
  180. end;
  181. procedure thlcgcpu.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  182. begin
  183. if paramanager.use_fixed_stack then
  184. begin
  185. inherited;
  186. exit;
  187. end;
  188. tcg386(cg).g_copyvaluepara_openarray(list,ref,lenloc,arrdef.elesize,destreg);
  189. end;
  190. procedure thlcgcpu.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  191. begin
  192. if paramanager.use_fixed_stack then
  193. begin
  194. inherited;
  195. exit;
  196. end;
  197. tcg386(cg).g_releasevaluepara_openarray(list,l);
  198. end;
  199. procedure create_hlcodegen;
  200. begin
  201. hlcg:=thlcgcpu.create;
  202. create_codegen;
  203. end;
  204. end.