n8086inl.pas 14 KB

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