n8086mem.pas 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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;
  24. type
  25. ti8086addrnode = class(tcgaddrnode)
  26. procedure pass_generate_code;override;
  27. end;
  28. ti8086derefnode = class(tx86derefnode)
  29. procedure pass_generate_code;override;
  30. end;
  31. implementation
  32. uses
  33. systems,globals,
  34. cutils,verbose,
  35. symbase,symconst,symdef,symtable,symtype,symsym,
  36. parabase,paramgr,
  37. aasmtai,aasmdata,
  38. nld,ncon,nadd,
  39. cgutils,cgobj,
  40. defutil,hlcgobj,
  41. pass_2,ncgutil;
  42. {*****************************************************************************
  43. TI8086ADDRNODE
  44. *****************************************************************************}
  45. procedure ti8086addrnode.pass_generate_code;
  46. var
  47. segref: treference;
  48. begin
  49. if (current_settings.x86memorymodel in x86_far_code_models) and
  50. (left.nodetype=loadn) and
  51. (tloadnode(left).symtableentry.typ=labelsym) then
  52. begin
  53. secondpass(left);
  54. location_reset(location,LOC_REGISTER,OS_32);
  55. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  56. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  57. internalerror(2013091801);
  58. { load offset }
  59. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  60. { load segment }
  61. segref:=left.location.reference;
  62. segref.refaddr:=addr_seg;
  63. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,segref,GetNextReg(location.register));
  64. end
  65. else
  66. inherited;
  67. end;
  68. {*****************************************************************************
  69. TI8086DEREFNODE
  70. *****************************************************************************}
  71. procedure ti8086derefnode.pass_generate_code;
  72. var
  73. paraloc1 : tcgpara;
  74. pd : tprocdef;
  75. sym : tsym;
  76. st : tsymtable;
  77. tmpref: treference;
  78. begin
  79. if tpointerdef(left.resultdef).x86pointertyp in [x86pt_far,x86pt_huge] then
  80. begin
  81. secondpass(left);
  82. { assume natural alignment, except for packed records }
  83. if not(resultdef.typ in [recorddef,objectdef]) or
  84. (tabstractrecordsymtable(tabstractrecorddef(resultdef).symtable).usefieldalignment<>1) then
  85. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),resultdef.alignment)
  86. else
  87. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),1);
  88. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE,LOC_CONSTANT]) then
  89. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  90. case left.location.loc of
  91. LOC_CREGISTER,
  92. LOC_REGISTER:
  93. begin
  94. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,true);
  95. location.reference.base := left.location.register;
  96. location.reference.segment := GetNextReg(left.location.register);
  97. end;
  98. LOC_CREFERENCE,
  99. LOC_REFERENCE:
  100. begin
  101. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  102. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,left.location.reference,location.reference.base);
  103. location.reference.segment:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  104. tmpref:=left.location.reference;
  105. inc(tmpref.offset,2);
  106. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,tmpref,location.reference.segment);
  107. end;
  108. LOC_CONSTANT:
  109. begin
  110. location.reference.offset:=left.location.value and $FFFF;
  111. location.reference.segment:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  112. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_16,(left.location.value shr 16) and $FFFF,location.reference.segment);
  113. end;
  114. else
  115. internalerror(200507031);
  116. end;
  117. if (cs_use_heaptrc in current_settings.globalswitches) and
  118. (cs_checkpointer in current_settings.localswitches) and
  119. not(cs_compilesystem in current_settings.moduleswitches) and
  120. {$ifdef x86}
  121. (tpointerdef(left.resultdef).x86pointertyp = default_x86_data_pointer_type) and
  122. {$endif x86}
  123. not(nf_no_checkpointer in flags) and
  124. { can be NR_NO in case of LOC_CONSTANT }
  125. (location.reference.base<>NR_NO) then
  126. begin
  127. if not searchsym_in_named_module('HEAPTRC','CHECKPOINTER',sym,st) or
  128. (sym.typ<>procsym) then
  129. internalerror(2012010601);
  130. pd:=tprocdef(tprocsym(sym).ProcdefList[0]);
  131. paraloc1.init;
  132. paramanager.getintparaloc(pd,1,paraloc1);
  133. hlcg.a_load_reg_cgpara(current_asmdata.CurrAsmList,resultdef,location.reference.base,paraloc1);
  134. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  135. paraloc1.done;
  136. hlcg.allocallcpuregisters(current_asmdata.CurrAsmList);
  137. hlcg.a_call_name(current_asmdata.CurrAsmList,pd,'FPC_CHECKPOINTER',nil,false);
  138. hlcg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  139. end;
  140. end
  141. else
  142. inherited pass_generate_code;
  143. end;
  144. begin
  145. caddrnode:=ti8086addrnode;
  146. cderefnode:=ti8086derefnode;
  147. end.