n8086mem.pas 8.9 KB

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