n8086mem.pas 9.7 KB

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