hlcgcpu.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 }
  30. thlcgcpu = class(thlcgx86)
  31. protected
  32. procedure gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint); override;
  33. public
  34. function getaddressregister(list:TAsmList;size:tdef):Tregister;override;
  35. procedure reference_reset_base(var ref: treference; regsize: tdef; reg: tregister; offset, alignment: longint); override;
  36. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);override;
  37. procedure g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister); override;
  38. procedure g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation); override;
  39. procedure location_force_mem(list:TAsmList;var l:tlocation;size:tdef);override;
  40. end;
  41. procedure create_hlcodegen;
  42. implementation
  43. uses
  44. globtype,verbose,
  45. paramgr,
  46. cpubase,cpuinfo,tgobj,cgobj,cgcpu,
  47. defutil,
  48. symconst,
  49. procinfo,
  50. aasmcpu;
  51. { thlcgcpu }
  52. procedure thlcgcpu.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  53. var
  54. locsize : tcgsize;
  55. tmploc : tlocation;
  56. href : treference;
  57. stacksize : longint;
  58. begin
  59. if not(l.size in [OS_32,OS_S32,OS_64,OS_S64,OS_128,OS_S128]) then
  60. locsize:=l.size
  61. else
  62. locsize:=int_float_cgsize(tcgsize2size[l.size]);
  63. case l.loc of
  64. LOC_FPUREGISTER,
  65. LOC_CFPUREGISTER:
  66. begin
  67. case cgpara.location^.loc of
  68. LOC_REFERENCE:
  69. begin
  70. stacksize:=align(locintsize,cgpara.alignment);
  71. if (not paramanager.use_fixed_stack) and
  72. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  73. begin
  74. cg.g_stackpointer_alloc(list,stacksize);
  75. reference_reset_base(href,voidstackpointertype,NR_STACK_POINTER_REG,0,voidstackpointertype.size);
  76. end
  77. else
  78. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  79. cg.a_loadfpu_reg_ref(list,locsize,locsize,l.register,href);
  80. end;
  81. LOC_FPUREGISTER:
  82. begin
  83. cg.a_loadfpu_reg_reg(list,locsize,cgpara.location^.size,l.register,cgpara.location^.register);
  84. end;
  85. { can happen if a record with only 1 "single field" is
  86. returned in a floating point register and then is directly
  87. passed to a regcall parameter }
  88. LOC_REGISTER:
  89. begin
  90. tmploc:=l;
  91. location_force_mem(list,tmploc,size);
  92. case locsize of
  93. OS_F32:
  94. tmploc.size:=OS_32;
  95. OS_F64:
  96. tmploc.size:=OS_64;
  97. else
  98. internalerror(2010053116);
  99. end;
  100. cg.a_load_loc_cgpara(list,tmploc,cgpara);
  101. location_freetemp(list,tmploc);
  102. end
  103. else
  104. internalerror(2010053003);
  105. end;
  106. end;
  107. LOC_MMREGISTER,
  108. LOC_CMMREGISTER:
  109. begin
  110. case cgpara.location^.loc of
  111. LOC_REFERENCE:
  112. begin
  113. { can't use TCGSize2Size[l.size], because the size of an
  114. 80 bit extended parameter can be either 10 or 12 bytes }
  115. stacksize:=align(locintsize,cgpara.alignment);
  116. if (not paramanager.use_fixed_stack) and
  117. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  118. begin
  119. cg.g_stackpointer_alloc(list,stacksize);
  120. reference_reset_base(href,voidstackpointertype,NR_STACK_POINTER_REG,0,voidstackpointertype.size);
  121. end
  122. else
  123. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  124. cg.a_loadmm_reg_ref(list,locsize,locsize,l.register,href,mms_movescalar);
  125. end;
  126. LOC_FPUREGISTER:
  127. begin
  128. tmploc:=l;
  129. location_force_mem(list,tmploc,size);
  130. cg.a_loadfpu_ref_cgpara(list,tmploc.size,tmploc.reference,cgpara);
  131. location_freetemp(list,tmploc);
  132. end;
  133. else
  134. internalerror(2010053004);
  135. end;
  136. end;
  137. LOC_REFERENCE,
  138. LOC_CREFERENCE :
  139. begin
  140. case cgpara.location^.loc of
  141. LOC_REFERENCE:
  142. begin
  143. stacksize:=align(locintsize,cgpara.alignment);
  144. if (not paramanager.use_fixed_stack) and
  145. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  146. cg.a_load_ref_cgpara(list,locsize,l.reference,cgpara)
  147. else
  148. begin
  149. reference_reset_base(href,voidstackpointertype,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  150. cg.g_concatcopy(list,l.reference,href,stacksize);
  151. end;
  152. end;
  153. LOC_FPUREGISTER:
  154. begin
  155. cg.a_loadfpu_ref_cgpara(list,locsize,l.reference,cgpara);
  156. end;
  157. else
  158. internalerror(2010053005);
  159. end;
  160. end;
  161. else
  162. internalerror(2002042430);
  163. end;
  164. end;
  165. function thlcgcpu.getaddressregister(list: TAsmList; size: tdef): Tregister;
  166. begin
  167. if is_farpointer(size) or is_hugepointer(size) then
  168. Result:=cg.getintregister(list,OS_32)
  169. else
  170. Result:=cg.getintregister(list,OS_16);
  171. end;
  172. procedure thlcgcpu.reference_reset_base(var ref: treference; regsize: tdef;
  173. reg: tregister; offset, alignment: longint);
  174. begin
  175. inherited reference_reset_base(ref, regsize, reg, offset, alignment);
  176. if is_farpointer(regsize) or is_hugepointer(regsize) then
  177. ref.segment:=GetNextReg(reg);
  178. end;
  179. procedure thlcgcpu.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  180. var
  181. tmpref,segref: treference;
  182. begin
  183. { step 1: call the x86 low level code generator to handle the offset;
  184. we set the segment to NR_NO to disable the i8086 segment handling code
  185. in the low level cg (which can be removed, once all calls to
  186. a_loadaddr_ref_reg go through the high level code generator) }
  187. tmpref:=ref;
  188. tmpref.segment:=NR_NO;
  189. cg.a_loadaddr_ref_reg(list, tmpref, r);
  190. { step 2: if destination is a far pointer, we have to pass a segment as well }
  191. if is_farpointer(tosize) or is_hugepointer(tosize) then
  192. begin
  193. { if a segment register is specified in ref, we use that }
  194. if ref.segment<>NR_NO then
  195. begin
  196. if is_segment_reg(ref.segment) then
  197. list.concat(Taicpu.op_reg_reg(A_MOV,S_W,ref.segment,GetNextReg(r)))
  198. else
  199. cg.a_load_reg_reg(list,OS_16,OS_16,ref.segment,GetNextReg(r));
  200. end
  201. { references relative to a symbol use the segment of the symbol,
  202. which can be obtained by the SEG directive }
  203. else if assigned(ref.symbol) then
  204. begin
  205. reference_reset_symbol(segref,ref.symbol,0,0);
  206. segref.refaddr:=addr_seg;
  207. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,segref,GetNextReg(r));
  208. end
  209. else if ref.base=NR_BP then
  210. list.concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_SS,GetNextReg(r)))
  211. else
  212. internalerror(2014032801);
  213. end;
  214. end;
  215. procedure thlcgcpu.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  216. begin
  217. if paramanager.use_fixed_stack then
  218. begin
  219. inherited;
  220. exit;
  221. end;
  222. tcg8086(cg).g_copyvaluepara_openarray(list,ref,lenloc,arrdef.elesize,destreg);
  223. end;
  224. procedure thlcgcpu.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  225. begin
  226. if paramanager.use_fixed_stack then
  227. begin
  228. inherited;
  229. exit;
  230. end;
  231. tcg8086(cg).g_releasevaluepara_openarray(list,l);
  232. end;
  233. procedure thlcgcpu.location_force_mem(list: TAsmList; var l: tlocation; size: tdef);
  234. var
  235. r,tmpref: treference;
  236. is_sixbyterecord: Boolean;
  237. is_fourbyterecord: Boolean;
  238. is_methodptr: Boolean;
  239. is_nestedprocptr: Boolean;
  240. begin
  241. is_sixbyterecord:=(size.typ=recorddef) and (size.size=6);
  242. is_fourbyterecord:=(size.typ=recorddef) and (size.size=4);
  243. is_methodptr:=(size.typ=procvardef)
  244. and (po_methodpointer in tprocvardef(size).procoptions)
  245. and not(po_addressonly in tprocvardef(size).procoptions);
  246. is_nestedprocptr:=(size.typ=procvardef)
  247. and is_nested_pd(tprocvardef(size))
  248. and not(po_addressonly in tprocvardef(size).procoptions);
  249. { handle i8086 method pointers (incl. 6-byte mixed near + far),
  250. 6-byte records and nested proc ptrs }
  251. if (is_sixbyterecord or is_methodptr or is_nestedprocptr) and (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  252. begin
  253. tg.gethltemp(list,size,size.size,tt_normal,r);
  254. tmpref:=r;
  255. if current_settings.x86memorymodel in x86_far_code_models then
  256. begin
  257. cg.a_load_reg_ref(list,OS_32,OS_32,l.register,tmpref);
  258. inc(tmpref.offset,4);
  259. end
  260. else
  261. begin
  262. cg.a_load_reg_ref(list,OS_16,OS_16,l.register,tmpref);
  263. inc(tmpref.offset,2);
  264. end;
  265. if current_settings.x86memorymodel in x86_far_data_models then
  266. cg.a_load_reg_ref(list,OS_32,OS_32,l.registerhi,tmpref)
  267. else
  268. cg.a_load_reg_ref(list,OS_16,OS_16,l.registerhi,tmpref);
  269. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  270. l.reference:=r;
  271. end
  272. { 4-byte records in registers need special handling as well. A record may
  273. be located in registerhi:register if it was converted from a procvar or
  274. in GetNextReg(register):register if it was converted from a longint.
  275. We can tell between the two by checking whether registerhi has been set. }
  276. else if is_fourbyterecord and (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  277. begin
  278. tg.gethltemp(list,size,size.size,tt_normal,r);
  279. tmpref:=r;
  280. cg.a_load_reg_ref(list,OS_16,OS_16,l.register,tmpref);
  281. inc(tmpref.offset,2);
  282. if l.registerhi<>tregister(0) then
  283. cg.a_load_reg_ref(list,OS_16,OS_16,l.registerhi,tmpref)
  284. else
  285. cg.a_load_reg_ref(list,OS_16,OS_16,GetNextReg(l.register),tmpref);
  286. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  287. l.reference:=r;
  288. end
  289. else
  290. inherited;
  291. end;
  292. procedure create_hlcodegen;
  293. begin
  294. hlcg:=thlcgcpu.create;
  295. create_codegen;
  296. end;
  297. end.