n8086mem.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate i8086 assembler for in memory related nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit n8086mem;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. symtype,
  23. cgbase,cpuinfo,cpubase,
  24. node,nmem,ncgmem,nx86mem,ni86mem;
  25. type
  26. ti8086addrnode = class(ti86addrnode)
  27. protected
  28. procedure set_labelsym_resultdef; override;
  29. procedure set_absvarsym_resultdef; override;
  30. public
  31. get_offset_only: boolean;
  32. procedure pass_generate_code;override;
  33. end;
  34. ti8086derefnode = class(tx86derefnode)
  35. procedure pass_generate_code;override;
  36. end;
  37. { tx86vecnode doesn't work for i8086, so we inherit tcgvecnode }
  38. ti8086vecnode = class(tcgvecnode)
  39. protected
  40. function get_address_type: tdef;override;
  41. function first_arraydef: tnode;override;
  42. procedure update_reference_reg_mul(maybe_const_reg: tregister; regsize: tdef; l: aint);override;
  43. end;
  44. implementation
  45. uses
  46. systems,globals,constexp,
  47. cutils,verbose,
  48. symbase,symconst,symdef,symtable,symsym,symx86,symcpu,
  49. parabase,paramgr,
  50. aasmtai,aasmdata,
  51. nld,ncon,nadd,ncal,ncnv,
  52. cgutils,cgobj,
  53. defutil,hlcgobj,
  54. pass_1,pass_2,ncgutil;
  55. {*****************************************************************************
  56. TI8086ADDRNODE
  57. *****************************************************************************}
  58. procedure ti8086addrnode.set_labelsym_resultdef;
  59. begin
  60. if anf_ofs in addrnodeflags then
  61. resultdef:=voidnearcspointertype
  62. else
  63. inherited;
  64. end;
  65. procedure ti8086addrnode.set_absvarsym_resultdef;
  66. begin
  67. if not(anf_typedaddr in addrnodeflags) then
  68. resultdef:=voidfarpointertype
  69. else
  70. resultdef:=tcpupointerdefclass(cpointerdef).createx86(left.resultdef,x86pt_far);
  71. end;
  72. procedure ti8086addrnode.pass_generate_code;
  73. begin
  74. if get_offset_only then
  75. begin
  76. secondpass(left);
  77. location_reset(location,LOC_REGISTER,OS_16);
  78. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidnearpointertype);
  79. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  80. internalerror(2015103003);
  81. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,voidnearpointertype,left.location.reference,location.register);
  82. end
  83. else
  84. inherited;
  85. end;
  86. {*****************************************************************************
  87. TI8086DEREFNODE
  88. *****************************************************************************}
  89. procedure ti8086derefnode.pass_generate_code;
  90. var
  91. paraloc1 : tcgpara;
  92. pd : tprocdef;
  93. sym : tsym;
  94. st : tsymtable;
  95. tmpref: treference;
  96. begin
  97. if tcpupointerdef(left.resultdef).x86pointertyp in [x86pt_far,x86pt_huge] then
  98. begin
  99. secondpass(left);
  100. { assume natural alignment, except for packed records }
  101. if not(resultdef.typ in [recorddef,objectdef]) or
  102. (tabstractrecordsymtable(tabstractrecorddef(resultdef).symtable).usefieldalignment<>1) then
  103. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),resultdef.alignment,[])
  104. else
  105. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),1,[]);
  106. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE,LOC_CONSTANT]) then
  107. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  108. case left.location.loc of
  109. LOC_CREGISTER,
  110. LOC_REGISTER:
  111. begin
  112. hlcg.maybe_change_load_node_reg(current_asmdata.CurrAsmList,left,true);
  113. location.reference.base := left.location.register;
  114. location.reference.segment := cg.GetNextReg(left.location.register);
  115. end;
  116. LOC_CREFERENCE,
  117. LOC_REFERENCE:
  118. begin
  119. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  120. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,left.location.reference,location.reference.base);
  121. location.reference.segment:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  122. tmpref:=left.location.reference;
  123. inc(tmpref.offset,2);
  124. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,tmpref,location.reference.segment);
  125. end;
  126. LOC_CONSTANT:
  127. begin
  128. location.reference.offset:=left.location.value and $FFFF;
  129. location.reference.segment:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  130. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_16,(left.location.value shr 16) and $FFFF,location.reference.segment);
  131. end;
  132. else
  133. internalerror(2005070302);
  134. end;
  135. if (cs_use_heaptrc in current_settings.globalswitches) and
  136. (cs_checkpointer in current_settings.localswitches) and
  137. not(cs_compilesystem in current_settings.moduleswitches) and
  138. {$ifdef x86}
  139. (tcpupointerdef(left.resultdef).x86pointertyp = tcpupointerdefclass(cpointerdef).default_x86_data_pointer_type) and
  140. {$endif x86}
  141. not(drnf_no_checkpointer in derefnodeflags) and
  142. { can be NR_NO in case of LOC_CONSTANT }
  143. (location.reference.base<>NR_NO) then
  144. begin
  145. if not searchsym_in_named_module('HEAPTRC','CHECKPOINTER',sym,st) or
  146. (sym.typ<>procsym) then
  147. internalerror(2012010603);
  148. pd:=tprocdef(tprocsym(sym).ProcdefList[0]);
  149. paraloc1.init;
  150. paramanager.getcgtempparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  151. hlcg.a_loadaddr_ref_cgpara(current_asmdata.CurrAsmList,resultdef,location.reference,paraloc1);
  152. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  153. paraloc1.done;
  154. hlcg.allocallcpuregisters(current_asmdata.CurrAsmList);
  155. hlcg.a_call_name(current_asmdata.CurrAsmList,pd,'FPC_CHECKPOINTER',[],nil,false);
  156. hlcg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  157. system.include(current_settings.moduleswitches,cs_checkpointer_called);
  158. end;
  159. end
  160. else
  161. inherited pass_generate_code;
  162. end;
  163. {*****************************************************************************
  164. TI8086VECNODE
  165. *****************************************************************************}
  166. function ti8086vecnode.get_address_type: tdef;
  167. begin
  168. result:=tx86pointerdef(cpointerdef).getreusablex86(resultdef,x86pt_near);
  169. end;
  170. function ti8086vecnode.first_arraydef: tnode;
  171. var
  172. arraydef: tcpuarraydef;
  173. procname:string;
  174. begin
  175. if tcpuarraydef(left.resultdef).is_huge then
  176. begin
  177. arraydef:=tcpuarraydef(left.resultdef);
  178. if not (ado_IsConvertedPointer in arraydef.arrayoptions) then
  179. internalerror(2014080701);
  180. if left.nodetype<>typeconvn then
  181. internalerror(2014080702);
  182. procname:='fpc_hugeptr_add_longint';
  183. if cs_hugeptr_arithmetic_normalization in current_settings.localswitches then
  184. procname:=procname+'_normalized';
  185. if arraydef.elementdef.size>1 then
  186. right:=caddnode.create(muln,right,
  187. cordconstnode.create(arraydef.elementdef.size,s32inttype,true));
  188. result:=ccallnode.createintern(procname,
  189. ccallparanode.create(right,
  190. ccallparanode.create(ttypeconvnode(left).left,nil)));
  191. inserttypeconv_internal(result,tx86pointerdef(cpointerdef).getreusablex86(arraydef.elementdef,x86pt_huge));
  192. result:=cderefnode.create(result);
  193. ttypeconvnode(left).left:=nil;
  194. ttypeconvnode(left).free;
  195. left := nil;
  196. right := nil;
  197. firstpass(result);
  198. end
  199. else
  200. result:=inherited;
  201. end;
  202. procedure ti8086vecnode.update_reference_reg_mul(maybe_const_reg: tregister; regsize: tdef; l: aint);
  203. var
  204. saveseg: TRegister;
  205. begin
  206. saveseg:=location.reference.segment;
  207. location.reference.segment:=NR_NO;
  208. inherited;
  209. location.reference.segment:=saveseg;
  210. end;
  211. begin
  212. caddrnode:=ti8086addrnode;
  213. cderefnode:=ti8086derefnode;
  214. cvecnode:=ti8086vecnode;
  215. end.