n8086cnv.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. { TODO: update for far procs }
  96. if not tabstractprocdef(left.resultdef).is_addressonly then
  97. location_copy(location,left.location)
  98. else
  99. begin
  100. { assigning a global function to a nested procvar -> create
  101. tmethodpointer record and set the "frame pointer" to nil }
  102. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  103. internalerror(2013031503);
  104. location_reset_ref(location,LOC_REFERENCE,int_cgsize(sizeof(pint)*2),sizeof(pint));
  105. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,resultdef.size,tt_normal,location.reference);
  106. tmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  107. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,tmpreg);
  108. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,tmpreg,location.reference);
  109. { setting the frame pointer to nil is not strictly necessary
  110. since the global procedure won't use it, but it can help with
  111. debugging }
  112. inc(location.reference.offset,sizeof(pint));
  113. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_ADDR,0,location.reference);
  114. dec(location.reference.offset,sizeof(pint));
  115. end;
  116. end;
  117. end;
  118. procedure t8086typeconvnode.second_nil_to_methodprocvar;
  119. begin
  120. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  121. if current_settings.x86memorymodel in x86_far_data_models then
  122. begin
  123. location.registerhi:=cg.getintregister(current_asmdata.currasmlist,OS_32);
  124. cg.a_load_const_reg(current_asmdata.currasmlist,OS_32,0,location.registerhi);
  125. end
  126. else
  127. begin
  128. location.registerhi:=cg.getaddressregister(current_asmdata.currasmlist);
  129. cg.a_load_const_reg(current_asmdata.currasmlist,OS_ADDR,0,location.registerhi);
  130. end;
  131. if (resultdef.typ=procvardef) and (po_far in tprocvardef(resultdef).procoptions) then
  132. begin
  133. location.register:=cg.getintregister(current_asmdata.currasmlist,OS_32);
  134. cg.a_load_const_reg(current_asmdata.currasmlist,OS_32,0,location.register);
  135. end
  136. else
  137. begin
  138. location.register:=cg.getaddressregister(current_asmdata.currasmlist);
  139. cg.a_load_const_reg(current_asmdata.currasmlist,OS_ADDR,0,location.register);
  140. end;
  141. end;
  142. begin
  143. ctypeconvnode:=t8086typeconvnode
  144. end.