cg386inl.pas 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 inline nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cg386inl;
  19. interface
  20. uses
  21. tree;
  22. procedure secondinline(var p : ptree);
  23. implementation
  24. uses
  25. globtype,systems,
  26. cobjects,verbose,globals,files,
  27. symconst,symtable,aasm,types,
  28. hcodegen,temp_gen,pass_1,pass_2,
  29. cpubase,cpuasm,
  30. cgai386,tgeni386,cg386cal;
  31. {*****************************************************************************
  32. Helpers
  33. *****************************************************************************}
  34. { reverts the parameter list }
  35. var nb_para : integer;
  36. function reversparameter(p : ptree) : ptree;
  37. var
  38. hp1,hp2 : ptree;
  39. begin
  40. hp1:=nil;
  41. nb_para := 0;
  42. while assigned(p) do
  43. begin
  44. { pull out }
  45. hp2:=p;
  46. p:=p^.right;
  47. inc(nb_para);
  48. { pull in }
  49. hp2^.right:=hp1;
  50. hp1:=hp2;
  51. end;
  52. reversparameter:=hp1;
  53. end;
  54. {*****************************************************************************
  55. SecondInLine
  56. *****************************************************************************}
  57. procedure StoreDirectFuncResult(var dest:ptree);
  58. var
  59. hp : ptree;
  60. hdef : porddef;
  61. hreg : tregister;
  62. hregister : tregister;
  63. oldregisterdef : boolean;
  64. begin
  65. { Get the accumulator first so it can't be used in the dest }
  66. if (dest^.resulttype^.deftype=orddef) and
  67. not(is_64bitint(dest^.resulttype)) then
  68. hregister:=getexplicitregister32(accumulator);
  69. { process dest }
  70. SecondPass(dest);
  71. if Codegenerror then
  72. exit;
  73. { store the value }
  74. Case dest^.resulttype^.deftype of
  75. floatdef:
  76. floatstore(PFloatDef(dest^.resulttype)^.typ,dest^.location.reference);
  77. orddef:
  78. begin
  79. if is_64bitint(dest^.resulttype) then
  80. begin
  81. emit_movq_reg_loc(R_EDX,R_EAX,dest^.location);
  82. end
  83. else
  84. begin
  85. Case dest^.resulttype^.size of
  86. 1 : hreg:=regtoreg8(hregister);
  87. 2 : hreg:=regtoreg16(hregister);
  88. 4 : hreg:=hregister;
  89. End;
  90. emit_mov_reg_loc(hreg,dest^.location);
  91. If (cs_check_range in aktlocalswitches) and
  92. {no need to rangecheck longints or cardinals on 32bit processors}
  93. not((porddef(dest^.resulttype)^.typ = s32bit) and
  94. (porddef(dest^.resulttype)^.low = $80000000) and
  95. (porddef(dest^.resulttype)^.high = $7fffffff)) and
  96. not((porddef(dest^.resulttype)^.typ = u32bit) and
  97. (porddef(dest^.resulttype)^.low = 0) and
  98. (porddef(dest^.resulttype)^.high = $ffffffff)) then
  99. Begin
  100. {do not register this temporary def}
  101. OldRegisterDef := RegisterDef;
  102. RegisterDef := False;
  103. hdef:=nil;
  104. Case PordDef(dest^.resulttype)^.typ of
  105. u8bit,u16bit,u32bit:
  106. begin
  107. new(hdef,init(u32bit,0,$ffffffff));
  108. hreg:=hregister;
  109. end;
  110. s8bit,s16bit,s32bit:
  111. begin
  112. new(hdef,init(s32bit,$80000000,$7fffffff));
  113. hreg:=hregister;
  114. end;
  115. end;
  116. { create a fake node }
  117. hp := genzeronode(nothingn);
  118. hp^.location.loc := LOC_REGISTER;
  119. hp^.location.register := hreg;
  120. if assigned(hdef) then
  121. hp^.resulttype:=hdef
  122. else
  123. hp^.resulttype:=dest^.resulttype;
  124. { emit the range check }
  125. emitrangecheck(hp,dest^.resulttype);
  126. hp^.right := nil;
  127. if assigned(hdef) then
  128. Dispose(hdef, Done);
  129. RegisterDef := OldRegisterDef;
  130. disposetree(hp);
  131. End;
  132. ungetregister(hregister);
  133. end;
  134. End;
  135. else
  136. internalerror(66766766);
  137. end;
  138. { free used registers }
  139. del_locref(dest^.location);
  140. end;
  141. procedure secondinline(var p : ptree);
  142. const
  143. {tfloattype = (s32real,s64real,s80real,s64bit,f16bit,f32bit);}
  144. { float_name: array[tfloattype] of string[8]=
  145. ('S32REAL','S64REAL','S80REAL','S64BIT','F16BIT','F32BIT'); }
  146. incdecop:array[in_inc_x..in_dec_x] of tasmop=(A_INC,A_DEC);
  147. addsubop:array[in_inc_x..in_dec_x] of tasmop=(A_ADD,A_SUB);
  148. var
  149. aktfile : treference;
  150. ft : tfiletype;
  151. opsize : topsize;
  152. op,
  153. asmop : tasmop;
  154. pushed : tpushed;
  155. {inc/dec}
  156. addconstant : boolean;
  157. addvalue : longint;
  158. procedure handlereadwrite(doread,doln : boolean);
  159. { produces code for READ(LN) and WRITE(LN) }
  160. procedure loadstream;
  161. const
  162. io:array[boolean] of string[7]=('_OUTPUT','_INPUT');
  163. var
  164. r : preference;
  165. begin
  166. new(r);
  167. reset_reference(r^);
  168. r^.symbol:=newasmsymbol('U_'+upper(target_info.system_unit)+io[doread]);
  169. emit_ref_reg(A_LEA,S_L,r,R_EDI)
  170. end;
  171. const
  172. rdwrprefix:array[boolean] of string[15]=('FPC_WRITE_TEXT_','FPC_READ_TEXT_');
  173. var
  174. node,hp : ptree;
  175. typedtyp,
  176. pararesult : pdef;
  177. orgfloattype : tfloattype;
  178. has_length : boolean;
  179. dummycoll : tparaitem;
  180. iolabel : pasmlabel;
  181. npara : longint;
  182. esireloaded : boolean;
  183. begin
  184. { here we don't use register calling conventions }
  185. dummycoll.init;
  186. dummycoll.register:=R_NO;
  187. { I/O check }
  188. if (cs_check_io in aktlocalswitches) and
  189. not(po_iocheck in aktprocsym^.definition^.procoptions) then
  190. begin
  191. getlabel(iolabel);
  192. emitlab(iolabel);
  193. end
  194. else
  195. iolabel:=nil;
  196. { for write of real with the length specified }
  197. has_length:=false;
  198. hp:=nil;
  199. { reserve temporary pointer to data variable }
  200. aktfile.symbol:=nil;
  201. gettempofsizereference(4,aktfile);
  202. { first state text data }
  203. ft:=ft_text;
  204. { and state a parameter ? }
  205. if p^.left=nil then
  206. begin
  207. { the following instructions are for "writeln;" }
  208. loadstream;
  209. { save @aktfile in temporary variable }
  210. emit_reg_ref(A_MOV,S_L,R_EDI,newreference(aktfile));
  211. end
  212. else
  213. begin
  214. { revers paramters }
  215. node:=reversparameter(p^.left);
  216. p^.left := node;
  217. npara := nb_para;
  218. { calculate data variable }
  219. { is first parameter a file type ? }
  220. if node^.left^.resulttype^.deftype=filedef then
  221. begin
  222. ft:=pfiledef(node^.left^.resulttype)^.filetype;
  223. if ft=ft_typed then
  224. typedtyp:=pfiledef(node^.left^.resulttype)^.typed_as;
  225. secondpass(node^.left);
  226. if codegenerror then
  227. exit;
  228. { save reference in temporary variables }
  229. if node^.left^.location.loc<>LOC_REFERENCE then
  230. begin
  231. CGMessage(cg_e_illegal_expression);
  232. exit;
  233. end;
  234. emit_ref_reg(A_LEA,S_L,newreference(node^.left^.location.reference),R_EDI);
  235. { skip to the next parameter }
  236. node:=node^.right;
  237. end
  238. else
  239. begin
  240. { load stdin/stdout stream }
  241. loadstream;
  242. end;
  243. { save @aktfile in temporary variable }
  244. emit_reg_ref(A_MOV,S_L,R_EDI,newreference(aktfile));
  245. if doread then
  246. { parameter by READ gives call by reference }
  247. dummycoll.paratyp:=vs_var
  248. { an WRITE Call by "Const" }
  249. else
  250. dummycoll.paratyp:=vs_const;
  251. { because of secondcallparan, which otherwise attaches }
  252. if ft=ft_typed then
  253. { this is to avoid copy of simple const parameters }
  254. {dummycoll.data:=new(pformaldef,init)}
  255. dummycoll.data:=cformaldef
  256. else
  257. { I think, this isn't a good solution (FK) }
  258. dummycoll.data:=nil;
  259. while assigned(node) do
  260. begin
  261. esireloaded:=false;
  262. pushusedregisters(pushed,$ff);
  263. hp:=node;
  264. node:=node^.right;
  265. hp^.right:=nil;
  266. if hp^.is_colon_para then
  267. CGMessage(parser_e_illegal_colon_qualifier);
  268. { when float is written then we need bestreal to be pushed
  269. convert here else we loose the old flaot type }
  270. if (not doread) and
  271. (ft<>ft_typed) and
  272. (hp^.left^.resulttype^.deftype=floatdef) then
  273. begin
  274. orgfloattype:=pfloatdef(hp^.left^.resulttype)^.typ;
  275. hp^.left:=gentypeconvnode(hp^.left,bestrealdef^);
  276. firstpass(hp^.left);
  277. end;
  278. { when read ord,floats are functions, so they need this
  279. parameter as their destination instead of being pushed }
  280. if doread and
  281. (ft<>ft_typed) and
  282. (hp^.resulttype^.deftype in [orddef,floatdef]) then
  283. begin
  284. end
  285. else
  286. begin
  287. if ft=ft_typed then
  288. never_copy_const_param:=true;
  289. { reset data type }
  290. dummycoll.data:=nil;
  291. { create temporary defs for high tree generation }
  292. if doread and (is_shortstring(hp^.resulttype)) then
  293. dummycoll.data:=openshortstringdef
  294. else
  295. if (is_chararray(hp^.resulttype)) then
  296. dummycoll.data:=openchararraydef;
  297. secondcallparan(hp,@dummycoll,false,false,false,0);
  298. if ft=ft_typed then
  299. never_copy_const_param:=false;
  300. end;
  301. hp^.right:=node;
  302. if codegenerror then
  303. exit;
  304. emit_push_mem(aktfile);
  305. if (ft=ft_typed) then
  306. begin
  307. { OK let's try this }
  308. { first we must only allow the right type }
  309. { we have to call blockread or blockwrite }
  310. { but the real problem is that }
  311. { reset and rewrite should have set }
  312. { the type size }
  313. { as recordsize for that file !!!! }
  314. { how can we make that }
  315. { I think that is only possible by adding }
  316. { reset and rewrite to the inline list a call }
  317. { allways read only one record by element }
  318. push_int(typedtyp^.size);
  319. if doread then
  320. emitcall('FPC_TYPED_READ')
  321. else
  322. emitcall('FPC_TYPED_WRITE');
  323. end
  324. else
  325. begin
  326. { save current position }
  327. pararesult:=hp^.left^.resulttype;
  328. { handle possible field width }
  329. { of course only for write(ln) }
  330. if not doread then
  331. begin
  332. { handle total width parameter }
  333. if assigned(node) and node^.is_colon_para then
  334. begin
  335. hp:=node;
  336. node:=node^.right;
  337. hp^.right:=nil;
  338. secondcallparan(hp,@dummycoll,false,false,false,0);
  339. hp^.right:=node;
  340. if codegenerror then
  341. exit;
  342. has_length:=true;
  343. end
  344. else
  345. if pararesult^.deftype<>floatdef then
  346. push_int(0)
  347. else
  348. push_int(-32767);
  349. { a second colon para for a float ? }
  350. if assigned(node) and node^.is_colon_para then
  351. begin
  352. hp:=node;
  353. node:=node^.right;
  354. hp^.right:=nil;
  355. secondcallparan(hp,@dummycoll,false,false,false,0);
  356. hp^.right:=node;
  357. if pararesult^.deftype<>floatdef then
  358. CGMessage(parser_e_illegal_colon_qualifier);
  359. if codegenerror then
  360. exit;
  361. end
  362. else
  363. begin
  364. if pararesult^.deftype=floatdef then
  365. push_int(-1);
  366. end;
  367. { push also the real type for floats }
  368. if pararesult^.deftype=floatdef then
  369. push_int(ord(orgfloattype));
  370. end;
  371. case pararesult^.deftype of
  372. stringdef :
  373. begin
  374. emitcall(rdwrprefix[doread]+pstringdef(pararesult)^.stringtypname);
  375. end;
  376. pointerdef :
  377. begin
  378. if is_pchar(pararesult) then
  379. emitcall(rdwrprefix[doread]+'PCHAR_AS_POINTER')
  380. end;
  381. arraydef :
  382. begin
  383. if is_chararray(pararesult) then
  384. emitcall(rdwrprefix[doread]+'PCHAR_AS_ARRAY')
  385. end;
  386. floatdef :
  387. begin
  388. emitcall(rdwrprefix[doread]+'FLOAT');
  389. {
  390. if pfloatdef(p^.resulttype)^.typ<>f32bit then
  391. dec(fpuvaroffset);
  392. }
  393. if doread then
  394. begin
  395. maybe_loadesi;
  396. esireloaded:=true;
  397. StoreDirectFuncResult(hp^.left);
  398. end;
  399. end;
  400. orddef :
  401. begin
  402. case porddef(pararesult)^.typ of
  403. s8bit,s16bit,s32bit :
  404. emitcall(rdwrprefix[doread]+'SINT');
  405. u8bit,u16bit,u32bit :
  406. emitcall(rdwrprefix[doread]+'UINT');
  407. uchar :
  408. emitcall(rdwrprefix[doread]+'CHAR');
  409. s64bit :
  410. emitcall(rdwrprefix[doread]+'INT64');
  411. u64bit :
  412. emitcall(rdwrprefix[doread]+'QWORD');
  413. bool8bit,
  414. bool16bit,
  415. bool32bit :
  416. emitcall(rdwrprefix[doread]+'BOOLEAN');
  417. end;
  418. if doread then
  419. begin
  420. maybe_loadesi;
  421. esireloaded:=true;
  422. StoreDirectFuncResult(hp^.left);
  423. end;
  424. end;
  425. end;
  426. end;
  427. { load ESI in methods again }
  428. popusedregisters(pushed);
  429. if not(esireloaded) then
  430. maybe_loadesi;
  431. end;
  432. end;
  433. { Insert end of writing for textfiles }
  434. if ft=ft_text then
  435. begin
  436. pushusedregisters(pushed,$ff);
  437. emit_push_mem(aktfile);
  438. if doread then
  439. begin
  440. if doln then
  441. emitcall('FPC_READLN_END')
  442. else
  443. emitcall('FPC_READ_END');
  444. end
  445. else
  446. begin
  447. if doln then
  448. emitcall('FPC_WRITELN_END')
  449. else
  450. emitcall('FPC_WRITE_END');
  451. end;
  452. popusedregisters(pushed);
  453. maybe_loadesi;
  454. end;
  455. { Insert IOCheck if set }
  456. if assigned(iolabel) then
  457. begin
  458. { registers are saved in the procedure }
  459. emit_sym(A_PUSH,S_L,iolabel);
  460. emitcall('FPC_IOCHECK');
  461. end;
  462. { Freeup all used temps }
  463. ungetiftemp(aktfile);
  464. if assigned(p^.left) then
  465. begin
  466. p^.left:=reversparameter(p^.left);
  467. if npara<>nb_para then
  468. CGMessage(cg_f_internal_error_in_secondinline);
  469. hp:=p^.left;
  470. while assigned(hp) do
  471. begin
  472. if assigned(hp^.left) then
  473. if (hp^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  474. ungetiftemp(hp^.left^.location.reference);
  475. hp:=hp^.right;
  476. end;
  477. end;
  478. end;
  479. procedure handle_str;
  480. var
  481. hp,node : ptree;
  482. dummycoll : tparaitem;
  483. is_real,has_length : boolean;
  484. realtype : tfloattype;
  485. procedureprefix : string;
  486. begin
  487. dummycoll.init;
  488. dummycoll.register:=R_NO;
  489. pushusedregisters(pushed,$ff);
  490. node:=p^.left;
  491. is_real:=false;
  492. has_length:=false;
  493. while assigned(node^.right) do node:=node^.right;
  494. { if a real parameter somewhere then call REALSTR }
  495. if (node^.left^.resulttype^.deftype=floatdef) then
  496. begin
  497. is_real:=true;
  498. realtype:=pfloatdef(node^.left^.resulttype)^.typ;
  499. end;
  500. node:=p^.left;
  501. { we have at least two args }
  502. { with at max 2 colon_para in between }
  503. { string arg }
  504. hp:=node;
  505. node:=node^.right;
  506. hp^.right:=nil;
  507. dummycoll.paratyp:=vs_var;
  508. if is_shortstring(hp^.resulttype) then
  509. dummycoll.data:=openshortstringdef
  510. else
  511. dummycoll.data:=hp^.resulttype;
  512. procedureprefix:='FPC_'+pstringdef(hp^.resulttype)^.stringtypname+'_';
  513. secondcallparan(hp,@dummycoll,false,false,false,0);
  514. if codegenerror then
  515. exit;
  516. dummycoll.paratyp:=vs_const;
  517. disposetree(p^.left);
  518. p^.left:=nil;
  519. { second arg }
  520. hp:=node;
  521. node:=node^.right;
  522. hp^.right:=nil;
  523. { if real push real type }
  524. if is_real then
  525. push_int(ord(realtype));
  526. { frac para }
  527. if hp^.is_colon_para and assigned(node) and
  528. node^.is_colon_para then
  529. begin
  530. dummycoll.data:=hp^.resulttype;
  531. secondcallparan(hp,@dummycoll,false
  532. ,false,false,0
  533. );
  534. if codegenerror then
  535. exit;
  536. disposetree(hp);
  537. hp:=node;
  538. node:=node^.right;
  539. hp^.right:=nil;
  540. has_length:=true;
  541. end
  542. else
  543. if is_real then
  544. push_int(-1);
  545. { third arg, length only if is_real }
  546. if hp^.is_colon_para then
  547. begin
  548. dummycoll.data:=hp^.resulttype;
  549. secondcallparan(hp,@dummycoll,false
  550. ,false,false,0
  551. );
  552. if codegenerror then
  553. exit;
  554. disposetree(hp);
  555. hp:=node;
  556. node:=node^.right;
  557. hp^.right:=nil;
  558. end
  559. else
  560. if is_real then
  561. push_int(-32767)
  562. else
  563. push_int(-1);
  564. { Convert float to bestreal }
  565. if is_real then
  566. begin
  567. hp^.left:=gentypeconvnode(hp^.left,bestrealdef^);
  568. firstpass(hp^.left);
  569. end;
  570. { last arg longint or real }
  571. secondcallparan(hp,@dummycoll,false
  572. ,false,false,0
  573. );
  574. if codegenerror then
  575. exit;
  576. if is_real then
  577. emitcall(procedureprefix+'FLOAT')
  578. else
  579. case porddef(hp^.resulttype)^.typ of
  580. u32bit:
  581. emitcall(procedureprefix+'CARDINAL');
  582. u64bit:
  583. emitcall(procedureprefix+'QWORD');
  584. s64bit:
  585. emitcall(procedureprefix+'INT64');
  586. else
  587. emitcall(procedureprefix+'LONGINT');
  588. end;
  589. disposetree(hp);
  590. popusedregisters(pushed);
  591. end;
  592. Procedure Handle_Val;
  593. var
  594. hp,node, code_para, dest_para : ptree;
  595. hreg,hreg2: TRegister;
  596. hdef: POrdDef;
  597. procedureprefix : string;
  598. hr, hr2: TReference;
  599. dummycoll : tparaitem;
  600. has_code, has_32bit_code, oldregisterdef: boolean;
  601. r : preference;
  602. begin
  603. dummycoll.init;
  604. dummycoll.register:=R_NO;
  605. node:=p^.left;
  606. hp:=node;
  607. node:=node^.right;
  608. hp^.right:=nil;
  609. {if we have 3 parameters, we have a code parameter}
  610. has_code := Assigned(node^.right);
  611. has_32bit_code := false;
  612. reset_reference(hr);
  613. hreg := R_NO;
  614. If has_code then
  615. Begin
  616. {code is an orddef, that's checked in tcinl}
  617. code_para := hp;
  618. hp := node;
  619. node := node^.right;
  620. hp^.right := nil;
  621. has_32bit_code := (porddef(code_para^.left^.resulttype)^.typ in [u32bit,s32bit]);
  622. End;
  623. {hp = destination now, save for later use}
  624. dest_para := hp;
  625. {if EAX is already in use, it's a register variable. Since we don't
  626. need another register besides EAX, release the one we got}
  627. If hreg <> R_EAX Then ungetregister32(hreg);
  628. {load and push the address of the destination}
  629. dummycoll.paratyp:=vs_var;
  630. dummycoll.data:=dest_para^.resulttype;
  631. secondcallparan(dest_para,@dummycoll,false,false,false,0);
  632. if codegenerror then
  633. exit;
  634. {save the regvars}
  635. pushusedregisters(pushed,$ff);
  636. {now that we've already pushed the addres of dest_para^.left on the
  637. stack, we can put the real parameters on the stack}
  638. If has_32bit_code Then
  639. Begin
  640. dummycoll.paratyp:=vs_var;
  641. dummycoll.data:=code_para^.resulttype;
  642. secondcallparan(code_para,@dummycoll,false,false,false,0);
  643. if codegenerror then
  644. exit;
  645. Disposetree(code_para);
  646. End
  647. Else
  648. Begin
  649. {only 32bit code parameter is supported, so fake one}
  650. GetTempOfSizeReference(4,hr);
  651. emitpushreferenceaddr(hr);
  652. End;
  653. {node = first parameter = string}
  654. dummycoll.paratyp:=vs_const;
  655. dummycoll.data:=node^.resulttype;
  656. secondcallparan(node,@dummycoll,false,false,false,0);
  657. if codegenerror then
  658. exit;
  659. Case dest_para^.resulttype^.deftype of
  660. floatdef:
  661. begin
  662. procedureprefix := 'FPC_VAL_REAL_';
  663. if pfloatdef(p^.resulttype)^.typ<>f32bit then
  664. inc(fpuvaroffset);
  665. end;
  666. orddef:
  667. if is_64bitint(dest_para^.resulttype) then
  668. begin
  669. if is_signed(dest_para^.resulttype) then
  670. procedureprefix := 'FPC_VAL_INT64_'
  671. else
  672. procedureprefix := 'FPC_VAL_QWORD_';
  673. end
  674. else
  675. begin
  676. if is_signed(dest_para^.resulttype) then
  677. begin
  678. {if we are converting to a signed number, we have to include the
  679. size of the destination, so the Val function can extend the sign
  680. of the result to allow proper range checking}
  681. emit_const(A_PUSH,S_L,dest_para^.resulttype^.size);
  682. procedureprefix := 'FPC_VAL_SINT_'
  683. end
  684. else
  685. procedureprefix := 'FPC_VAL_UINT_';
  686. end;
  687. End;
  688. emitcall(procedureprefix+pstringdef(node^.resulttype)^.stringtypname);
  689. { before disposing node we need to ungettemp !! PM }
  690. if node^.left^.location.loc in [LOC_REFERENCE,LOC_MEM] then
  691. ungetiftemp(node^.left^.location.reference);
  692. disposetree(node);
  693. p^.left := nil;
  694. {reload esi in case the dest_para/code_para is a class variable or so}
  695. maybe_loadesi;
  696. If (dest_para^.resulttype^.deftype = orddef) Then
  697. Begin
  698. {store the result in a safe place, because EAX may be used by a
  699. register variable}
  700. hreg := getexplicitregister32(R_EAX);
  701. emit_reg_reg(A_MOV,S_L,R_EAX,hreg);
  702. if is_64bitint(dest_para^.resulttype) then
  703. begin
  704. hreg2:=getexplicitregister32(R_EDX);
  705. emit_reg_reg(A_MOV,S_L,R_EDX,hreg2);
  706. end;
  707. {as of now, hreg now holds the location of the result, if it was
  708. integer}
  709. End;
  710. { restore the register vars}
  711. popusedregisters(pushed);
  712. If has_code and Not(has_32bit_code) Then
  713. {only 16bit code is possible}
  714. Begin
  715. {load the address of the code parameter}
  716. secondpass(code_para^.left);
  717. {move the code to its destination}
  718. emit_ref_reg(A_MOV,S_L,NewReference(hr),R_EDI);
  719. emit_mov_reg_loc(R_DI,code_para^.left^.location);
  720. Disposetree(code_para);
  721. End;
  722. {restore the address of the result}
  723. emit_reg(A_POP,S_L,R_EDI);
  724. {set up hr2 to a refernce with EDI as base register}
  725. reset_reference(hr2);
  726. hr2.base := R_EDI;
  727. {save the function result in the destination variable}
  728. Case dest_para^.left^.resulttype^.deftype of
  729. floatdef:
  730. floatstore(PFloatDef(dest_para^.left^.resulttype)^.typ, hr2);
  731. orddef:
  732. Case PordDef(dest_para^.left^.resulttype)^.typ of
  733. u8bit,s8bit:
  734. emit_reg_ref(A_MOV, S_B,
  735. RegToReg8(hreg),newreference(hr2));
  736. u16bit,s16bit:
  737. emit_reg_ref(A_MOV, S_W,
  738. RegToReg16(hreg),newreference(hr2));
  739. u32bit,s32bit:
  740. emit_reg_ref(A_MOV, S_L,
  741. hreg,newreference(hr2));
  742. u64bit,s64bit:
  743. begin
  744. emit_reg_ref(A_MOV, S_L,
  745. hreg,newreference(hr2));
  746. r:=newreference(hr2);
  747. inc(r^.offset,4);
  748. emit_reg_ref(A_MOV, S_L,
  749. hreg2,r);
  750. end;
  751. End;
  752. End;
  753. If (cs_check_range in aktlocalswitches) and
  754. (dest_para^.left^.resulttype^.deftype = orddef) and
  755. (not(is_64bitint(dest_para^.left^.resulttype))) and
  756. {the following has to be changed to 64bit checking, once Val
  757. returns 64 bit values (unless a special Val function is created
  758. for that)}
  759. {no need to rangecheck longints or cardinals on 32bit processors}
  760. not((porddef(dest_para^.left^.resulttype)^.typ = s32bit) and
  761. (porddef(dest_para^.left^.resulttype)^.low = $80000000) and
  762. (porddef(dest_para^.left^.resulttype)^.high = $7fffffff)) and
  763. not((porddef(dest_para^.left^.resulttype)^.typ = u32bit) and
  764. (porddef(dest_para^.left^.resulttype)^.low = 0) and
  765. (porddef(dest_para^.left^.resulttype)^.high = $ffffffff)) then
  766. Begin
  767. hp := getcopy(dest_para^.left);
  768. hp^.location.loc := LOC_REGISTER;
  769. hp^.location.register := hreg;
  770. {do not register this temporary def}
  771. OldRegisterDef := RegisterDef;
  772. RegisterDef := False;
  773. Case PordDef(dest_para^.left^.resulttype)^.typ of
  774. u8bit,u16bit,u32bit: new(hdef,init(u32bit,0,$ffffffff));
  775. s8bit,s16bit,s32bit: new(hdef,init(s32bit,$80000000,$7fffffff));
  776. end;
  777. hp^.resulttype := hdef;
  778. emitrangecheck(hp,dest_para^.left^.resulttype);
  779. hp^.right := nil;
  780. Dispose(hp^.resulttype, Done);
  781. RegisterDef := OldRegisterDef;
  782. disposetree(hp);
  783. End;
  784. {dest_para^.right is already nil}
  785. disposetree(dest_para);
  786. UnGetIfTemp(hr);
  787. end;
  788. var
  789. r : preference;
  790. hp : ptree;
  791. l : longint;
  792. ispushed : boolean;
  793. hregister : tregister;
  794. otlabel,oflabel,l1 : pasmlabel;
  795. oldpushedparasize : longint;
  796. begin
  797. { save & reset pushedparasize }
  798. oldpushedparasize:=pushedparasize;
  799. pushedparasize:=0;
  800. case p^.inlinenumber of
  801. in_assert_x_y:
  802. begin
  803. { the node should be removed in the firstpass }
  804. if not (cs_do_assertion in aktlocalswitches) then
  805. internalerror(7123458);
  806. otlabel:=truelabel;
  807. oflabel:=falselabel;
  808. getlabel(truelabel);
  809. getlabel(falselabel);
  810. secondpass(p^.left^.left);
  811. maketojumpbool(p^.left^.left);
  812. emitlab(falselabel);
  813. { erroraddr }
  814. emit_reg(A_PUSH,S_L,R_EBP);
  815. { lineno }
  816. emit_const(A_PUSH,S_L,aktfilepos.line);
  817. { filename string }
  818. hp:=genstringconstnode(current_module^.sourcefiles^.get_file_name(aktfilepos.fileindex));
  819. secondpass(hp);
  820. if codegenerror then
  821. exit;
  822. emitpushreferenceaddr(hp^.location.reference);
  823. disposetree(hp);
  824. { push msg }
  825. secondpass(p^.left^.right^.left);
  826. emitpushreferenceaddr(p^.left^.right^.left^.location.reference);
  827. { call }
  828. emitcall('FPC_ASSERT');
  829. emitlab(truelabel);
  830. freelabel(truelabel);
  831. freelabel(falselabel);
  832. truelabel:=otlabel;
  833. falselabel:=oflabel;
  834. end;
  835. in_lo_word,
  836. in_hi_word :
  837. begin
  838. secondpass(p^.left);
  839. p^.location.loc:=LOC_REGISTER;
  840. if p^.left^.location.loc<>LOC_REGISTER then
  841. begin
  842. if p^.left^.location.loc=LOC_CREGISTER then
  843. begin
  844. p^.location.register:=reg32toreg16(getregister32);
  845. emit_reg_reg(A_MOV,S_W,p^.left^.location.register,
  846. p^.location.register);
  847. end
  848. else
  849. begin
  850. del_reference(p^.left^.location.reference);
  851. p^.location.register:=reg32toreg16(getregister32);
  852. emit_ref_reg(A_MOV,S_W,newreference(p^.left^.location.reference),
  853. p^.location.register);
  854. end;
  855. end
  856. else p^.location.register:=p^.left^.location.register;
  857. if p^.inlinenumber=in_hi_word then
  858. emit_const_reg(A_SHR,S_W,8,p^.location.register);
  859. p^.location.register:=reg16toreg8(p^.location.register);
  860. end;
  861. in_sizeof_x,
  862. in_typeof_x :
  863. begin
  864. { for both cases load vmt }
  865. if p^.left^.treetype=typen then
  866. begin
  867. p^.location.register:=getregister32;
  868. emit_sym_ofs_reg(A_MOV,
  869. S_L,newasmsymbol(pobjectdef(p^.left^.resulttype)^.vmt_mangledname),0,
  870. p^.location.register);
  871. end
  872. else
  873. begin
  874. secondpass(p^.left);
  875. del_reference(p^.left^.location.reference);
  876. p^.location.loc:=LOC_REGISTER;
  877. p^.location.register:=getregister32;
  878. { load VMT pointer }
  879. inc(p^.left^.location.reference.offset,
  880. pobjectdef(p^.left^.resulttype)^.vmt_offset);
  881. emit_ref_reg(A_MOV,S_L,
  882. newreference(p^.left^.location.reference),
  883. p^.location.register);
  884. end;
  885. { in sizeof load size }
  886. if p^.inlinenumber=in_sizeof_x then
  887. begin
  888. new(r);
  889. reset_reference(r^);
  890. r^.base:=p^.location.register;
  891. emit_ref_reg(A_MOV,S_L,r,
  892. p^.location.register);
  893. end;
  894. end;
  895. in_lo_long,
  896. in_hi_long :
  897. begin
  898. secondpass(p^.left);
  899. p^.location.loc:=LOC_REGISTER;
  900. if p^.left^.location.loc<>LOC_REGISTER then
  901. begin
  902. if p^.left^.location.loc=LOC_CREGISTER then
  903. begin
  904. p^.location.register:=getregister32;
  905. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,
  906. p^.location.register);
  907. end
  908. else
  909. begin
  910. del_reference(p^.left^.location.reference);
  911. p^.location.register:=getregister32;
  912. emit_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  913. p^.location.register);
  914. end;
  915. end
  916. else p^.location.register:=p^.left^.location.register;
  917. if p^.inlinenumber=in_hi_long then
  918. emit_const_reg(A_SHR,S_L,16,p^.location.register);
  919. p^.location.register:=reg32toreg16(p^.location.register);
  920. end;
  921. in_lo_qword,
  922. in_hi_qword:
  923. begin
  924. secondpass(p^.left);
  925. p^.location.loc:=LOC_REGISTER;
  926. case p^.left^.location.loc of
  927. LOC_CREGISTER:
  928. begin
  929. p^.location.register:=getregister32;
  930. if p^.inlinenumber=in_hi_qword then
  931. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerhigh,
  932. p^.location.register)
  933. else
  934. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerlow,
  935. p^.location.register)
  936. end;
  937. LOC_MEM,LOC_REFERENCE:
  938. begin
  939. del_reference(p^.left^.location.reference);
  940. p^.location.register:=getregister32;
  941. r:=newreference(p^.left^.location.reference);
  942. if p^.inlinenumber=in_hi_qword then
  943. inc(r^.offset,4);
  944. emit_ref_reg(A_MOV,S_L,
  945. r,p^.location.register);
  946. end;
  947. LOC_REGISTER:
  948. begin
  949. if p^.inlinenumber=in_hi_qword then
  950. begin
  951. p^.location.register:=p^.left^.location.registerhigh;
  952. ungetregister32(p^.left^.location.registerlow);
  953. end
  954. else
  955. begin
  956. p^.location.register:=p^.left^.location.registerlow;
  957. ungetregister32(p^.left^.location.registerhigh);
  958. end;
  959. end;
  960. end;
  961. end;
  962. in_length_string :
  963. begin
  964. secondpass(p^.left);
  965. set_location(p^.location,p^.left^.location);
  966. { length in ansi strings is at offset -8 }
  967. if is_ansistring(p^.left^.resulttype) then
  968. dec(p^.location.reference.offset,8)
  969. { char is always 1, so make it a constant value }
  970. else if is_char(p^.left^.resulttype) then
  971. begin
  972. clear_location(p^.location);
  973. p^.location.loc:=LOC_MEM;
  974. p^.location.reference.is_immediate:=true;
  975. p^.location.reference.offset:=1;
  976. end;
  977. end;
  978. in_pred_x,
  979. in_succ_x:
  980. begin
  981. secondpass(p^.left);
  982. if not (cs_check_overflow in aktlocalswitches) then
  983. if p^.inlinenumber=in_pred_x then
  984. asmop:=A_DEC
  985. else
  986. asmop:=A_INC
  987. else
  988. if p^.inlinenumber=in_pred_x then
  989. asmop:=A_SUB
  990. else
  991. asmop:=A_ADD;
  992. case p^.resulttype^.size of
  993. 4 : opsize:=S_L;
  994. 2 : opsize:=S_W;
  995. 1 : opsize:=S_B;
  996. else
  997. internalerror(10080);
  998. end;
  999. p^.location.loc:=LOC_REGISTER;
  1000. if p^.left^.location.loc<>LOC_REGISTER then
  1001. begin
  1002. p^.location.register:=getregister32;
  1003. if (p^.resulttype^.size=2) then
  1004. p^.location.register:=reg32toreg16(p^.location.register);
  1005. if (p^.resulttype^.size=1) then
  1006. p^.location.register:=reg32toreg8(p^.location.register);
  1007. if p^.left^.location.loc=LOC_CREGISTER then
  1008. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,
  1009. p^.location.register)
  1010. else
  1011. if p^.left^.location.loc=LOC_FLAGS then
  1012. emit_flag2reg(p^.left^.location.resflags,p^.location.register)
  1013. else
  1014. begin
  1015. del_reference(p^.left^.location.reference);
  1016. emit_ref_reg(A_MOV,opsize,newreference(p^.left^.location.reference),
  1017. p^.location.register);
  1018. end;
  1019. end
  1020. else p^.location.register:=p^.left^.location.register;
  1021. if not (cs_check_overflow in aktlocalswitches) then
  1022. emit_reg(asmop,opsize,
  1023. p^.location.register)
  1024. else
  1025. emit_const_reg(asmop,opsize,1,
  1026. p^.location.register);
  1027. emitoverflowcheck(p);
  1028. emitrangecheck(p,p^.resulttype);
  1029. end;
  1030. in_dec_x,
  1031. in_inc_x :
  1032. begin
  1033. { set defaults }
  1034. addvalue:=1;
  1035. addconstant:=true;
  1036. { load first parameter, must be a reference }
  1037. secondpass(p^.left^.left);
  1038. case p^.left^.left^.resulttype^.deftype of
  1039. orddef,
  1040. enumdef : begin
  1041. case p^.left^.left^.resulttype^.size of
  1042. 1 : opsize:=S_B;
  1043. 2 : opsize:=S_W;
  1044. 4 : opsize:=S_L;
  1045. end;
  1046. end;
  1047. pointerdef : begin
  1048. opsize:=S_L;
  1049. if porddef(ppointerdef(p^.left^.left^.resulttype)^.definition)=voiddef then
  1050. addvalue:=1
  1051. else
  1052. addvalue:=ppointerdef(p^.left^.left^.resulttype)^.definition^.size;
  1053. end;
  1054. else
  1055. internalerror(10081);
  1056. end;
  1057. { second argument specified?, must be a s32bit in register }
  1058. if assigned(p^.left^.right) then
  1059. begin
  1060. ispushed:=maybe_push(p^.left^.right^.left^.registers32,p^.left^.left,false);
  1061. secondpass(p^.left^.right^.left);
  1062. if ispushed then
  1063. restore(p^.left^.left,false);
  1064. { when constant, just multiply the addvalue }
  1065. if is_constintnode(p^.left^.right^.left) then
  1066. addvalue:=addvalue*get_ordinal_value(p^.left^.right^.left)
  1067. else
  1068. begin
  1069. case p^.left^.right^.left^.location.loc of
  1070. LOC_REGISTER,
  1071. LOC_CREGISTER : hregister:=p^.left^.right^.left^.location.register;
  1072. LOC_MEM,
  1073. LOC_REFERENCE : begin
  1074. del_reference(p^.left^.right^.left^.location.reference);
  1075. hregister:=getregister32;
  1076. emit_ref_reg(A_MOV,S_L,
  1077. newreference(p^.left^.right^.left^.location.reference),hregister);
  1078. end;
  1079. else
  1080. internalerror(10082);
  1081. end;
  1082. { insert multiply with addvalue if its >1 }
  1083. if addvalue>1 then
  1084. emit_const_reg(A_IMUL,opsize,
  1085. addvalue,hregister);
  1086. addconstant:=false;
  1087. end;
  1088. end;
  1089. { write the add instruction }
  1090. if addconstant then
  1091. begin
  1092. if (addvalue=1) and not(cs_check_overflow in aktlocalswitches) then
  1093. begin
  1094. if p^.left^.left^.location.loc=LOC_CREGISTER then
  1095. emit_reg(incdecop[p^.inlinenumber],opsize,
  1096. p^.left^.left^.location.register)
  1097. else
  1098. emit_ref(incdecop[p^.inlinenumber],opsize,
  1099. newreference(p^.left^.left^.location.reference))
  1100. end
  1101. else
  1102. begin
  1103. if p^.left^.left^.location.loc=LOC_CREGISTER then
  1104. emit_const_reg(addsubop[p^.inlinenumber],opsize,
  1105. addvalue,p^.left^.left^.location.register)
  1106. else
  1107. emit_const_ref(addsubop[p^.inlinenumber],opsize,
  1108. addvalue,newreference(p^.left^.left^.location.reference));
  1109. end
  1110. end
  1111. else
  1112. begin
  1113. { BUG HERE : detected with nasm :
  1114. hregister is allways 32 bit
  1115. it should be converted to 16 or 8 bit depending on op_size PM }
  1116. { still not perfect :
  1117. if hregister is already a 16 bit reg ?? PM }
  1118. { makeregXX is the solution (FK) }
  1119. case opsize of
  1120. S_B : hregister:=makereg8(hregister);
  1121. S_W : hregister:=makereg16(hregister);
  1122. end;
  1123. if p^.left^.left^.location.loc=LOC_CREGISTER then
  1124. emit_reg_reg(addsubop[p^.inlinenumber],opsize,
  1125. hregister,p^.left^.left^.location.register)
  1126. else
  1127. emit_reg_ref(addsubop[p^.inlinenumber],opsize,
  1128. hregister,newreference(p^.left^.left^.location.reference));
  1129. case opsize of
  1130. S_B : hregister:=reg8toreg32(hregister);
  1131. S_W : hregister:=reg16toreg32(hregister);
  1132. end;
  1133. ungetregister32(hregister);
  1134. end;
  1135. emitoverflowcheck(p^.left^.left);
  1136. emitrangecheck(p^.left^.left,p^.left^.left^.resulttype);
  1137. end;
  1138. in_assigned_x :
  1139. begin
  1140. secondpass(p^.left^.left);
  1141. p^.location.loc:=LOC_FLAGS;
  1142. if (p^.left^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1143. begin
  1144. emit_reg_reg(A_OR,S_L,
  1145. p^.left^.left^.location.register,
  1146. p^.left^.left^.location.register);
  1147. ungetregister32(p^.left^.left^.location.register);
  1148. end
  1149. else
  1150. begin
  1151. emit_const_ref(A_CMP,S_L,0,
  1152. newreference(p^.left^.left^.location.reference));
  1153. del_reference(p^.left^.left^.location.reference);
  1154. end;
  1155. p^.location.resflags:=F_NE;
  1156. end;
  1157. in_reset_typedfile,in_rewrite_typedfile :
  1158. begin
  1159. pushusedregisters(pushed,$ff);
  1160. emit_const(A_PUSH,S_L,pfiledef(p^.left^.resulttype)^.typed_as^.size);
  1161. secondpass(p^.left);
  1162. emitpushreferenceaddr(p^.left^.location.reference);
  1163. if p^.inlinenumber=in_reset_typedfile then
  1164. emitcall('FPC_RESET_TYPED')
  1165. else
  1166. emitcall('FPC_REWRITE_TYPED');
  1167. popusedregisters(pushed);
  1168. end;
  1169. in_write_x :
  1170. handlereadwrite(false,false);
  1171. in_writeln_x :
  1172. handlereadwrite(false,true);
  1173. in_read_x :
  1174. handlereadwrite(true,false);
  1175. in_readln_x :
  1176. handlereadwrite(true,true);
  1177. in_str_x_string :
  1178. begin
  1179. handle_str;
  1180. maybe_loadesi;
  1181. end;
  1182. in_val_x :
  1183. Begin
  1184. handle_val;
  1185. End;
  1186. in_include_x_y,
  1187. in_exclude_x_y:
  1188. begin
  1189. secondpass(p^.left^.left);
  1190. if p^.left^.right^.left^.treetype=ordconstn then
  1191. begin
  1192. { calculate bit position }
  1193. l:=1 shl (p^.left^.right^.left^.value mod 32);
  1194. { determine operator }
  1195. if p^.inlinenumber=in_include_x_y then
  1196. asmop:=A_OR
  1197. else
  1198. begin
  1199. asmop:=A_AND;
  1200. l:=not(l);
  1201. end;
  1202. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  1203. begin
  1204. inc(p^.left^.left^.location.reference.offset,(p^.left^.right^.left^.value div 32)*4);
  1205. emit_const_ref(asmop,S_L,
  1206. l,newreference(p^.left^.left^.location.reference));
  1207. del_reference(p^.left^.left^.location.reference);
  1208. end
  1209. else
  1210. { LOC_CREGISTER }
  1211. emit_const_reg(asmop,S_L,
  1212. l,p^.left^.left^.location.register);
  1213. end
  1214. else
  1215. begin
  1216. { generate code for the element to set }
  1217. ispushed:=maybe_push(p^.left^.right^.left^.registers32,p^.left^.left,false);
  1218. secondpass(p^.left^.right^.left);
  1219. if ispushed then
  1220. restore(p^.left^.left,false);
  1221. { determine asm operator }
  1222. if p^.inlinenumber=in_include_x_y then
  1223. asmop:=A_BTS
  1224. else
  1225. asmop:=A_BTR;
  1226. if psetdef(p^.left^.resulttype)^.settype=smallset then
  1227. begin
  1228. if p^.left^.right^.left^.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  1229. hregister:=p^.left^.right^.left^.location.register
  1230. else
  1231. begin
  1232. hregister:=R_EDI;
  1233. opsize:=def2def_opsize(p^.left^.right^.left^.resulttype,u32bitdef);
  1234. if opsize in [S_B,S_W,S_L] then
  1235. op:=A_MOV
  1236. else
  1237. op:=A_MOVZX;
  1238. emit_ref_reg(op,opsize,
  1239. newreference(p^.left^.right^.left^.location.reference),R_EDI);
  1240. end;
  1241. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  1242. emit_reg_ref(asmop,S_L,hregister,
  1243. newreference(p^.left^.left^.location.reference))
  1244. else
  1245. emit_reg_reg(asmop,S_L,hregister,
  1246. p^.left^.left^.location.register);
  1247. end
  1248. else
  1249. begin
  1250. pushsetelement(p^.left^.right^.left);
  1251. { normset is allways a ref }
  1252. emitpushreferenceaddr(p^.left^.left^.location.reference);
  1253. if p^.inlinenumber=in_include_x_y then
  1254. emitcall('FPC_SET_SET_BYTE')
  1255. else
  1256. emitcall('FPC_SET_UNSET_BYTE');
  1257. {CGMessage(cg_e_include_not_implemented);}
  1258. end;
  1259. end;
  1260. end;
  1261. in_pi:
  1262. begin
  1263. emit_none(A_FLDPI,S_NO);
  1264. inc(fpuvaroffset);
  1265. end;
  1266. in_sin_extended,
  1267. in_arctan_extended,
  1268. in_abs_extended,
  1269. in_sqr_extended,
  1270. in_sqrt_extended,
  1271. in_ln_extended,
  1272. in_cos_extended:
  1273. begin
  1274. secondpass(p^.left);
  1275. case p^.left^.location.loc of
  1276. LOC_FPU:
  1277. ;
  1278. LOC_CFPUREGISTER:
  1279. begin
  1280. emit_reg(A_FLD,S_NO,
  1281. correct_fpuregister(p^.left^.location.register,fpuvaroffset));
  1282. inc(fpuvaroffset);
  1283. end;
  1284. LOC_REFERENCE,LOC_MEM:
  1285. floatload(pfloatdef(p^.left^.resulttype)^.typ,p^.left^.location.reference);
  1286. else
  1287. internalerror(309991);
  1288. end;
  1289. case p^.inlinenumber of
  1290. in_sin_extended,
  1291. in_cos_extended:
  1292. begin
  1293. getlabel(l1);
  1294. if p^.inlinenumber=in_sin_extended then
  1295. emit_none(A_FSIN,S_NO)
  1296. else
  1297. emit_none(A_FCOS,S_NO);
  1298. {
  1299. emit_reg(A_FNSTSW,S_NO,R_AX);
  1300. emit_none(A_SAHF,S_NO);
  1301. emitjmp(C_NP,l1);
  1302. emit_reg(A_FSTP,S_NO,R_ST0);
  1303. emit_none(A_FLDZ,S_NO);
  1304. emitlab(l1);
  1305. }
  1306. end;
  1307. in_arctan_extended:
  1308. begin
  1309. emit_none(A_FLD1,S_NO);
  1310. emit_none(A_FPATAN,S_NO);
  1311. end;
  1312. in_abs_extended:
  1313. emit_none(A_FABS,S_NO);
  1314. in_sqr_extended:
  1315. begin
  1316. emit_reg(A_FLD,S_NO,R_ST0);
  1317. emit_none(A_FMULP,S_NO);
  1318. end;
  1319. in_sqrt_extended:
  1320. emit_none(A_FSQRT,S_NO);
  1321. in_ln_extended:
  1322. begin
  1323. emit_none(A_FLDLN2,S_NO);
  1324. emit_none(A_FXCH,S_NO);
  1325. emit_none(A_FYL2X,S_NO);
  1326. end;
  1327. end;
  1328. end;
  1329. {$ifdef SUPPORT_MMX}
  1330. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1331. begin
  1332. if p^.left^.location.loc=LOC_REGISTER then
  1333. begin
  1334. {!!!!!!!}
  1335. end
  1336. else if p^.left^.left^.location.loc=LOC_REGISTER then
  1337. begin
  1338. {!!!!!!!}
  1339. end
  1340. else
  1341. begin
  1342. {!!!!!!!}
  1343. end;
  1344. end;
  1345. {$endif SUPPORT_MMX}
  1346. else internalerror(9);
  1347. end;
  1348. { reset pushedparasize }
  1349. pushedparasize:=oldpushedparasize;
  1350. end;
  1351. end.
  1352. {
  1353. $Log$
  1354. Revision 1.77 1999-11-06 14:34:17 peter
  1355. * truncated log to 20 revs
  1356. Revision 1.76 1999/10/29 15:28:51 peter
  1357. * fixed assert, the tree is now disposed in firstpass if assertions
  1358. are off.
  1359. Revision 1.75 1999/10/26 12:30:40 peter
  1360. * const parameter is now checked
  1361. * better and generic check if a node can be used for assigning
  1362. * export fixes
  1363. * procvar equal works now (it never had worked at least from 0.99.8)
  1364. * defcoll changed to linkedlist with pparaitem so it can easily be
  1365. walked both directions
  1366. Revision 1.74 1999/10/21 16:41:38 florian
  1367. * problems with readln fixed: esi wasn't restored correctly when
  1368. reading ordinal fields of objects futher the register allocation
  1369. didn't take care of the extra register when reading ordinal values
  1370. * enumerations can now be used in constant indexes of properties
  1371. Revision 1.73 1999/09/28 20:48:23 florian
  1372. * fixed bug 610
  1373. + added $D- for TP in symtable.pas else it can't be compiled anymore
  1374. (too much symbols :()
  1375. Revision 1.72 1999/09/26 13:26:05 florian
  1376. * exception patch of Romio nevertheless the excpetion handling
  1377. needs some corections regarding register saving
  1378. * gettempansistring is again a procedure
  1379. Revision 1.71 1999/09/16 07:52:37 pierre
  1380. * FLDPI must increment fpuvaroffset
  1381. Revision 1.70 1999/09/15 20:35:38 florian
  1382. * small fix to operator overloading when in MMX mode
  1383. + the compiler uses now fldz and fld1 if possible
  1384. + some fixes to floating point registers
  1385. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  1386. * .... ???
  1387. Revision 1.69 1999/08/28 15:34:16 florian
  1388. * bug 519 fixed
  1389. Revision 1.68 1999/08/19 13:08:47 pierre
  1390. * emit_??? used
  1391. Revision 1.67 1999/08/10 13:21:08 pierre
  1392. * fpuvaroffset not increased for f32bit float type
  1393. Revision 1.66 1999/08/10 12:47:53 pierre
  1394. * fpuvaroffset problems solved
  1395. Revision 1.65 1999/08/04 00:22:47 florian
  1396. * renamed i386asm and i386base to cpuasm and cpubase
  1397. Revision 1.64 1999/08/03 22:02:42 peter
  1398. * moved bitmask constants to sets
  1399. * some other type/const renamings
  1400. Revision 1.63 1999/07/23 16:05:18 peter
  1401. * alignment is now saved in the symtable
  1402. * C alignment added for records
  1403. * PPU version increased to solve .12 <-> .13 probs
  1404. Revision 1.62 1999/07/05 20:13:10 peter
  1405. * removed temp defines
  1406. Revision 1.61 1999/07/03 14:14:27 florian
  1407. + start of val(int64/qword)
  1408. * longbool, wordbool constants weren't written, fixed
  1409. Revision 1.60 1999/07/01 15:49:09 florian
  1410. * int64/qword type release
  1411. + lo/hi for int64/qword
  1412. Revision 1.59 1999/06/21 16:33:27 jonas
  1413. * fixed include() with smallsets
  1414. Revision 1.58 1999/06/11 11:44:56 peter
  1415. *** empty log message ***
  1416. Revision 1.57 1999/06/02 10:11:43 florian
  1417. * make cycle fixed i.e. compilation with 0.99.10
  1418. * some fixes for qword
  1419. * start of register calling conventions
  1420. }