n8086mem.pas 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_absvarsym_resultdef; override;
  29. procedure pass_generate_code;override;
  30. public
  31. get_offset_only: boolean;
  32. end;
  33. ti8086derefnode = class(tx86derefnode)
  34. procedure pass_generate_code;override;
  35. end;
  36. { tx86vecnode doesn't work for i8086, so we inherit tcgvecnode }
  37. ti8086vecnode = class(tcgvecnode)
  38. protected
  39. function first_arraydef: tnode;override;
  40. procedure update_reference_reg_mul(maybe_const_reg: tregister; regsize: tdef; l: aint);override;
  41. end;
  42. implementation
  43. uses
  44. systems,globals,constexp,
  45. cutils,verbose,
  46. symbase,symconst,symdef,symtable,symsym,symx86,symcpu,
  47. parabase,paramgr,
  48. aasmtai,aasmdata,
  49. nld,ncon,nadd,ncal,ncnv,
  50. cgutils,cgobj,
  51. defutil,hlcgobj,
  52. pass_1,pass_2,ncgutil;
  53. {*****************************************************************************
  54. TI8086ADDRNODE
  55. *****************************************************************************}
  56. procedure ti8086addrnode.set_absvarsym_resultdef;
  57. begin
  58. if not(nf_typedaddr in flags) then
  59. resultdef:=voidfarpointertype
  60. else
  61. resultdef:=tcpupointerdefclass(cpointerdef).createx86(left.resultdef,x86pt_far);
  62. end;
  63. procedure ti8086addrnode.pass_generate_code;
  64. begin
  65. if get_offset_only then
  66. begin
  67. secondpass(left);
  68. location_reset(location,LOC_REGISTER,OS_16);
  69. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidnearpointertype);
  70. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  71. internalerror(2015103001);
  72. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,voidnearpointertype,left.location.reference,location.register);
  73. end
  74. else
  75. inherited;
  76. end;
  77. {*****************************************************************************
  78. TI8086DEREFNODE
  79. *****************************************************************************}
  80. procedure ti8086derefnode.pass_generate_code;
  81. var
  82. paraloc1 : tcgpara;
  83. pd : tprocdef;
  84. sym : tsym;
  85. st : tsymtable;
  86. tmpref: treference;
  87. begin
  88. if tcpupointerdef(left.resultdef).x86pointertyp in [x86pt_far,x86pt_huge] then
  89. begin
  90. secondpass(left);
  91. { assume natural alignment, except for packed records }
  92. if not(resultdef.typ in [recorddef,objectdef]) or
  93. (tabstractrecordsymtable(tabstractrecorddef(resultdef).symtable).usefieldalignment<>1) then
  94. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),resultdef.alignment,[])
  95. else
  96. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),1,[]);
  97. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE,LOC_CONSTANT]) then
  98. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  99. case left.location.loc of
  100. LOC_CREGISTER,
  101. LOC_REGISTER:
  102. begin
  103. hlcg.maybe_change_load_node_reg(current_asmdata.CurrAsmList,left,true);
  104. location.reference.base := left.location.register;
  105. location.reference.segment := cg.GetNextReg(left.location.register);
  106. end;
  107. LOC_CREFERENCE,
  108. LOC_REFERENCE:
  109. begin
  110. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  111. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,left.location.reference,location.reference.base);
  112. location.reference.segment:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  113. tmpref:=left.location.reference;
  114. inc(tmpref.offset,2);
  115. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,tmpref,location.reference.segment);
  116. end;
  117. LOC_CONSTANT:
  118. begin
  119. location.reference.offset:=left.location.value and $FFFF;
  120. location.reference.segment:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  121. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_16,(left.location.value shr 16) and $FFFF,location.reference.segment);
  122. end;
  123. else
  124. internalerror(200507031);
  125. end;
  126. if (cs_use_heaptrc in current_settings.globalswitches) and
  127. (cs_checkpointer in current_settings.localswitches) and
  128. not(cs_compilesystem in current_settings.moduleswitches) and
  129. {$ifdef x86}
  130. (tcpupointerdef(left.resultdef).x86pointertyp = tcpupointerdefclass(cpointerdef).default_x86_data_pointer_type) and
  131. {$endif x86}
  132. not(nf_no_checkpointer in flags) and
  133. { can be NR_NO in case of LOC_CONSTANT }
  134. (location.reference.base<>NR_NO) then
  135. begin
  136. if not searchsym_in_named_module('HEAPTRC','CHECKPOINTER',sym,st) or
  137. (sym.typ<>procsym) then
  138. internalerror(2012010601);
  139. pd:=tprocdef(tprocsym(sym).ProcdefList[0]);
  140. paraloc1.init;
  141. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  142. hlcg.a_loadaddr_ref_cgpara(current_asmdata.CurrAsmList,resultdef,location.reference,paraloc1);
  143. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  144. paraloc1.done;
  145. hlcg.allocallcpuregisters(current_asmdata.CurrAsmList);
  146. hlcg.a_call_name(current_asmdata.CurrAsmList,pd,'FPC_CHECKPOINTER',[],nil,false);
  147. hlcg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  148. system.include(current_settings.moduleswitches,cs_checkpointer_called);
  149. end;
  150. end
  151. else
  152. inherited pass_generate_code;
  153. end;
  154. {*****************************************************************************
  155. TI8086VECNODE
  156. *****************************************************************************}
  157. function ti8086vecnode.first_arraydef: tnode;
  158. var
  159. arraydef: tcpuarraydef;
  160. procname:string;
  161. begin
  162. if tcpuarraydef(left.resultdef).is_huge then
  163. begin
  164. arraydef:=tcpuarraydef(left.resultdef);
  165. if not (ado_IsConvertedPointer in arraydef.arrayoptions) then
  166. internalerror(2014080701);
  167. if left.nodetype<>typeconvn then
  168. internalerror(2014080702);
  169. procname:='fpc_hugeptr_add_longint';
  170. if cs_hugeptr_arithmetic_normalization in current_settings.localswitches then
  171. procname:=procname+'_normalized';
  172. if arraydef.elementdef.size>1 then
  173. right:=caddnode.create(muln,right,
  174. cordconstnode.create(arraydef.elementdef.size,s32inttype,true));
  175. result:=ccallnode.createintern(procname,
  176. ccallparanode.create(right,
  177. ccallparanode.create(ttypeconvnode(left).left,nil)));
  178. inserttypeconv_internal(result,tx86pointerdef(cpointerdef).getreusablex86(arraydef.elementdef,x86pt_huge));
  179. result:=cderefnode.create(result);
  180. ttypeconvnode(left).left:=nil;
  181. ttypeconvnode(left).free;
  182. left := nil;
  183. right := nil;
  184. firstpass(result);
  185. end
  186. else
  187. result:=inherited;
  188. end;
  189. procedure ti8086vecnode.update_reference_reg_mul(maybe_const_reg: tregister; regsize: tdef; l: aint);
  190. var
  191. saveseg: TRegister;
  192. begin
  193. saveseg:=location.reference.segment;
  194. location.reference.segment:=NR_NO;
  195. inherited;
  196. location.reference.segment:=saveseg;
  197. end;
  198. begin
  199. caddrnode:=ti8086addrnode;
  200. cderefnode:=ti8086derefnode;
  201. cvecnode:=ti8086vecnode;
  202. end.