n8086inl.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 if left.location.reference.base=NR_BP then
  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,NR_SS,location.register));
  100. end
  101. else
  102. begin
  103. location_reset(location,LOC_REGISTER,OS_16);
  104. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  105. current_asmdata.CurrAsmList.Concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_DS,location.register));
  106. end;
  107. end;
  108. procedure ti8086inlinenode.second_get_frame;
  109. begin
  110. if current_settings.x86memorymodel in x86_far_data_models then
  111. begin
  112. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  113. internalerror(2014030201);
  114. location_reset(location,LOC_REGISTER,OS_32);
  115. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  116. emit_reg_reg(A_MOV,S_W,current_procinfo.framepointer,location.register);
  117. current_asmdata.CurrAsmList.Concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_SS,cg.GetNextReg(location.register)));
  118. end
  119. else
  120. inherited second_get_frame;
  121. end;
  122. function ti8086inlinenode.first_IncDec: tnode;
  123. var
  124. procname:string;
  125. elesize: Tconstexprint;
  126. hp: tnode;
  127. begin
  128. if is_hugepointer(tcallparanode(left).left.resultdef) then
  129. begin
  130. case inlinenumber of
  131. in_inc_x:
  132. procname:='fpc_hugeptr_inc_longint';
  133. in_dec_x:
  134. procname:='fpc_hugeptr_dec_longint';
  135. else
  136. internalerror(2014121001);
  137. end;
  138. if cs_hugeptr_arithmetic_normalization in current_settings.localswitches then
  139. procname:=procname+'_normalized';
  140. if is_void(tpointerdef(tcallparanode(left).left.resultdef).pointeddef) then
  141. elesize:=1
  142. else
  143. elesize:=tpointerdef(tcallparanode(left).left.resultdef).pointeddef.size;
  144. hp := cordconstnode.create(elesize,s32inttype,false);
  145. { extra parameter? }
  146. if assigned(tcallparanode(left).right) then
  147. hp:=caddnode.create(muln,hp,tcallparanode(tcallparanode(left).right).left.getcopy);
  148. result:=ccallnode.createintern(procname,
  149. ccallparanode.create(hp,
  150. ccallparanode.create(tcallparanode(left).left.getcopy,nil)));
  151. typecheckpass(result);
  152. firstpass(result);
  153. end
  154. else
  155. result:=inherited;
  156. end;
  157. procedure ti8086inlinenode.second_incdec;
  158. const
  159. addsubop:array[in_inc_x..in_dec_x] of TOpCG=(OP_ADD,OP_SUB);
  160. var
  161. addvalue : TConstExprInt;
  162. addconstant : boolean;
  163. hregister : tregister;
  164. tmploc: tlocation;
  165. begin
  166. if is_farpointer(tcallparanode(left).left.resultdef) then
  167. begin
  168. { set defaults }
  169. addconstant:=true;
  170. hregister:=NR_NO;
  171. { first secondpass second argument, because if the first arg }
  172. { is used in that expression then SSL may move it to another }
  173. { register }
  174. if assigned(tcallparanode(left).right) then
  175. secondpass(tcallparanode(tcallparanode(left).right).left);
  176. { load first parameter, must be a reference }
  177. secondpass(tcallparanode(left).left);
  178. tmploc:=tcallparanode(left).left.location;
  179. tmploc.size:=OS_S16;
  180. { get addvalue }
  181. case tcallparanode(left).left.resultdef.typ of
  182. pointerdef :
  183. begin
  184. if is_void(tpointerdef(tcallparanode(left).left.resultdef).pointeddef) then
  185. addvalue:=1
  186. else
  187. addvalue:=tpointerdef(tcallparanode(left).left.resultdef).pointeddef.size;
  188. end;
  189. else
  190. internalerror(10081);
  191. end;
  192. { second_ argument specified?, must be a s16bit in register }
  193. if assigned(tcallparanode(left).right) then
  194. begin
  195. { when constant, just multiply the addvalue }
  196. if is_constintnode(tcallparanode(tcallparanode(left).right).left) then
  197. addvalue:=addvalue*get_ordinal_value(tcallparanode(tcallparanode(left).right).left)
  198. else if is_constpointernode(tcallparanode(tcallparanode(left).right).left) then
  199. addvalue:=addvalue*tpointerconstnode(tcallparanode(tcallparanode(left).right).left).value
  200. else
  201. begin
  202. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,tcallparanode(tcallparanode(left).right).left.resultdef,s16inttype,addvalue<=1);
  203. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  204. { insert multiply with addvalue if its >1 }
  205. if addvalue>1 then
  206. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,s16inttype,addvalue.svalue,hregister);
  207. addconstant:=false;
  208. end;
  209. end;
  210. { write the add instruction }
  211. if addconstant then
  212. begin
  213. hlcg.a_op_const_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],s16inttype,
  214. smallint(addvalue.svalue),
  215. tmploc);
  216. end
  217. else
  218. begin
  219. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],s16inttype,
  220. hregister,tmploc);
  221. end;
  222. end
  223. else
  224. inherited second_incdec;
  225. end;
  226. procedure ti8086inlinenode.second_abs_long;
  227. var
  228. opsize: TCgSize;
  229. begin
  230. opsize:=def_cgsize(left.resultdef);
  231. if opsize in [OS_64,OS_S64] then
  232. begin
  233. secondpass(left);
  234. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  235. location:=left.location;
  236. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  237. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  238. cg64.a_load64_reg_reg(current_asmdata.CurrAsmList,left.location.register64,location.register64);
  239. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,OS_16,15,cg.GetNextReg(left.location.register64.reghi));
  240. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_16,cg.GetNextReg(left.location.register64.reghi),location.register64.reglo);
  241. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_16,cg.GetNextReg(left.location.register64.reghi),cg.GetNextReg(location.register64.reglo));
  242. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_16,cg.GetNextReg(left.location.register64.reghi),location.register64.reghi);
  243. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_16,cg.GetNextReg(left.location.register64.reghi),cg.GetNextReg(location.register64.reghi));
  244. emit_reg_reg(A_SUB,S_W,cg.GetNextReg(left.location.register64.reghi),location.register64.reglo);
  245. emit_reg_reg(A_SBB,S_W,cg.GetNextReg(left.location.register64.reghi),cg.GetNextReg(location.register64.reglo));
  246. emit_reg_reg(A_SBB,S_W,cg.GetNextReg(left.location.register64.reghi),location.register64.reghi);
  247. emit_reg_reg(A_SBB,S_W,cg.GetNextReg(left.location.register64.reghi),cg.GetNextReg(location.register64.reghi));
  248. end
  249. else if opsize in [OS_32,OS_S32] then
  250. begin
  251. secondpass(left);
  252. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  253. location:=left.location;
  254. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  255. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,location.register);
  256. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,OS_16,15,cg.GetNextReg(left.location.register));
  257. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_16,cg.GetNextReg(left.location.register),location.register);
  258. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_16,cg.GetNextReg(left.location.register),cg.GetNextReg(location.register));
  259. emit_reg_reg(A_SUB,S_W,cg.GetNextReg(left.location.register),location.register);
  260. emit_reg_reg(A_SBB,S_W,cg.GetNextReg(left.location.register),cg.GetNextReg(location.register));
  261. end
  262. else
  263. inherited second_abs_long;
  264. end;
  265. begin
  266. cinlinenode:=ti8086inlinenode;
  267. end.