n8086mem.pas 9.4 KB

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