n8086cnv.pas 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate code for i8086 assembler for type converting 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 n8086cnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncgcnv,nx86cnv,defutil,defcmp;
  22. type
  23. t8086typeconvnode = class(tx86typeconvnode)
  24. protected
  25. procedure second_proc_to_procvar;override;
  26. procedure second_nil_to_methodprocvar;override;
  27. end;
  28. implementation
  29. uses
  30. verbose,systems,globals,globtype,
  31. aasmbase,aasmtai,aasmdata,aasmcpu,
  32. symconst,symdef,
  33. cgbase,cga,procinfo,pass_1,pass_2,
  34. ncon,ncal,ncnv,
  35. cpubase,cpuinfo,
  36. cgutils,cgobj,hlcgobj,cgx86,ncgutil,
  37. tgobj;
  38. procedure t8086typeconvnode.second_proc_to_procvar;
  39. var
  40. tmpreg: tregister;
  41. tmpref: treference;
  42. begin
  43. if not (po_far in tabstractprocdef(resultdef).procoptions) then
  44. begin
  45. inherited;
  46. exit;
  47. end;
  48. if tabstractprocdef(resultdef).is_addressonly then
  49. begin
  50. location_reset(location,LOC_REGISTER,OS_32);
  51. { only a code pointer? (when taking the address of classtype.method
  52. we also only get a code pointer even though the resultdef is a
  53. procedure of object, and hence is_addressonly would return false)
  54. }
  55. if left.location.size = OS_32 then
  56. begin
  57. case left.location.loc of
  58. LOC_REFERENCE,LOC_CREFERENCE:
  59. begin
  60. { the procedure symbol is encoded in reference.symbol -> take address }
  61. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  62. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  63. tmpref:=left.location.reference;
  64. tmpref.refaddr:=addr_seg;
  65. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,tmpref,GetNextReg(location.register));
  66. end;
  67. else
  68. internalerror(2013031501)
  69. end;
  70. end
  71. else
  72. begin
  73. { conversion from a procedure of object/nested procvar to plain procvar }
  74. case left.location.loc of
  75. LOC_REFERENCE,LOC_CREFERENCE:
  76. begin
  77. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  78. { code field is the first one }
  79. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,location.register);
  80. end;
  81. LOC_REGISTER,LOC_CREGISTER:
  82. begin
  83. if target_info.endian=endian_little then
  84. location.register:=left.location.register
  85. else
  86. location.register:=left.location.registerhi;
  87. end;
  88. else
  89. internalerror(2013031502)
  90. end;
  91. end;
  92. end
  93. else
  94. begin
  95. if not tabstractprocdef(left.resultdef).is_addressonly then
  96. location_copy(location,left.location)
  97. else
  98. begin
  99. { assigning a global function to a nested procvar -> create
  100. tmethodpointer record and set the "frame pointer" to nil }
  101. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  102. internalerror(2013031503);
  103. location_reset_ref(location,LOC_REFERENCE,int_cgsize(6),sizeof(pint));
  104. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,resultdef.size,tt_normal,location.reference);
  105. tmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  106. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,tmpreg);
  107. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_16,OS_16,tmpreg,location.reference);
  108. tmpref:=left.location.reference;
  109. tmpref.refaddr:=addr_seg;
  110. inc(location.reference.offset,2);
  111. cg.a_load_ref_ref(current_asmdata.CurrAsmList,OS_16,OS_16,tmpref,location.reference);
  112. { setting the frame pointer to nil is not strictly necessary
  113. since the global procedure won't use it, but it can help with
  114. debugging }
  115. inc(location.reference.offset,2);
  116. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_ADDR,0,location.reference);
  117. dec(location.reference.offset,4);
  118. end;
  119. end;
  120. end;
  121. procedure t8086typeconvnode.second_nil_to_methodprocvar;
  122. begin
  123. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  124. if current_settings.x86memorymodel in x86_far_data_models then
  125. begin
  126. location.registerhi:=cg.getintregister(current_asmdata.currasmlist,OS_32);
  127. cg.a_load_const_reg(current_asmdata.currasmlist,OS_32,0,location.registerhi);
  128. end
  129. else
  130. begin
  131. location.registerhi:=cg.getaddressregister(current_asmdata.currasmlist);
  132. cg.a_load_const_reg(current_asmdata.currasmlist,OS_ADDR,0,location.registerhi);
  133. end;
  134. if (resultdef.typ=procvardef) and (po_far in tprocvardef(resultdef).procoptions) then
  135. begin
  136. location.register:=cg.getintregister(current_asmdata.currasmlist,OS_32);
  137. cg.a_load_const_reg(current_asmdata.currasmlist,OS_32,0,location.register);
  138. end
  139. else
  140. begin
  141. location.register:=cg.getaddressregister(current_asmdata.currasmlist);
  142. cg.a_load_const_reg(current_asmdata.currasmlist,OS_ADDR,0,location.register);
  143. end;
  144. end;
  145. begin
  146. ctypeconvnode:=t8086typeconvnode
  147. end.