hlcgcpu.pas 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. 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. procedure g_exception_reason_save(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const href: treference); override;
  36. procedure g_exception_reason_save_const(list: TAsmList; size: tdef; a: tcgint; const href: treference); override;
  37. procedure g_exception_reason_load(list: TAsmList; fromsize, tosize: tdef; const href: treference; reg: tregister); override;
  38. procedure g_exception_reason_discard(list: TAsmList; size: tdef; href: treference); override;
  39. end;
  40. procedure create_hlcodegen;
  41. implementation
  42. uses
  43. verbose,
  44. paramgr,
  45. defutil,
  46. cpubase,aasmcpu,tgobj,cgobj,cgx86,cgcpu;
  47. { thlcgcpu }
  48. procedure thlcgcpu.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  49. var
  50. locsize : tcgsize;
  51. tmploc : tlocation;
  52. href : treference;
  53. stacksize : longint;
  54. begin
  55. if not(l.size in [OS_32,OS_S32,OS_64,OS_S64,OS_128,OS_S128]) then
  56. locsize:=l.size
  57. else
  58. locsize:=int_float_cgsize(tcgsize2size[l.size]);
  59. case l.loc of
  60. LOC_FPUREGISTER,
  61. LOC_CFPUREGISTER:
  62. begin
  63. case cgpara.location^.loc of
  64. LOC_REFERENCE:
  65. begin
  66. stacksize:=align(locintsize,cgpara.alignment);
  67. if (not paramanager.use_fixed_stack) and
  68. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  69. begin
  70. cg.g_stackpointer_alloc(list,stacksize);
  71. reference_reset_base(href,voidstackpointertype,NR_STACK_POINTER_REG,0,voidstackpointertype.size);
  72. end
  73. else
  74. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  75. cg.a_loadfpu_reg_ref(list,locsize,locsize,l.register,href);
  76. end;
  77. LOC_FPUREGISTER:
  78. begin
  79. cg.a_loadfpu_reg_reg(list,locsize,cgpara.location^.size,l.register,cgpara.location^.register);
  80. end;
  81. { can happen if a record with only 1 "single field" is
  82. returned in a floating point register and then is directly
  83. passed to a regcall parameter }
  84. LOC_REGISTER:
  85. begin
  86. tmploc:=l;
  87. location_force_mem(list,tmploc,size);
  88. case locsize of
  89. OS_F32:
  90. tmploc.size:=OS_32;
  91. OS_F64:
  92. tmploc.size:=OS_64;
  93. else
  94. internalerror(2010053116);
  95. end;
  96. cg.a_load_loc_cgpara(list,tmploc,cgpara);
  97. location_freetemp(list,tmploc);
  98. end
  99. else
  100. internalerror(2010053003);
  101. end;
  102. end;
  103. LOC_MMREGISTER,
  104. LOC_CMMREGISTER:
  105. begin
  106. case cgpara.location^.loc of
  107. LOC_REFERENCE:
  108. begin
  109. { can't use TCGSize2Size[l.size], because the size of an
  110. 80 bit extended parameter can be either 10 or 12 bytes }
  111. stacksize:=align(locintsize,cgpara.alignment);
  112. if (not paramanager.use_fixed_stack) and
  113. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  114. begin
  115. cg.g_stackpointer_alloc(list,stacksize);
  116. reference_reset_base(href,voidstackpointertype,NR_STACK_POINTER_REG,0,voidstackpointertype.size);
  117. end
  118. else
  119. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  120. cg.a_loadmm_reg_ref(list,locsize,locsize,l.register,href,mms_movescalar);
  121. end;
  122. LOC_FPUREGISTER:
  123. begin
  124. tmploc:=l;
  125. location_force_mem(list,tmploc,size);
  126. cg.a_loadfpu_ref_cgpara(list,tmploc.size,tmploc.reference,cgpara);
  127. location_freetemp(list,tmploc);
  128. end;
  129. else
  130. internalerror(2010053004);
  131. end;
  132. end;
  133. LOC_REFERENCE,
  134. LOC_CREFERENCE :
  135. begin
  136. case cgpara.location^.loc of
  137. LOC_REFERENCE:
  138. begin
  139. stacksize:=align(locintsize,cgpara.alignment);
  140. if (not paramanager.use_fixed_stack) and
  141. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  142. cg.a_load_ref_cgpara(list,locsize,l.reference,cgpara)
  143. else
  144. begin
  145. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  146. cg.g_concatcopy(list,l.reference,href,stacksize);
  147. end;
  148. end;
  149. LOC_FPUREGISTER:
  150. begin
  151. cg.a_loadfpu_ref_cgpara(list,locsize,l.reference,cgpara);
  152. end;
  153. else
  154. internalerror(2010053005);
  155. end;
  156. end;
  157. else
  158. internalerror(2002042430);
  159. end;
  160. end;
  161. procedure thlcgcpu.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  162. begin
  163. if paramanager.use_fixed_stack then
  164. begin
  165. inherited;
  166. exit;
  167. end;
  168. tcg386(cg).g_copyvaluepara_openarray(list,ref,lenloc,arrdef.elesize,destreg);
  169. end;
  170. procedure thlcgcpu.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  171. begin
  172. if paramanager.use_fixed_stack then
  173. begin
  174. inherited;
  175. exit;
  176. end;
  177. tcg386(cg).g_releasevaluepara_openarray(list,l);
  178. end;
  179. procedure thlcgcpu.g_exception_reason_save(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const href: treference);
  180. begin
  181. if not paramanager.use_fixed_stack then
  182. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[def_cgsize(tosize)],reg))
  183. else
  184. inherited
  185. end;
  186. procedure thlcgcpu.g_exception_reason_save_const(list: TAsmList; size: tdef; a: tcgint; const href: treference);
  187. begin
  188. if not paramanager.use_fixed_stack then
  189. list.concat(Taicpu.op_const(A_PUSH,tcgsize2opsize[def_cgsize(size)],a))
  190. else
  191. inherited;
  192. end;
  193. procedure thlcgcpu.g_exception_reason_load(list: TAsmList; fromsize, tosize: tdef; const href: treference; reg: tregister);
  194. begin
  195. if not paramanager.use_fixed_stack then
  196. list.concat(Taicpu.op_reg(A_POP,tcgsize2opsize[def_cgsize(tosize)],reg))
  197. else
  198. inherited;
  199. end;
  200. procedure thlcgcpu.g_exception_reason_discard(list: TAsmList; size: tdef; href: treference);
  201. begin
  202. if not paramanager.use_fixed_stack then
  203. begin
  204. getcpuregister(list,NR_FUNCTION_RESULT_REG);
  205. list.concat(Taicpu.op_reg(A_POP,tcgsize2opsize[def_cgsize(size)],NR_FUNCTION_RESULT_REG));
  206. ungetcpuregister(list,NR_FUNCTION_RESULT_REG);
  207. end;
  208. end;
  209. procedure create_hlcodegen;
  210. begin
  211. hlcg:=thlcgcpu.create;
  212. create_codegen;
  213. end;
  214. begin
  215. chlcgobj:=thlcgcpu;
  216. end.