hlcgcpu.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. globtype,
  24. aasmdata,
  25. symtype,symdef,parabase,
  26. cgbase,cgutils,
  27. hlcgobj, hlcgx86;
  28. type
  29. thlcgcpu = class(thlcgx86)
  30. protected
  31. procedure gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint); override;
  32. public
  33. function a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; forceresdef: tdef; weak: boolean): tcgpara; override;
  34. procedure g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister); override;
  35. procedure g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation); override;
  36. end;
  37. procedure create_hlcodegen;
  38. implementation
  39. uses
  40. globals, procinfo, symconst,
  41. verbose,
  42. fmodule,systems,
  43. aasmbase,aasmtai,aasmcpu,
  44. paramgr,
  45. cpubase,tgobj,cgobj,cgcpu;
  46. { thlcgcpu }
  47. procedure thlcgcpu.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  48. var
  49. locsize : tcgsize;
  50. tmploc : tlocation;
  51. href : treference;
  52. stacksize : longint;
  53. begin
  54. if not(l.size in [OS_32,OS_S32,OS_64,OS_S64,OS_128,OS_S128]) then
  55. locsize:=l.size
  56. else
  57. locsize:=int_float_cgsize(tcgsize2size[l.size]);
  58. case l.loc of
  59. LOC_FPUREGISTER,
  60. LOC_CFPUREGISTER:
  61. begin
  62. case cgpara.location^.loc of
  63. LOC_REFERENCE:
  64. begin
  65. stacksize:=align(locintsize,cgpara.alignment);
  66. if (not paramanager.use_fixed_stack) and
  67. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  68. begin
  69. cg.g_stackpointer_alloc(list,stacksize);
  70. reference_reset_base(href,voidstackpointertype,NR_STACK_POINTER_REG,0,voidstackpointertype.size);
  71. end
  72. else
  73. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  74. cg.a_loadfpu_reg_ref(list,locsize,locsize,l.register,href);
  75. end;
  76. LOC_FPUREGISTER:
  77. begin
  78. cg.a_loadfpu_reg_reg(list,locsize,cgpara.location^.size,l.register,cgpara.location^.register);
  79. end;
  80. { can happen if a record with only 1 "single field" is
  81. returned in a floating point register and then is directly
  82. passed to a regcall parameter }
  83. LOC_REGISTER:
  84. begin
  85. tmploc:=l;
  86. location_force_mem(list,tmploc,size);
  87. case locsize of
  88. OS_F32:
  89. tmploc.size:=OS_32;
  90. OS_F64:
  91. tmploc.size:=OS_64;
  92. else
  93. internalerror(2010053116);
  94. end;
  95. cg.a_load_loc_cgpara(list,tmploc,cgpara);
  96. location_freetemp(list,tmploc);
  97. end
  98. else
  99. internalerror(2010053003);
  100. end;
  101. end;
  102. LOC_MMREGISTER,
  103. LOC_CMMREGISTER:
  104. begin
  105. case cgpara.location^.loc of
  106. LOC_REFERENCE:
  107. begin
  108. { can't use TCGSize2Size[l.size], because the size of an
  109. 80 bit extended parameter can be either 10 or 12 bytes }
  110. stacksize:=align(locintsize,cgpara.alignment);
  111. if (not paramanager.use_fixed_stack) and
  112. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  113. begin
  114. cg.g_stackpointer_alloc(list,stacksize);
  115. reference_reset_base(href,voidstackpointertype,NR_STACK_POINTER_REG,0,voidstackpointertype.size);
  116. end
  117. else
  118. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  119. cg.a_loadmm_reg_ref(list,locsize,locsize,l.register,href,mms_movescalar);
  120. end;
  121. LOC_FPUREGISTER:
  122. begin
  123. tmploc:=l;
  124. location_force_mem(list,tmploc,size);
  125. cg.a_loadfpu_ref_cgpara(list,tmploc.size,tmploc.reference,cgpara);
  126. location_freetemp(list,tmploc);
  127. end;
  128. else
  129. internalerror(2010053004);
  130. end;
  131. end;
  132. LOC_REFERENCE,
  133. LOC_CREFERENCE :
  134. begin
  135. case cgpara.location^.loc of
  136. LOC_REFERENCE:
  137. begin
  138. stacksize:=align(locintsize,cgpara.alignment);
  139. if (not paramanager.use_fixed_stack) and
  140. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  141. cg.a_load_ref_cgpara(list,locsize,l.reference,cgpara)
  142. else
  143. begin
  144. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  145. cg.g_concatcopy(list,l.reference,href,stacksize);
  146. end;
  147. end;
  148. LOC_FPUREGISTER:
  149. begin
  150. cg.a_loadfpu_ref_cgpara(list,locsize,l.reference,cgpara);
  151. end;
  152. else
  153. internalerror(2010053005);
  154. end;
  155. end;
  156. else
  157. internalerror(2002042430);
  158. end;
  159. end;
  160. function thlcgcpu.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; forceresdef: tdef; weak: boolean): tcgpara;
  161. var
  162. need_got_load: boolean;
  163. begin
  164. { Load GOT address to EBX before calling an external function.
  165. It is needed because GOT stubs for external function calls
  166. generated by a linker expect EBX as a GOT register. }
  167. need_got_load:=not (target_info.system in systems_darwin) and
  168. (cs_create_pic in current_settings.moduleswitches) and
  169. (tf_pic_uses_got in target_info.flags) and
  170. (po_external in pd.procoptions);
  171. if need_got_load then
  172. begin
  173. { Alloc EBX }
  174. getcpuregister(list, NR_PIC_OFFSET_REG);
  175. list.concat(taicpu.op_reg_reg(A_MOV,S_L,current_procinfo.got,NR_PIC_OFFSET_REG));
  176. end;
  177. Result:=inherited a_call_name(list, pd, s, forceresdef, weak);
  178. { Free EBX }
  179. if need_got_load then
  180. ungetcpuregister(list, NR_PIC_OFFSET_REG);
  181. end;
  182. procedure thlcgcpu.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  183. begin
  184. if paramanager.use_fixed_stack then
  185. begin
  186. inherited;
  187. exit;
  188. end;
  189. tcg386(cg).g_copyvaluepara_openarray(list,ref,lenloc,arrdef.elesize,destreg);
  190. end;
  191. procedure thlcgcpu.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  192. begin
  193. if paramanager.use_fixed_stack then
  194. begin
  195. inherited;
  196. exit;
  197. end;
  198. tcg386(cg).g_releasevaluepara_openarray(list,l);
  199. end;
  200. procedure create_hlcodegen;
  201. begin
  202. hlcg:=thlcgcpu.create;
  203. create_codegen;
  204. end;
  205. end.