hlcgcpu.pas 8.3 KB

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