cg386inl.pas 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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. 8 : opsize:=S_L;
  994. 4 : opsize:=S_L;
  995. 2 : opsize:=S_W;
  996. 1 : opsize:=S_B;
  997. else
  998. internalerror(10080);
  999. end;
  1000. p^.location.loc:=LOC_REGISTER;
  1001. if p^.left^.location.loc<>LOC_REGISTER then
  1002. begin
  1003. p^.location.register:=getregister32;
  1004. if (p^.resulttype^.size=2) then
  1005. p^.location.register:=reg32toreg16(p^.location.register);
  1006. if (p^.resulttype^.size=1) then
  1007. p^.location.register:=reg32toreg8(p^.location.register);
  1008. if p^.left^.location.loc=LOC_CREGISTER then
  1009. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,
  1010. p^.location.register)
  1011. else
  1012. if p^.left^.location.loc=LOC_FLAGS then
  1013. emit_flag2reg(p^.left^.location.resflags,p^.location.register)
  1014. else
  1015. begin
  1016. del_reference(p^.left^.location.reference);
  1017. emit_ref_reg(A_MOV,opsize,newreference(p^.left^.location.reference),
  1018. p^.location.register);
  1019. end;
  1020. end
  1021. else p^.location.register:=p^.left^.location.register;
  1022. if not (cs_check_overflow in aktlocalswitches) then
  1023. emit_reg(asmop,opsize,
  1024. p^.location.register)
  1025. else
  1026. emit_const_reg(asmop,opsize,1,
  1027. p^.location.register);
  1028. emitoverflowcheck(p);
  1029. emitrangecheck(p,p^.resulttype);
  1030. end;
  1031. in_dec_x,
  1032. in_inc_x :
  1033. begin
  1034. { set defaults }
  1035. addvalue:=1;
  1036. addconstant:=true;
  1037. { load first parameter, must be a reference }
  1038. secondpass(p^.left^.left);
  1039. case p^.left^.left^.resulttype^.deftype of
  1040. orddef,
  1041. enumdef : begin
  1042. case p^.left^.left^.resulttype^.size of
  1043. 1 : opsize:=S_B;
  1044. 2 : opsize:=S_W;
  1045. 4 : opsize:=S_L;
  1046. 8 : opsize:=S_L;
  1047. end;
  1048. end;
  1049. pointerdef : begin
  1050. opsize:=S_L;
  1051. if porddef(ppointerdef(p^.left^.left^.resulttype)^.definition)=voiddef then
  1052. addvalue:=1
  1053. else
  1054. addvalue:=ppointerdef(p^.left^.left^.resulttype)^.definition^.size;
  1055. end;
  1056. else
  1057. internalerror(10081);
  1058. end;
  1059. { second argument specified?, must be a s32bit in register }
  1060. if assigned(p^.left^.right) then
  1061. begin
  1062. ispushed:=maybe_push(p^.left^.right^.left^.registers32,p^.left^.left,false);
  1063. secondpass(p^.left^.right^.left);
  1064. if ispushed then
  1065. restore(p^.left^.left,false);
  1066. { when constant, just multiply the addvalue }
  1067. if is_constintnode(p^.left^.right^.left) then
  1068. addvalue:=addvalue*get_ordinal_value(p^.left^.right^.left)
  1069. else
  1070. begin
  1071. case p^.left^.right^.left^.location.loc of
  1072. LOC_REGISTER,
  1073. LOC_CREGISTER : hregister:=p^.left^.right^.left^.location.register;
  1074. LOC_MEM,
  1075. LOC_REFERENCE : begin
  1076. del_reference(p^.left^.right^.left^.location.reference);
  1077. hregister:=getregister32;
  1078. emit_ref_reg(A_MOV,S_L,
  1079. newreference(p^.left^.right^.left^.location.reference),hregister);
  1080. end;
  1081. else
  1082. internalerror(10082);
  1083. end;
  1084. { insert multiply with addvalue if its >1 }
  1085. if addvalue>1 then
  1086. emit_const_reg(A_IMUL,opsize,
  1087. addvalue,hregister);
  1088. addconstant:=false;
  1089. end;
  1090. end;
  1091. { write the add instruction }
  1092. if addconstant then
  1093. begin
  1094. if (addvalue=1) and not(cs_check_overflow in aktlocalswitches) then
  1095. begin
  1096. if p^.left^.left^.location.loc=LOC_CREGISTER then
  1097. emit_reg(incdecop[p^.inlinenumber],opsize,
  1098. p^.left^.left^.location.register)
  1099. else
  1100. emit_ref(incdecop[p^.inlinenumber],opsize,
  1101. newreference(p^.left^.left^.location.reference))
  1102. end
  1103. else
  1104. begin
  1105. if p^.left^.left^.location.loc=LOC_CREGISTER then
  1106. emit_const_reg(addsubop[p^.inlinenumber],opsize,
  1107. addvalue,p^.left^.left^.location.register)
  1108. else
  1109. emit_const_ref(addsubop[p^.inlinenumber],opsize,
  1110. addvalue,newreference(p^.left^.left^.location.reference));
  1111. end
  1112. end
  1113. else
  1114. begin
  1115. { BUG HERE : detected with nasm :
  1116. hregister is allways 32 bit
  1117. it should be converted to 16 or 8 bit depending on op_size PM }
  1118. { still not perfect :
  1119. if hregister is already a 16 bit reg ?? PM }
  1120. { makeregXX is the solution (FK) }
  1121. case opsize of
  1122. S_B : hregister:=makereg8(hregister);
  1123. S_W : hregister:=makereg16(hregister);
  1124. end;
  1125. if p^.left^.left^.location.loc=LOC_CREGISTER then
  1126. emit_reg_reg(addsubop[p^.inlinenumber],opsize,
  1127. hregister,p^.left^.left^.location.register)
  1128. else
  1129. emit_reg_ref(addsubop[p^.inlinenumber],opsize,
  1130. hregister,newreference(p^.left^.left^.location.reference));
  1131. case opsize of
  1132. S_B : hregister:=reg8toreg32(hregister);
  1133. S_W : hregister:=reg16toreg32(hregister);
  1134. end;
  1135. ungetregister32(hregister);
  1136. end;
  1137. emitoverflowcheck(p^.left^.left);
  1138. emitrangecheck(p^.left^.left,p^.left^.left^.resulttype);
  1139. end;
  1140. in_assigned_x :
  1141. begin
  1142. secondpass(p^.left^.left);
  1143. p^.location.loc:=LOC_FLAGS;
  1144. if (p^.left^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1145. begin
  1146. emit_reg_reg(A_OR,S_L,
  1147. p^.left^.left^.location.register,
  1148. p^.left^.left^.location.register);
  1149. ungetregister32(p^.left^.left^.location.register);
  1150. end
  1151. else
  1152. begin
  1153. emit_const_ref(A_CMP,S_L,0,
  1154. newreference(p^.left^.left^.location.reference));
  1155. del_reference(p^.left^.left^.location.reference);
  1156. end;
  1157. p^.location.resflags:=F_NE;
  1158. end;
  1159. in_reset_typedfile,in_rewrite_typedfile :
  1160. begin
  1161. pushusedregisters(pushed,$ff);
  1162. emit_const(A_PUSH,S_L,pfiledef(p^.left^.resulttype)^.typed_as^.size);
  1163. secondpass(p^.left);
  1164. emitpushreferenceaddr(p^.left^.location.reference);
  1165. if p^.inlinenumber=in_reset_typedfile then
  1166. emitcall('FPC_RESET_TYPED')
  1167. else
  1168. emitcall('FPC_REWRITE_TYPED');
  1169. popusedregisters(pushed);
  1170. end;
  1171. in_write_x :
  1172. handlereadwrite(false,false);
  1173. in_writeln_x :
  1174. handlereadwrite(false,true);
  1175. in_read_x :
  1176. handlereadwrite(true,false);
  1177. in_readln_x :
  1178. handlereadwrite(true,true);
  1179. in_str_x_string :
  1180. begin
  1181. handle_str;
  1182. maybe_loadesi;
  1183. end;
  1184. in_val_x :
  1185. Begin
  1186. handle_val;
  1187. End;
  1188. in_include_x_y,
  1189. in_exclude_x_y:
  1190. begin
  1191. secondpass(p^.left^.left);
  1192. if p^.left^.right^.left^.treetype=ordconstn then
  1193. begin
  1194. { calculate bit position }
  1195. l:=1 shl (p^.left^.right^.left^.value mod 32);
  1196. { determine operator }
  1197. if p^.inlinenumber=in_include_x_y then
  1198. asmop:=A_OR
  1199. else
  1200. begin
  1201. asmop:=A_AND;
  1202. l:=not(l);
  1203. end;
  1204. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  1205. begin
  1206. inc(p^.left^.left^.location.reference.offset,(p^.left^.right^.left^.value div 32)*4);
  1207. emit_const_ref(asmop,S_L,
  1208. l,newreference(p^.left^.left^.location.reference));
  1209. del_reference(p^.left^.left^.location.reference);
  1210. end
  1211. else
  1212. { LOC_CREGISTER }
  1213. emit_const_reg(asmop,S_L,
  1214. l,p^.left^.left^.location.register);
  1215. end
  1216. else
  1217. begin
  1218. { generate code for the element to set }
  1219. ispushed:=maybe_push(p^.left^.right^.left^.registers32,p^.left^.left,false);
  1220. secondpass(p^.left^.right^.left);
  1221. if ispushed then
  1222. restore(p^.left^.left,false);
  1223. { determine asm operator }
  1224. if p^.inlinenumber=in_include_x_y then
  1225. asmop:=A_BTS
  1226. else
  1227. asmop:=A_BTR;
  1228. if psetdef(p^.left^.resulttype)^.settype=smallset then
  1229. begin
  1230. if p^.left^.right^.left^.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  1231. hregister:=p^.left^.right^.left^.location.register
  1232. else
  1233. begin
  1234. hregister:=R_EDI;
  1235. opsize:=def2def_opsize(p^.left^.right^.left^.resulttype,u32bitdef);
  1236. if opsize in [S_B,S_W,S_L] then
  1237. op:=A_MOV
  1238. else
  1239. op:=A_MOVZX;
  1240. emit_ref_reg(op,opsize,
  1241. newreference(p^.left^.right^.left^.location.reference),R_EDI);
  1242. end;
  1243. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  1244. emit_reg_ref(asmop,S_L,hregister,
  1245. newreference(p^.left^.left^.location.reference))
  1246. else
  1247. emit_reg_reg(asmop,S_L,hregister,
  1248. p^.left^.left^.location.register);
  1249. end
  1250. else
  1251. begin
  1252. pushsetelement(p^.left^.right^.left);
  1253. { normset is allways a ref }
  1254. emitpushreferenceaddr(p^.left^.left^.location.reference);
  1255. if p^.inlinenumber=in_include_x_y then
  1256. emitcall('FPC_SET_SET_BYTE')
  1257. else
  1258. emitcall('FPC_SET_UNSET_BYTE');
  1259. {CGMessage(cg_e_include_not_implemented);}
  1260. end;
  1261. end;
  1262. end;
  1263. in_pi:
  1264. begin
  1265. emit_none(A_FLDPI,S_NO);
  1266. inc(fpuvaroffset);
  1267. end;
  1268. in_sin_extended,
  1269. in_arctan_extended,
  1270. in_abs_extended,
  1271. in_sqr_extended,
  1272. in_sqrt_extended,
  1273. in_ln_extended,
  1274. in_cos_extended:
  1275. begin
  1276. secondpass(p^.left);
  1277. case p^.left^.location.loc of
  1278. LOC_FPU:
  1279. ;
  1280. LOC_CFPUREGISTER:
  1281. begin
  1282. emit_reg(A_FLD,S_NO,
  1283. correct_fpuregister(p^.left^.location.register,fpuvaroffset));
  1284. inc(fpuvaroffset);
  1285. end;
  1286. LOC_REFERENCE,LOC_MEM:
  1287. floatload(pfloatdef(p^.left^.resulttype)^.typ,p^.left^.location.reference);
  1288. else
  1289. internalerror(309991);
  1290. end;
  1291. case p^.inlinenumber of
  1292. in_sin_extended,
  1293. in_cos_extended:
  1294. begin
  1295. getlabel(l1);
  1296. if p^.inlinenumber=in_sin_extended then
  1297. emit_none(A_FSIN,S_NO)
  1298. else
  1299. emit_none(A_FCOS,S_NO);
  1300. {
  1301. emit_reg(A_FNSTSW,S_NO,R_AX);
  1302. emit_none(A_SAHF,S_NO);
  1303. emitjmp(C_NP,l1);
  1304. emit_reg(A_FSTP,S_NO,R_ST0);
  1305. emit_none(A_FLDZ,S_NO);
  1306. emitlab(l1);
  1307. }
  1308. end;
  1309. in_arctan_extended:
  1310. begin
  1311. emit_none(A_FLD1,S_NO);
  1312. emit_none(A_FPATAN,S_NO);
  1313. end;
  1314. in_abs_extended:
  1315. emit_none(A_FABS,S_NO);
  1316. in_sqr_extended:
  1317. begin
  1318. emit_reg(A_FLD,S_NO,R_ST0);
  1319. emit_none(A_FMULP,S_NO);
  1320. end;
  1321. in_sqrt_extended:
  1322. emit_none(A_FSQRT,S_NO);
  1323. in_ln_extended:
  1324. begin
  1325. emit_none(A_FLDLN2,S_NO);
  1326. emit_none(A_FXCH,S_NO);
  1327. emit_none(A_FYL2X,S_NO);
  1328. end;
  1329. end;
  1330. end;
  1331. {$ifdef SUPPORT_MMX}
  1332. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1333. begin
  1334. if p^.left^.location.loc=LOC_REGISTER then
  1335. begin
  1336. {!!!!!!!}
  1337. end
  1338. else if p^.left^.left^.location.loc=LOC_REGISTER then
  1339. begin
  1340. {!!!!!!!}
  1341. end
  1342. else
  1343. begin
  1344. {!!!!!!!}
  1345. end;
  1346. end;
  1347. {$endif SUPPORT_MMX}
  1348. else internalerror(9);
  1349. end;
  1350. { reset pushedparasize }
  1351. pushedparasize:=oldpushedparasize;
  1352. end;
  1353. end.
  1354. {
  1355. $Log$
  1356. Revision 1.78 1999-11-09 22:54:45 peter
  1357. * fixed wrong asm with inc(qword), but not it's not correctly supported
  1358. Revision 1.77 1999/11/06 14:34:17 peter
  1359. * truncated log to 20 revs
  1360. Revision 1.76 1999/10/29 15:28:51 peter
  1361. * fixed assert, the tree is now disposed in firstpass if assertions
  1362. are off.
  1363. Revision 1.75 1999/10/26 12:30:40 peter
  1364. * const parameter is now checked
  1365. * better and generic check if a node can be used for assigning
  1366. * export fixes
  1367. * procvar equal works now (it never had worked at least from 0.99.8)
  1368. * defcoll changed to linkedlist with pparaitem so it can easily be
  1369. walked both directions
  1370. Revision 1.74 1999/10/21 16:41:38 florian
  1371. * problems with readln fixed: esi wasn't restored correctly when
  1372. reading ordinal fields of objects futher the register allocation
  1373. didn't take care of the extra register when reading ordinal values
  1374. * enumerations can now be used in constant indexes of properties
  1375. Revision 1.73 1999/09/28 20:48:23 florian
  1376. * fixed bug 610
  1377. + added $D- for TP in symtable.pas else it can't be compiled anymore
  1378. (too much symbols :()
  1379. Revision 1.72 1999/09/26 13:26:05 florian
  1380. * exception patch of Romio nevertheless the excpetion handling
  1381. needs some corections regarding register saving
  1382. * gettempansistring is again a procedure
  1383. Revision 1.71 1999/09/16 07:52:37 pierre
  1384. * FLDPI must increment fpuvaroffset
  1385. Revision 1.70 1999/09/15 20:35:38 florian
  1386. * small fix to operator overloading when in MMX mode
  1387. + the compiler uses now fldz and fld1 if possible
  1388. + some fixes to floating point registers
  1389. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  1390. * .... ???
  1391. Revision 1.69 1999/08/28 15:34:16 florian
  1392. * bug 519 fixed
  1393. Revision 1.68 1999/08/19 13:08:47 pierre
  1394. * emit_??? used
  1395. Revision 1.67 1999/08/10 13:21:08 pierre
  1396. * fpuvaroffset not increased for f32bit float type
  1397. Revision 1.66 1999/08/10 12:47:53 pierre
  1398. * fpuvaroffset problems solved
  1399. Revision 1.65 1999/08/04 00:22:47 florian
  1400. * renamed i386asm and i386base to cpuasm and cpubase
  1401. Revision 1.64 1999/08/03 22:02:42 peter
  1402. * moved bitmask constants to sets
  1403. * some other type/const renamings
  1404. Revision 1.63 1999/07/23 16:05:18 peter
  1405. * alignment is now saved in the symtable
  1406. * C alignment added for records
  1407. * PPU version increased to solve .12 <-> .13 probs
  1408. Revision 1.62 1999/07/05 20:13:10 peter
  1409. * removed temp defines
  1410. Revision 1.61 1999/07/03 14:14:27 florian
  1411. + start of val(int64/qword)
  1412. * longbool, wordbool constants weren't written, fixed
  1413. Revision 1.60 1999/07/01 15:49:09 florian
  1414. * int64/qword type release
  1415. + lo/hi for int64/qword
  1416. Revision 1.59 1999/06/21 16:33:27 jonas
  1417. * fixed include() with smallsets
  1418. Revision 1.58 1999/06/11 11:44:56 peter
  1419. *** empty log message ***
  1420. Revision 1.57 1999/06/02 10:11:43 florian
  1421. * make cycle fixed i.e. compilation with 0.99.10
  1422. * some fixes for qword
  1423. * start of register calling conventions
  1424. }