cg386inl.pas 60 KB

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