n8086mem.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. procedure update_reference_reg_mul(maybe_const_reg:tregister;l:aint);override;
  36. end;
  37. implementation
  38. uses
  39. systems,globals,
  40. cutils,verbose,
  41. symbase,symconst,symdef,symtable,symtype,symsym,symcpu,
  42. parabase,paramgr,
  43. aasmtai,aasmdata,
  44. nld,ncon,nadd,
  45. cgutils,cgobj,
  46. defutil,hlcgobj,
  47. pass_2,ncgutil;
  48. {*****************************************************************************
  49. TI8086ADDRNODE
  50. *****************************************************************************}
  51. procedure ti8086addrnode.set_absvarsym_resultdef;
  52. begin
  53. if not(nf_typedaddr in flags) then
  54. resultdef:=voidfarpointertype
  55. else
  56. resultdef:=tcpupointerdefclass(cpointerdef).createx86(left.resultdef,x86pt_far);
  57. end;
  58. function ti8086addrnode.typecheck_non_proc(realsource: tnode; out res: tnode): boolean;
  59. begin
  60. res:=nil;
  61. if (realsource.nodetype=loadn) and
  62. (tloadnode(realsource).symtableentry.typ=labelsym) then
  63. begin
  64. if current_settings.x86memorymodel in x86_far_code_models then
  65. resultdef:=voidfarpointertype
  66. else
  67. resultdef:=voidnearpointertype;
  68. result:=true
  69. end
  70. else
  71. result:=inherited;
  72. end;
  73. {*****************************************************************************
  74. TI8086DEREFNODE
  75. *****************************************************************************}
  76. procedure ti8086derefnode.pass_generate_code;
  77. var
  78. paraloc1 : tcgpara;
  79. pd : tprocdef;
  80. sym : tsym;
  81. st : tsymtable;
  82. tmpref: treference;
  83. begin
  84. if tcpupointerdef(left.resultdef).x86pointertyp in [x86pt_far,x86pt_huge] then
  85. begin
  86. secondpass(left);
  87. { assume natural alignment, except for packed records }
  88. if not(resultdef.typ in [recorddef,objectdef]) or
  89. (tabstractrecordsymtable(tabstractrecorddef(resultdef).symtable).usefieldalignment<>1) then
  90. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),resultdef.alignment)
  91. else
  92. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),1);
  93. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE,LOC_CONSTANT]) then
  94. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  95. case left.location.loc of
  96. LOC_CREGISTER,
  97. LOC_REGISTER:
  98. begin
  99. hlcg.maybe_change_load_node_reg(current_asmdata.CurrAsmList,left,true);
  100. location.reference.base := left.location.register;
  101. location.reference.segment := GetNextReg(left.location.register);
  102. end;
  103. LOC_CREFERENCE,
  104. LOC_REFERENCE:
  105. begin
  106. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  107. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,left.location.reference,location.reference.base);
  108. location.reference.segment:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  109. tmpref:=left.location.reference;
  110. inc(tmpref.offset,2);
  111. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,tmpref,location.reference.segment);
  112. end;
  113. LOC_CONSTANT:
  114. begin
  115. location.reference.offset:=left.location.value and $FFFF;
  116. location.reference.segment:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  117. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_16,(left.location.value shr 16) and $FFFF,location.reference.segment);
  118. end;
  119. else
  120. internalerror(200507031);
  121. end;
  122. if (cs_use_heaptrc in current_settings.globalswitches) and
  123. (cs_checkpointer in current_settings.localswitches) and
  124. not(cs_compilesystem in current_settings.moduleswitches) and
  125. {$ifdef x86}
  126. (tcpupointerdef(left.resultdef).x86pointertyp = tcpupointerdefclass(cpointerdef).default_x86_data_pointer_type) and
  127. {$endif x86}
  128. not(nf_no_checkpointer in flags) and
  129. { can be NR_NO in case of LOC_CONSTANT }
  130. (location.reference.base<>NR_NO) then
  131. begin
  132. if not searchsym_in_named_module('HEAPTRC','CHECKPOINTER',sym,st) or
  133. (sym.typ<>procsym) then
  134. internalerror(2012010601);
  135. pd:=tprocdef(tprocsym(sym).ProcdefList[0]);
  136. paraloc1.init;
  137. paramanager.getintparaloc(pd,1,paraloc1);
  138. hlcg.a_load_reg_cgpara(current_asmdata.CurrAsmList,resultdef,location.reference.base,paraloc1);
  139. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  140. paraloc1.done;
  141. hlcg.allocallcpuregisters(current_asmdata.CurrAsmList);
  142. hlcg.a_call_name(current_asmdata.CurrAsmList,pd,'FPC_CHECKPOINTER',nil,false);
  143. hlcg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  144. end;
  145. end
  146. else
  147. inherited pass_generate_code;
  148. end;
  149. {*****************************************************************************
  150. TI8086VECNODE
  151. *****************************************************************************}
  152. procedure ti8086vecnode.update_reference_reg_mul(maybe_const_reg:tregister;l:aint);
  153. var
  154. saveseg: TRegister;
  155. begin
  156. saveseg:=location.reference.segment;
  157. inherited update_reference_reg_mul(maybe_const_reg,l);
  158. location.reference.segment:=saveseg;
  159. end;
  160. begin
  161. caddrnode:=ti8086addrnode;
  162. cderefnode:=ti8086derefnode;
  163. cvecnode:=ti8086vecnode;
  164. end.