n8086inl.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 = class(tx86inlinenode)
  24. function pass_typecheck_cpu: tnode; override;
  25. procedure pass_generate_code_cpu;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,symsym,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. result:=inherited;
  59. end;
  60. end;
  61. procedure ti8086inlinenode.pass_generate_code_cpu;
  62. begin
  63. case inlinenumber of
  64. in_x86_inportl,
  65. in_x86_outportl:
  66. internalerror(2018070302);
  67. else
  68. inherited pass_generate_code_cpu;
  69. end;
  70. end;
  71. function ti8086inlinenode.typecheck_faraddr: tnode;
  72. var
  73. addr_node: tnode;
  74. addr_node_resultdef: tdef;
  75. seg_node: tnode;
  76. begin
  77. addr_node:=caddrnode.create(left);
  78. typecheckpass(addr_node);
  79. addr_node_resultdef:=addr_node.resultdef;
  80. if is_farpointer(addr_node.resultdef) or is_farprocvar(addr_node.resultdef) then
  81. begin
  82. left:=nil;
  83. result:=addr_node;
  84. end
  85. else
  86. begin
  87. seg_node:=geninlinenode(in_seg_x,false,left.getcopy);
  88. inserttypeconv_internal(seg_node,u32inttype);
  89. seg_node:=cshlshrnode.create(shln,seg_node,cordconstnode.create(16,u8inttype,false));
  90. inserttypeconv_internal(addr_node,u32inttype);
  91. left:=nil;
  92. result:=caddnode.create(addn,seg_node,addr_node);
  93. if addr_node_resultdef.typ=pointerdef then
  94. inserttypeconv_internal(result,tcpupointerdef.getreusablex86(tpointerdef(addr_node_resultdef).pointeddef,x86pt_far))
  95. else
  96. inserttypeconv_internal(result,voidfarpointertype);
  97. end;
  98. end;
  99. function ti8086inlinenode.typecheck_seg: tnode;
  100. var
  101. isprocvar,need_conv_to_voidptr: Boolean;
  102. procpointertype: tdef;
  103. hsym: tfieldvarsym;
  104. begin
  105. result := nil;
  106. resultdef:=u16inttype;
  107. { don't allow constants }
  108. if is_constnode(left) then
  109. begin
  110. CGMessagePos(left.fileinfo,type_e_no_addr_of_constant);
  111. exit;
  112. end;
  113. { Handle Seg(proc) special, also Seg(procvar) in tp-mode needs
  114. special handling }
  115. if (left.resultdef.typ=procdef) or
  116. (
  117. { in case of nf_internal, follow the normal FPC semantics so that
  118. we can easily get the actual address of a procvar }
  119. not(nf_internal in flags) and
  120. (left.resultdef.typ=procvardef) and
  121. ((m_tp_procvar in current_settings.modeswitches) or
  122. (m_mac_procvar in current_settings.modeswitches))
  123. ) then
  124. begin
  125. isprocvar:=(left.resultdef.typ=procvardef);
  126. need_conv_to_voidptr:=
  127. (m_tp_procvar in current_settings.modeswitches) or
  128. (m_mac_procvar in current_settings.modeswitches);
  129. if not isprocvar then
  130. begin
  131. if current_settings.x86memorymodel in x86_far_code_models then
  132. begin
  133. left:=ctypeconvnode.create_proc_to_procvar(left);
  134. if need_conv_to_voidptr then
  135. include(ttypeconvnode(left).convnodeflags,tcnf_proc_2_procvar_2_voidpointer);
  136. left.fileinfo:=fileinfo;
  137. typecheckpass(left);
  138. end
  139. else
  140. exit;
  141. end;
  142. { In tp procvar mode for methodpointers we need to load the proc field }
  143. if need_conv_to_voidptr then
  144. begin
  145. if not tabstractprocdef(left.resultdef).is_addressonly then
  146. begin
  147. { For procvars and for nested routines we need to return
  148. the proc field of the methodpointer }
  149. if isprocvar or
  150. is_nested_pd(tabstractprocdef(left.resultdef)) then
  151. begin
  152. if tabstractprocdef(left.resultdef).is_methodpointer then
  153. procpointertype:=methodpointertype
  154. else
  155. procpointertype:=nestedprocpointertype;
  156. { find proc field in methodpointer record }
  157. hsym:=tfieldvarsym(trecorddef(procpointertype).symtable.Find('proc'));
  158. if not assigned(hsym) then
  159. internalerror(2004120404);
  160. { Load tmehodpointer(left).proc }
  161. result:=csubscriptnode.create(
  162. hsym,
  163. ctypeconvnode.create_internal(left,procpointertype));
  164. left:=nil;
  165. end
  166. else
  167. CGMessage(type_e_variable_id_expected);
  168. end;
  169. end;
  170. (* else
  171. begin
  172. { Return the typeconvn only }
  173. result:=left;
  174. left:=nil;
  175. end;*)
  176. end;
  177. end;
  178. function ti8086inlinenode.first_seg: tnode;
  179. begin
  180. expectloc:=LOC_REGISTER;
  181. result:=nil;
  182. end;
  183. procedure ti8086inlinenode.second_seg;
  184. var
  185. segref: treference;
  186. begin
  187. secondpass(left);
  188. if left.resultdef.typ=procvardef then
  189. begin
  190. if left.resultdef.size<>4 then
  191. CGMessage(type_e_seg_procvardef_wrong_memory_model);
  192. case left.location.loc of
  193. LOC_REGISTER,LOC_CREGISTER:
  194. begin
  195. location_reset(location,LOC_REGISTER,OS_16);
  196. location.register:=cg.GetNextReg(left.location.register);
  197. end;
  198. LOC_CREFERENCE,LOC_REFERENCE:
  199. begin
  200. location_reset(location,LOC_REGISTER,OS_16);
  201. segref:=left.location.reference;
  202. inc(segref.offset,2);
  203. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  204. current_asmdata.CurrAsmList.concat(Taicpu.op_ref_reg(A_MOV,S_W,segref,location.register));
  205. end;
  206. else
  207. internalerror(2017121301);
  208. end;
  209. end
  210. else
  211. begin
  212. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  213. internalerror(2013040101);
  214. { if a segment register is specified in ref, we use that }
  215. if left.location.reference.segment<>NR_NO then
  216. begin
  217. location_reset(location,LOC_REGISTER,OS_16);
  218. if is_segment_reg(left.location.reference.segment) then
  219. begin
  220. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  221. current_asmdata.CurrAsmList.Concat(Taicpu.op_reg_reg(A_MOV,S_W,left.location.reference.segment,location.register));
  222. end
  223. else
  224. location.register:=left.location.reference.segment;
  225. end
  226. else
  227. begin
  228. location_reset(location,LOC_REGISTER,OS_16);
  229. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  230. current_asmdata.CurrAsmList.concat(Taicpu.op_reg_reg(A_MOV,S_W,get_default_segment_of_ref(left.location.reference),location.register));
  231. end;
  232. end;
  233. end;
  234. procedure ti8086inlinenode.second_get_frame;
  235. begin
  236. if current_settings.x86memorymodel in x86_far_data_models then
  237. begin
  238. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  239. internalerror(2014030201);
  240. location_reset(location,LOC_REGISTER,OS_32);
  241. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  242. emit_reg_reg(A_MOV,S_W,current_procinfo.framepointer,location.register);
  243. current_asmdata.CurrAsmList.Concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_SS,cg.GetNextReg(location.register)));
  244. end
  245. else
  246. inherited second_get_frame;
  247. end;
  248. function ti8086inlinenode.first_IncDec: tnode;
  249. var
  250. procname:string;
  251. elesize: Tconstexprint;
  252. hp: tnode;
  253. begin
  254. if is_hugepointer(tcallparanode(left).left.resultdef) then
  255. begin
  256. case inlinenumber of
  257. in_inc_x:
  258. procname:='fpc_hugeptr_inc_longint';
  259. in_dec_x:
  260. procname:='fpc_hugeptr_dec_longint';
  261. else
  262. internalerror(2014121001);
  263. end;
  264. if cs_hugeptr_arithmetic_normalization in current_settings.localswitches then
  265. procname:=procname+'_normalized';
  266. if is_void(tpointerdef(tcallparanode(left).left.resultdef).pointeddef) then
  267. elesize:=1
  268. else
  269. elesize:=tpointerdef(tcallparanode(left).left.resultdef).pointeddef.size;
  270. hp := cordconstnode.create(elesize,s32inttype,false);
  271. { extra parameter? }
  272. if assigned(tcallparanode(left).right) then
  273. hp:=caddnode.create(muln,hp,tcallparanode(tcallparanode(left).right).left.getcopy);
  274. result:=ccallnode.createintern(procname,
  275. ccallparanode.create(hp,
  276. ccallparanode.create(tcallparanode(left).left.getcopy,nil)));
  277. typecheckpass(result);
  278. firstpass(result);
  279. end
  280. else
  281. result:=inherited;
  282. end;
  283. procedure ti8086inlinenode.second_incdec;
  284. const
  285. addsubop:array[in_inc_x..in_dec_x] of TOpCG=(OP_ADD,OP_SUB);
  286. var
  287. addvalue : TConstExprInt;
  288. addconstant : boolean;
  289. hregister : tregister;
  290. tmploc: tlocation;
  291. begin
  292. if is_farpointer(tcallparanode(left).left.resultdef) then
  293. begin
  294. { set defaults }
  295. addconstant:=true;
  296. hregister:=NR_NO;
  297. { first secondpass second argument, because if the first arg }
  298. { is used in that expression then SSL may move it to another }
  299. { register }
  300. if assigned(tcallparanode(left).right) then
  301. secondpass(tcallparanode(tcallparanode(left).right).left);
  302. { load first parameter, must be a reference }
  303. secondpass(tcallparanode(left).left);
  304. tmploc:=tcallparanode(left).left.location;
  305. tmploc.size:=OS_S16;
  306. { get addvalue }
  307. case tcallparanode(left).left.resultdef.typ of
  308. pointerdef :
  309. begin
  310. if is_void(tpointerdef(tcallparanode(left).left.resultdef).pointeddef) then
  311. addvalue:=1
  312. else
  313. addvalue:=tpointerdef(tcallparanode(left).left.resultdef).pointeddef.size;
  314. end;
  315. else
  316. internalerror(2020100815);
  317. end;
  318. { second_ argument specified?, must be a s16bit in register }
  319. if assigned(tcallparanode(left).right) then
  320. begin
  321. { when constant, just multiply the addvalue }
  322. if is_constintnode(tcallparanode(tcallparanode(left).right).left) then
  323. addvalue:=addvalue*get_ordinal_value(tcallparanode(tcallparanode(left).right).left)
  324. else if is_constpointernode(tcallparanode(tcallparanode(left).right).left) then
  325. addvalue:=addvalue*tpointerconstnode(tcallparanode(tcallparanode(left).right).left).value
  326. else
  327. begin
  328. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,tcallparanode(tcallparanode(left).right).left.resultdef,s16inttype,addvalue<=1);
  329. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  330. { insert multiply with addvalue if its >1 }
  331. if addvalue>1 then
  332. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,s16inttype,addvalue.svalue,hregister);
  333. addconstant:=false;
  334. end;
  335. end;
  336. { write the add instruction }
  337. if addconstant then
  338. begin
  339. hlcg.a_op_const_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],s16inttype,
  340. smallint(addvalue.svalue),
  341. tmploc);
  342. end
  343. else
  344. begin
  345. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],s16inttype,
  346. hregister,tmploc);
  347. end;
  348. end
  349. else
  350. inherited second_incdec;
  351. end;
  352. procedure ti8086inlinenode.second_abs_long;
  353. var
  354. opsize: TCgSize;
  355. hl: TAsmLabel;
  356. begin
  357. opsize:=def_cgsize(left.resultdef);
  358. if opsize in [OS_64,OS_S64] then
  359. begin
  360. secondpass(left);
  361. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  362. location:=left.location;
  363. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  364. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  365. cg64.a_load64_reg_reg(current_asmdata.CurrAsmList,left.location.register64,location.register64);
  366. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,OS_16,15,cg.GetNextReg(left.location.register64.reghi));
  367. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_16,cg.GetNextReg(left.location.register64.reghi),location.register64.reglo);
  368. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_16,cg.GetNextReg(left.location.register64.reghi),cg.GetNextReg(location.register64.reglo));
  369. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_16,cg.GetNextReg(left.location.register64.reghi),location.register64.reghi);
  370. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_16,cg.GetNextReg(left.location.register64.reghi),cg.GetNextReg(location.register64.reghi));
  371. emit_reg_reg(A_SUB,S_W,cg.GetNextReg(left.location.register64.reghi),location.register64.reglo);
  372. emit_reg_reg(A_SBB,S_W,cg.GetNextReg(left.location.register64.reghi),cg.GetNextReg(location.register64.reglo));
  373. emit_reg_reg(A_SBB,S_W,cg.GetNextReg(left.location.register64.reghi),location.register64.reghi);
  374. emit_reg_reg(A_SBB,S_W,cg.GetNextReg(left.location.register64.reghi),cg.GetNextReg(location.register64.reghi));
  375. if cs_check_overflow in current_settings.localswitches then
  376. begin
  377. current_asmdata.getjumplabel(hl);
  378. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NO,hl);
  379. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  380. cg.a_label(current_asmdata.CurrAsmList,hl);
  381. end;
  382. end
  383. else if opsize in [OS_32,OS_S32] then
  384. begin
  385. secondpass(left);
  386. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  387. location:=left.location;
  388. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  389. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,location.register);
  390. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,OS_16,15,cg.GetNextReg(left.location.register));
  391. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_16,cg.GetNextReg(left.location.register),location.register);
  392. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_16,cg.GetNextReg(left.location.register),cg.GetNextReg(location.register));
  393. emit_reg_reg(A_SUB,S_W,cg.GetNextReg(left.location.register),location.register);
  394. emit_reg_reg(A_SBB,S_W,cg.GetNextReg(left.location.register),cg.GetNextReg(location.register));
  395. if cs_check_overflow in current_settings.localswitches then
  396. begin
  397. current_asmdata.getjumplabel(hl);
  398. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NO,hl);
  399. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  400. cg.a_label(current_asmdata.CurrAsmList,hl);
  401. end;
  402. end
  403. else
  404. inherited second_abs_long;
  405. end;
  406. begin
  407. cinlinenode:=ti8086inlinenode;
  408. end.