n8086inl.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. function first_IncDec: tnode;override;
  30. procedure second_incdec;override;
  31. end;
  32. implementation
  33. uses
  34. ninl,
  35. systems,
  36. globtype,globals,
  37. cutils,verbose,
  38. constexp,
  39. symconst,
  40. defutil,
  41. aasmbase,aasmtai,aasmdata,aasmcpu,
  42. symtype,symdef,symcpu,
  43. cgbase,pass_1,pass_2,
  44. cpuinfo,cpubase,paramgr,
  45. nbas,nadd,ncon,ncal,ncnv,nld,ncgutil,
  46. tgobj,
  47. cga,cgutils,cgx86,cgobj,hlcgobj,
  48. htypechk,procinfo;
  49. function ti8086inlinenode.typecheck_seg: tnode;
  50. begin
  51. result := nil;
  52. resultdef:=u16inttype;
  53. { don't allow constants }
  54. if is_constnode(left) then
  55. begin
  56. CGMessagePos(left.fileinfo,type_e_no_addr_of_constant);
  57. exit;
  58. end;
  59. end;
  60. function ti8086inlinenode.first_seg: tnode;
  61. begin
  62. expectloc:=LOC_REGISTER;
  63. result:=nil;
  64. end;
  65. procedure ti8086inlinenode.second_seg;
  66. var
  67. segref: treference;
  68. begin
  69. secondpass(left);
  70. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  71. internalerror(2013040101);
  72. { if a segment register is specified in ref, we use that }
  73. if left.location.reference.segment<>NR_NO then
  74. begin
  75. location_reset(location,LOC_REGISTER,OS_16);
  76. if is_segment_reg(left.location.reference.segment) then
  77. begin
  78. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  79. current_asmdata.CurrAsmList.Concat(Taicpu.op_reg_reg(A_MOV,S_W,left.location.reference.segment,location.register));
  80. end
  81. else
  82. location.register:=left.location.reference.segment;
  83. end
  84. { references relative to a symbol use the segment of the symbol,
  85. which can be obtained by the SEG directive }
  86. else if assigned(left.location.reference.symbol) then
  87. begin
  88. location_reset(location,LOC_REGISTER,OS_16);
  89. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  90. reference_reset_symbol(segref,left.location.reference.symbol,0,left.location.reference.alignment,left.location.reference.volatility);
  91. segref.refaddr:=addr_seg;
  92. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,segref,location.register);
  93. end
  94. else if left.location.reference.base=NR_BP then
  95. begin
  96. location_reset(location,LOC_REGISTER,OS_16);
  97. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  98. current_asmdata.CurrAsmList.concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_SS,location.register));
  99. end
  100. else
  101. begin
  102. location_reset(location,LOC_REGISTER,OS_16);
  103. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  104. current_asmdata.CurrAsmList.Concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_DS,location.register));
  105. end;
  106. end;
  107. procedure ti8086inlinenode.second_get_frame;
  108. begin
  109. if current_settings.x86memorymodel in x86_far_data_models then
  110. begin
  111. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  112. internalerror(2014030201);
  113. location_reset(location,LOC_REGISTER,OS_32);
  114. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  115. emit_reg_reg(A_MOV,S_W,current_procinfo.framepointer,location.register);
  116. current_asmdata.CurrAsmList.Concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_SS,GetNextReg(location.register)));
  117. end
  118. else
  119. inherited second_get_frame;
  120. end;
  121. function ti8086inlinenode.first_IncDec: tnode;
  122. var
  123. procname:string;
  124. elesize: Tconstexprint;
  125. hp: tnode;
  126. begin
  127. if is_hugepointer(tcallparanode(left).left.resultdef) then
  128. begin
  129. case inlinenumber of
  130. in_inc_x:
  131. procname:='fpc_hugeptr_inc_longint';
  132. in_dec_x:
  133. procname:='fpc_hugeptr_dec_longint';
  134. else
  135. internalerror(2014121001);
  136. end;
  137. if cs_hugeptr_arithmetic_normalization in current_settings.localswitches then
  138. procname:=procname+'_normalized';
  139. if is_void(tpointerdef(tcallparanode(left).left.resultdef).pointeddef) then
  140. elesize:=1
  141. else
  142. elesize:=tpointerdef(tcallparanode(left).left.resultdef).pointeddef.size;
  143. hp := cordconstnode.create(elesize,s32inttype,false);
  144. { extra parameter? }
  145. if assigned(tcallparanode(left).right) then
  146. hp:=caddnode.create(muln,hp,tcallparanode(tcallparanode(left).right).left.getcopy);
  147. result:=ccallnode.createintern(procname,
  148. ccallparanode.create(hp,
  149. ccallparanode.create(tcallparanode(left).left.getcopy,nil)));
  150. typecheckpass(result);
  151. firstpass(result);
  152. end
  153. else
  154. result:=inherited;
  155. end;
  156. procedure ti8086inlinenode.second_incdec;
  157. const
  158. addsubop:array[in_inc_x..in_dec_x] of TOpCG=(OP_ADD,OP_SUB);
  159. var
  160. addvalue : TConstExprInt;
  161. addconstant : boolean;
  162. hregister : tregister;
  163. tmploc: tlocation;
  164. begin
  165. if is_farpointer(tcallparanode(left).left.resultdef) then
  166. begin
  167. { set defaults }
  168. addconstant:=true;
  169. hregister:=NR_NO;
  170. { first secondpass second argument, because if the first arg }
  171. { is used in that expression then SSL may move it to another }
  172. { register }
  173. if assigned(tcallparanode(left).right) then
  174. secondpass(tcallparanode(tcallparanode(left).right).left);
  175. { load first parameter, must be a reference }
  176. secondpass(tcallparanode(left).left);
  177. tmploc:=tcallparanode(left).left.location;
  178. tmploc.size:=OS_S16;
  179. { get addvalue }
  180. case tcallparanode(left).left.resultdef.typ of
  181. pointerdef :
  182. begin
  183. if is_void(tpointerdef(tcallparanode(left).left.resultdef).pointeddef) then
  184. addvalue:=1
  185. else
  186. addvalue:=tpointerdef(tcallparanode(left).left.resultdef).pointeddef.size;
  187. end;
  188. else
  189. internalerror(10081);
  190. end;
  191. { second_ argument specified?, must be a s16bit in register }
  192. if assigned(tcallparanode(left).right) then
  193. begin
  194. { when constant, just multiply the addvalue }
  195. if is_constintnode(tcallparanode(tcallparanode(left).right).left) then
  196. addvalue:=addvalue*get_ordinal_value(tcallparanode(tcallparanode(left).right).left)
  197. else if is_constpointernode(tcallparanode(tcallparanode(left).right).left) then
  198. addvalue:=addvalue*tpointerconstnode(tcallparanode(tcallparanode(left).right).left).value
  199. else
  200. begin
  201. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,tcallparanode(tcallparanode(left).right).left.resultdef,s16inttype,addvalue<=1);
  202. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  203. { insert multiply with addvalue if its >1 }
  204. if addvalue>1 then
  205. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,s16inttype,addvalue.svalue,hregister);
  206. addconstant:=false;
  207. end;
  208. end;
  209. { write the add instruction }
  210. if addconstant then
  211. begin
  212. hlcg.a_op_const_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],s16inttype,
  213. smallint(addvalue.svalue),
  214. tmploc);
  215. end
  216. else
  217. begin
  218. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],s16inttype,
  219. hregister,tmploc);
  220. end;
  221. end
  222. else
  223. inherited second_incdec;
  224. end;
  225. begin
  226. cinlinenode:=ti8086inlinenode;
  227. end.