n8086inl.pas 12 KB

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