n8086inl.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate i8086 inline 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 n8086inl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. nx86inl,node;
  22. type
  23. { ti8086inlinenode }
  24. ti8086inlinenode = class(tx86inlinenode)
  25. function typecheck_seg: tnode; override;
  26. function first_seg: tnode; override;
  27. procedure second_seg; override;
  28. procedure second_get_frame;override;
  29. procedure second_incdec;override;
  30. end;
  31. implementation
  32. uses
  33. ninl,
  34. systems,
  35. globtype,globals,
  36. cutils,verbose,
  37. constexp,
  38. symconst,
  39. defutil,
  40. aasmbase,aasmtai,aasmdata,aasmcpu,
  41. symtype,symdef,symcpu,
  42. cgbase,pass_2,
  43. cpuinfo,cpubase,paramgr,
  44. nbas,ncon,ncal,ncnv,nld,ncgutil,
  45. tgobj,
  46. cga,cgutils,cgx86,cgobj,hlcgobj,
  47. htypechk,procinfo;
  48. function ti8086inlinenode.typecheck_seg: tnode;
  49. begin
  50. result := nil;
  51. resultdef:=u16inttype;
  52. { don't allow constants }
  53. if is_constnode(left) then
  54. begin
  55. CGMessagePos(left.fileinfo,type_e_no_addr_of_constant);
  56. exit;
  57. end;
  58. end;
  59. function ti8086inlinenode.first_seg: tnode;
  60. begin
  61. expectloc:=LOC_REGISTER;
  62. result:=nil;
  63. end;
  64. procedure ti8086inlinenode.second_seg;
  65. var
  66. segref: treference;
  67. begin
  68. secondpass(left);
  69. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  70. internalerror(2013040101);
  71. { if a segment register is specified in ref, we use that }
  72. if left.location.reference.segment<>NR_NO then
  73. begin
  74. location_reset(location,LOC_REGISTER,OS_16);
  75. if is_segment_reg(left.location.reference.segment) then
  76. begin
  77. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  78. current_asmdata.CurrAsmList.Concat(Taicpu.op_reg_reg(A_MOV,S_W,left.location.reference.segment,location.register));
  79. end
  80. else
  81. location.register:=left.location.reference.segment;
  82. end
  83. { references relative to a symbol use the segment of the symbol,
  84. which can be obtained by the SEG directive }
  85. else if assigned(left.location.reference.symbol) then
  86. begin
  87. location_reset(location,LOC_REGISTER,OS_16);
  88. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  89. reference_reset_symbol(segref,left.location.reference.symbol,0,0);
  90. segref.refaddr:=addr_seg;
  91. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,segref,location.register);
  92. end
  93. else if left.location.reference.base=NR_BP then
  94. begin
  95. location_reset(location,LOC_REGISTER,OS_16);
  96. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  97. current_asmdata.CurrAsmList.concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_SS,location.register));
  98. end
  99. else
  100. begin
  101. location_reset(location,LOC_REGISTER,OS_16);
  102. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  103. current_asmdata.CurrAsmList.Concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_DS,location.register));
  104. end;
  105. end;
  106. procedure ti8086inlinenode.second_get_frame;
  107. begin
  108. if current_settings.x86memorymodel in x86_far_data_models then
  109. begin
  110. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  111. internalerror(2014030201);
  112. location_reset(location,LOC_REGISTER,OS_32);
  113. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  114. emit_reg_reg(A_MOV,S_W,current_procinfo.framepointer,location.register);
  115. current_asmdata.CurrAsmList.Concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_SS,GetNextReg(location.register)));
  116. end
  117. else
  118. inherited second_get_frame;
  119. end;
  120. procedure ti8086inlinenode.second_incdec;
  121. const
  122. addsubop:array[in_inc_x..in_dec_x] of TOpCG=(OP_ADD,OP_SUB);
  123. var
  124. addvalue : TConstExprInt;
  125. addconstant : boolean;
  126. hregister : tregister;
  127. tmploc: tlocation;
  128. begin
  129. if is_farpointer(tcallparanode(left).left.resultdef) then
  130. begin
  131. { set defaults }
  132. addconstant:=true;
  133. hregister:=NR_NO;
  134. { first secondpass second argument, because if the first arg }
  135. { is used in that expression then SSL may move it to another }
  136. { register }
  137. if assigned(tcallparanode(left).right) then
  138. secondpass(tcallparanode(tcallparanode(left).right).left);
  139. { load first parameter, must be a reference }
  140. secondpass(tcallparanode(left).left);
  141. tmploc:=tcallparanode(left).left.location;
  142. tmploc.size:=OS_S16;
  143. { get addvalue }
  144. case tcallparanode(left).left.resultdef.typ of
  145. pointerdef :
  146. begin
  147. if is_void(tpointerdef(tcallparanode(left).left.resultdef).pointeddef) then
  148. addvalue:=1
  149. else
  150. addvalue:=tpointerdef(tcallparanode(left).left.resultdef).pointeddef.size;
  151. end;
  152. else
  153. internalerror(10081);
  154. end;
  155. { second_ argument specified?, must be a s16bit in register }
  156. if assigned(tcallparanode(left).right) then
  157. begin
  158. { when constant, just multiply the addvalue }
  159. if is_constintnode(tcallparanode(tcallparanode(left).right).left) then
  160. addvalue:=addvalue*get_ordinal_value(tcallparanode(tcallparanode(left).right).left)
  161. else if is_constpointernode(tcallparanode(tcallparanode(left).right).left) then
  162. addvalue:=addvalue*tpointerconstnode(tcallparanode(tcallparanode(left).right).left).value
  163. else
  164. begin
  165. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,tcallparanode(tcallparanode(left).right).left.resultdef,s16inttype,addvalue<=1);
  166. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  167. { insert multiply with addvalue if its >1 }
  168. if addvalue>1 then
  169. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,s16inttype,addvalue.svalue,hregister);
  170. addconstant:=false;
  171. end;
  172. end;
  173. { write the add instruction }
  174. if addconstant then
  175. begin
  176. hlcg.a_op_const_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],s16inttype,
  177. smallint(addvalue.svalue),
  178. tmploc);
  179. end
  180. else
  181. begin
  182. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],s16inttype,
  183. hregister,tmploc);
  184. end;
  185. end
  186. else
  187. inherited second_incdec;
  188. end;
  189. begin
  190. cinlinenode:=ti8086inlinenode;
  191. end.