cg386inl.pas 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  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. symtable,aasm,types,
  28. hcodegen,temp_gen,pass_1,pass_2,
  29. i386base,i386asm,
  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. hregister:=getexplicitregister32(accumulator);
  67. { process dest }
  68. SecondPass(dest);
  69. if Codegenerror then
  70. exit;
  71. { store the value }
  72. Case dest^.resulttype^.deftype of
  73. floatdef:
  74. floatstore(PFloatDef(dest^.resulttype)^.typ,dest^.location.reference);
  75. orddef:
  76. begin
  77. if porddef(dest^.resulttype)^.typ in [u64bit,s64bitint] then
  78. begin
  79. emit_movq_reg_loc(R_EDX,R_EAX,dest^.location);
  80. end
  81. else
  82. begin
  83. Case dest^.resulttype^.size of
  84. 1 : hreg:=regtoreg8(hregister);
  85. 2 : hreg:=regtoreg16(hregister);
  86. 4 : hreg:=hregister;
  87. End;
  88. emit_mov_reg_loc(hreg,dest^.location);
  89. If (cs_check_range in aktlocalswitches) and
  90. {no need to rangecheck longints or cardinals on 32bit processors}
  91. not((porddef(dest^.resulttype)^.typ = s32bit) and
  92. (porddef(dest^.resulttype)^.low = $80000000) and
  93. (porddef(dest^.resulttype)^.high = $7fffffff)) and
  94. not((porddef(dest^.resulttype)^.typ = u32bit) and
  95. (porddef(dest^.resulttype)^.low = 0) and
  96. (porddef(dest^.resulttype)^.high = $ffffffff)) then
  97. Begin
  98. {do not register this temporary def}
  99. OldRegisterDef := RegisterDef;
  100. RegisterDef := False;
  101. hdef:=nil;
  102. Case PordDef(dest^.resulttype)^.typ of
  103. u8bit,u16bit,u32bit:
  104. begin
  105. new(hdef,init(u32bit,0,$ffffffff));
  106. hreg:=hregister;
  107. end;
  108. s8bit,s16bit,s32bit:
  109. begin
  110. new(hdef,init(s32bit,$80000000,$7fffffff));
  111. hreg:=hregister;
  112. end;
  113. end;
  114. { create a fake node }
  115. hp := genzeronode(nothingn);
  116. hp^.location.loc := LOC_REGISTER;
  117. hp^.location.register := hreg;
  118. if assigned(hdef) then
  119. hp^.resulttype:=hdef
  120. else
  121. hp^.resulttype:=dest^.resulttype;
  122. { emit the range check }
  123. emitrangecheck(hp,dest^.resulttype);
  124. hp^.right := nil;
  125. if assigned(hdef) then
  126. Dispose(hdef, Done);
  127. RegisterDef := OldRegisterDef;
  128. disposetree(hp);
  129. End;
  130. end;
  131. End;
  132. else
  133. internalerror(66766766);
  134. end;
  135. { free used registers }
  136. del_locref(dest^.location);
  137. ungetregister(hregister);
  138. end;
  139. procedure secondinline(var p : ptree);
  140. const
  141. {tfloattype = (s32real,s64real,s80real,s64bit,f16bit,f32bit);}
  142. { float_name: array[tfloattype] of string[8]=
  143. ('S32REAL','S64REAL','S80REAL','S64BIT','F16BIT','F32BIT'); }
  144. incdecop:array[in_inc_x..in_dec_x] of tasmop=(A_INC,A_DEC);
  145. addsubop:array[in_inc_x..in_dec_x] of tasmop=(A_ADD,A_SUB);
  146. var
  147. aktfile : treference;
  148. ft : tfiletype;
  149. opsize : topsize;
  150. op,
  151. asmop : tasmop;
  152. pushed : tpushed;
  153. {inc/dec}
  154. addconstant : boolean;
  155. addvalue : longint;
  156. procedure handlereadwrite(doread,doln : boolean);
  157. { produces code for READ(LN) and WRITE(LN) }
  158. procedure loadstream;
  159. const
  160. io:array[boolean] of string[7]=('_OUTPUT','_INPUT');
  161. var
  162. r : preference;
  163. begin
  164. new(r);
  165. reset_reference(r^);
  166. r^.symbol:=newasmsymbol('U_'+upper(target_info.system_unit)+io[doread]);
  167. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,r,R_EDI)))
  168. end;
  169. const
  170. rdwrprefix:array[boolean] of string[15]=('FPC_WRITE_TEXT_','FPC_READ_TEXT_');
  171. var
  172. node,hp : ptree;
  173. typedtyp,
  174. pararesult : pdef;
  175. orgfloattype : tfloattype;
  176. has_length : boolean;
  177. dummycoll : tdefcoll;
  178. iolabel : pasmlabel;
  179. npara : longint;
  180. begin
  181. { here we don't use register calling conventions }
  182. dummycoll.register:=R_NO;
  183. { I/O check }
  184. if (cs_check_io in aktlocalswitches) and
  185. ((aktprocsym^.definition^.options and poiocheck)=0) then
  186. begin
  187. getlabel(iolabel);
  188. emitlab(iolabel);
  189. end
  190. else
  191. iolabel:=nil;
  192. { for write of real with the length specified }
  193. has_length:=false;
  194. hp:=nil;
  195. { reserve temporary pointer to data variable }
  196. aktfile.symbol:=nil;
  197. gettempofsizereference(4,aktfile);
  198. { first state text data }
  199. ft:=ft_text;
  200. { and state a parameter ? }
  201. if p^.left=nil then
  202. begin
  203. { the following instructions are for "writeln;" }
  204. loadstream;
  205. { save @aktfile in temporary variable }
  206. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,newreference(aktfile))));
  207. end
  208. else
  209. begin
  210. { revers paramters }
  211. node:=reversparameter(p^.left);
  212. p^.left := node;
  213. npara := nb_para;
  214. { calculate data variable }
  215. { is first parameter a file type ? }
  216. if node^.left^.resulttype^.deftype=filedef then
  217. begin
  218. ft:=pfiledef(node^.left^.resulttype)^.filetype;
  219. if ft=ft_typed then
  220. typedtyp:=pfiledef(node^.left^.resulttype)^.typed_as;
  221. secondpass(node^.left);
  222. if codegenerror then
  223. exit;
  224. { save reference in temporary variables }
  225. if node^.left^.location.loc<>LOC_REFERENCE then
  226. begin
  227. CGMessage(cg_e_illegal_expression);
  228. exit;
  229. end;
  230. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(node^.left^.location.reference),R_EDI)));
  231. { skip to the next parameter }
  232. node:=node^.right;
  233. end
  234. else
  235. begin
  236. { load stdin/stdout stream }
  237. loadstream;
  238. end;
  239. { save @aktfile in temporary variable }
  240. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,newreference(aktfile))));
  241. if doread then
  242. { parameter by READ gives call by reference }
  243. dummycoll.paratyp:=vs_var
  244. { an WRITE Call by "Const" }
  245. else
  246. dummycoll.paratyp:=vs_const;
  247. { because of secondcallparan, which otherwise attaches }
  248. if ft=ft_typed then
  249. { this is to avoid copy of simple const parameters }
  250. {dummycoll.data:=new(pformaldef,init)}
  251. dummycoll.data:=cformaldef
  252. else
  253. { I think, this isn't a good solution (FK) }
  254. dummycoll.data:=nil;
  255. while assigned(node) do
  256. begin
  257. pushusedregisters(pushed,$ff);
  258. hp:=node;
  259. node:=node^.right;
  260. hp^.right:=nil;
  261. if hp^.is_colon_para then
  262. CGMessage(parser_e_illegal_colon_qualifier);
  263. { when float is written then we need bestreal to be pushed
  264. convert here else we loose the old flaot type }
  265. if (not doread) and
  266. (ft<>ft_typed) and
  267. (hp^.left^.resulttype^.deftype=floatdef) then
  268. begin
  269. orgfloattype:=pfloatdef(hp^.left^.resulttype)^.typ;
  270. hp^.left:=gentypeconvnode(hp^.left,bestrealdef^);
  271. firstpass(hp^.left);
  272. end;
  273. { when read ord,floats are functions, so they need this
  274. parameter as their destination instead of being pushed }
  275. if doread and
  276. (ft<>ft_typed) and
  277. (hp^.resulttype^.deftype in [orddef,floatdef]) then
  278. begin
  279. end
  280. else
  281. begin
  282. if ft=ft_typed then
  283. never_copy_const_param:=true;
  284. { reset data type }
  285. dummycoll.data:=nil;
  286. { create temporary defs for high tree generation }
  287. if doread and (is_shortstring(hp^.resulttype)) then
  288. dummycoll.data:=openshortstringdef
  289. else
  290. if (is_chararray(hp^.resulttype)) then
  291. dummycoll.data:=openchararraydef;
  292. secondcallparan(hp,@dummycoll,false,false,false,0);
  293. if ft=ft_typed then
  294. never_copy_const_param:=false;
  295. end;
  296. hp^.right:=node;
  297. if codegenerror then
  298. exit;
  299. emit_push_mem(aktfile);
  300. if (ft=ft_typed) then
  301. begin
  302. { OK let's try this }
  303. { first we must only allow the right type }
  304. { we have to call blockread or blockwrite }
  305. { but the real problem is that }
  306. { reset and rewrite should have set }
  307. { the type size }
  308. { as recordsize for that file !!!! }
  309. { how can we make that }
  310. { I think that is only possible by adding }
  311. { reset and rewrite to the inline list a call }
  312. { allways read only one record by element }
  313. push_int(typedtyp^.size);
  314. if doread then
  315. emitcall('FPC_TYPED_READ')
  316. else
  317. emitcall('FPC_TYPED_WRITE');
  318. end
  319. else
  320. begin
  321. { save current position }
  322. pararesult:=hp^.left^.resulttype;
  323. { handle possible field width }
  324. { of course only for write(ln) }
  325. if not doread then
  326. begin
  327. { handle total width parameter }
  328. if assigned(node) and node^.is_colon_para then
  329. begin
  330. hp:=node;
  331. node:=node^.right;
  332. hp^.right:=nil;
  333. secondcallparan(hp,@dummycoll,false,false,false,0);
  334. hp^.right:=node;
  335. if codegenerror then
  336. exit;
  337. has_length:=true;
  338. end
  339. else
  340. if pararesult^.deftype<>floatdef then
  341. push_int(0)
  342. else
  343. push_int(-32767);
  344. { a second colon para for a float ? }
  345. if assigned(node) and node^.is_colon_para then
  346. begin
  347. hp:=node;
  348. node:=node^.right;
  349. hp^.right:=nil;
  350. secondcallparan(hp,@dummycoll,false,false,false,0);
  351. hp^.right:=node;
  352. if pararesult^.deftype<>floatdef then
  353. CGMessage(parser_e_illegal_colon_qualifier);
  354. if codegenerror then
  355. exit;
  356. end
  357. else
  358. begin
  359. if pararesult^.deftype=floatdef then
  360. push_int(-1);
  361. end;
  362. { push also the real type for floats }
  363. if pararesult^.deftype=floatdef then
  364. push_int(ord(orgfloattype));
  365. end;
  366. case pararesult^.deftype of
  367. stringdef :
  368. begin
  369. emitcall(rdwrprefix[doread]+pstringdef(pararesult)^.stringtypname);
  370. end;
  371. pointerdef :
  372. begin
  373. if is_pchar(pararesult) then
  374. emitcall(rdwrprefix[doread]+'PCHAR_AS_POINTER')
  375. end;
  376. arraydef :
  377. begin
  378. if is_chararray(pararesult) then
  379. emitcall(rdwrprefix[doread]+'PCHAR_AS_ARRAY')
  380. end;
  381. floatdef :
  382. begin
  383. emitcall(rdwrprefix[doread]+'FLOAT');
  384. if doread then
  385. StoreDirectFuncResult(hp^.left);
  386. end;
  387. orddef :
  388. begin
  389. case porddef(pararesult)^.typ of
  390. s8bit,s16bit,s32bit :
  391. emitcall(rdwrprefix[doread]+'SINT');
  392. u8bit,u16bit,u32bit :
  393. emitcall(rdwrprefix[doread]+'UINT');
  394. uchar :
  395. emitcall(rdwrprefix[doread]+'CHAR');
  396. s64bitint:
  397. emitcall(rdwrprefix[doread]+'INT64');
  398. u64bit :
  399. emitcall(rdwrprefix[doread]+'QWORD');
  400. bool8bit,
  401. bool16bit,
  402. bool32bit :
  403. emitcall(rdwrprefix[doread]+'BOOLEAN');
  404. end;
  405. if doread then
  406. StoreDirectFuncResult(hp^.left);
  407. end;
  408. end;
  409. end;
  410. { load ESI in methods again }
  411. popusedregisters(pushed);
  412. maybe_loadesi;
  413. end;
  414. end;
  415. { Insert end of writing for textfiles }
  416. if ft=ft_text then
  417. begin
  418. pushusedregisters(pushed,$ff);
  419. emit_push_mem(aktfile);
  420. if doread then
  421. begin
  422. if doln then
  423. emitcall('FPC_READLN_END')
  424. else
  425. emitcall('FPC_READ_END');
  426. end
  427. else
  428. begin
  429. if doln then
  430. emitcall('FPC_WRITELN_END')
  431. else
  432. emitcall('FPC_WRITE_END');
  433. end;
  434. popusedregisters(pushed);
  435. maybe_loadesi;
  436. end;
  437. { Insert IOCheck if set }
  438. if assigned(iolabel) then
  439. begin
  440. { registers are saved in the procedure }
  441. exprasmlist^.concat(new(pai386,op_sym(A_PUSH,S_L,iolabel)));
  442. emitcall('FPC_IOCHECK');
  443. end;
  444. { Freeup all used temps }
  445. ungetiftemp(aktfile);
  446. if assigned(p^.left) then
  447. begin
  448. p^.left:=reversparameter(p^.left);
  449. if npara<>nb_para then
  450. CGMessage(cg_f_internal_error_in_secondinline);
  451. hp:=p^.left;
  452. while assigned(hp) do
  453. begin
  454. if assigned(hp^.left) then
  455. if (hp^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  456. ungetiftemp(hp^.left^.location.reference);
  457. hp:=hp^.right;
  458. end;
  459. end;
  460. end;
  461. procedure handle_str;
  462. var
  463. hp,node : ptree;
  464. dummycoll : tdefcoll;
  465. is_real,has_length : boolean;
  466. realtype : tfloattype;
  467. procedureprefix : string;
  468. begin
  469. dummycoll.register:=R_NO;
  470. pushusedregisters(pushed,$ff);
  471. node:=p^.left;
  472. is_real:=false;
  473. has_length:=false;
  474. while assigned(node^.right) do node:=node^.right;
  475. { if a real parameter somewhere then call REALSTR }
  476. if (node^.left^.resulttype^.deftype=floatdef) then
  477. begin
  478. is_real:=true;
  479. realtype:=pfloatdef(node^.left^.resulttype)^.typ;
  480. end;
  481. node:=p^.left;
  482. { we have at least two args }
  483. { with at max 2 colon_para in between }
  484. { string arg }
  485. hp:=node;
  486. node:=node^.right;
  487. hp^.right:=nil;
  488. dummycoll.paratyp:=vs_var;
  489. if is_shortstring(hp^.resulttype) then
  490. dummycoll.data:=openshortstringdef
  491. else
  492. dummycoll.data:=hp^.resulttype;
  493. procedureprefix:='FPC_'+pstringdef(hp^.resulttype)^.stringtypname+'_';
  494. secondcallparan(hp,@dummycoll,false,false,false,0);
  495. if codegenerror then
  496. exit;
  497. dummycoll.paratyp:=vs_const;
  498. disposetree(p^.left);
  499. p^.left:=nil;
  500. { second arg }
  501. hp:=node;
  502. node:=node^.right;
  503. hp^.right:=nil;
  504. { if real push real type }
  505. if is_real then
  506. push_int(ord(realtype));
  507. { frac para }
  508. if hp^.is_colon_para and assigned(node) and
  509. node^.is_colon_para then
  510. begin
  511. dummycoll.data:=hp^.resulttype;
  512. secondcallparan(hp,@dummycoll,false
  513. ,false,false,0
  514. );
  515. if codegenerror then
  516. exit;
  517. disposetree(hp);
  518. hp:=node;
  519. node:=node^.right;
  520. hp^.right:=nil;
  521. has_length:=true;
  522. end
  523. else
  524. if is_real then
  525. push_int(-1);
  526. { third arg, length only if is_real }
  527. if hp^.is_colon_para then
  528. begin
  529. dummycoll.data:=hp^.resulttype;
  530. secondcallparan(hp,@dummycoll,false
  531. ,false,false,0
  532. );
  533. if codegenerror then
  534. exit;
  535. disposetree(hp);
  536. hp:=node;
  537. node:=node^.right;
  538. hp^.right:=nil;
  539. end
  540. else
  541. if is_real then
  542. push_int(-32767)
  543. else
  544. push_int(-1);
  545. { Convert float to bestreal }
  546. if is_real then
  547. begin
  548. hp^.left:=gentypeconvnode(hp^.left,bestrealdef^);
  549. firstpass(hp^.left);
  550. end;
  551. { last arg longint or real }
  552. secondcallparan(hp,@dummycoll,false
  553. ,false,false,0
  554. );
  555. if codegenerror then
  556. exit;
  557. if is_real then
  558. emitcall(procedureprefix+'FLOAT')
  559. else
  560. case porddef(hp^.resulttype)^.typ of
  561. u32bit:
  562. emitcall(procedureprefix+'CARDINAL');
  563. u64bit:
  564. emitcall(procedureprefix+'QWORD');
  565. s64bitint:
  566. emitcall(procedureprefix+'INT64');
  567. else
  568. emitcall(procedureprefix+'LONGINT');
  569. end;
  570. disposetree(hp);
  571. popusedregisters(pushed);
  572. end;
  573. {$IfnDef OLDVAL}
  574. Procedure Handle_Val;
  575. var
  576. hp,node, code_para, dest_para : ptree;
  577. hreg,hreg2: TRegister;
  578. hdef: POrdDef;
  579. procedureprefix : string;
  580. hr, hr2: TReference;
  581. dummycoll : tdefcoll;
  582. has_code, has_32bit_code, oldregisterdef: boolean;
  583. r : preference;
  584. begin
  585. dummycoll.register:=R_NO;
  586. node:=p^.left;
  587. hp:=node;
  588. node:=node^.right;
  589. hp^.right:=nil;
  590. {if we have 3 parameters, we have a code parameter}
  591. has_code := Assigned(node^.right);
  592. has_32bit_code := false;
  593. reset_reference(hr);
  594. hreg := R_NO;
  595. If has_code then
  596. Begin
  597. {code is an orddef, that's checked in tcinl}
  598. code_para := hp;
  599. hp := node;
  600. node := node^.right;
  601. hp^.right := nil;
  602. has_32bit_code := (porddef(code_para^.left^.resulttype)^.typ in [u32bit,s32bit]);
  603. End;
  604. {hp = destination now, save for later use}
  605. dest_para := hp;
  606. {if EAX is already in use, it's a register variable. Since we don't
  607. need another register besides EAX, release the one we got}
  608. If hreg <> R_EAX Then ungetregister32(hreg);
  609. {load and push the address of the destination}
  610. dummycoll.paratyp:=vs_var;
  611. dummycoll.data:=dest_para^.resulttype;
  612. secondcallparan(dest_para,@dummycoll,false,false,false,0);
  613. if codegenerror then
  614. exit;
  615. {save the regvars}
  616. pushusedregisters(pushed,$ff);
  617. {now that we've already pushed the addres of dest_para^.left on the
  618. stack, we can put the real parameters on the stack}
  619. If has_32bit_code Then
  620. Begin
  621. dummycoll.paratyp:=vs_var;
  622. dummycoll.data:=code_para^.resulttype;
  623. secondcallparan(code_para,@dummycoll,false,false,false,0);
  624. if codegenerror then
  625. exit;
  626. Disposetree(code_para);
  627. End
  628. Else
  629. Begin
  630. {only 32bit code parameter is supported, so fake one}
  631. GetTempOfSizeReference(4,hr);
  632. emitpushreferenceaddr(hr);
  633. End;
  634. {node = first parameter = string}
  635. dummycoll.paratyp:=vs_const;
  636. dummycoll.data:=node^.resulttype;
  637. secondcallparan(node,@dummycoll,false,false,false,0);
  638. if codegenerror then
  639. exit;
  640. Case dest_para^.resulttype^.deftype of
  641. floatdef:
  642. procedureprefix := 'FPC_VAL_REAL_';
  643. orddef:
  644. if is_64bitint(dest_para^.resulttype) then
  645. begin
  646. if is_signed(dest_para^.resulttype) then
  647. procedureprefix := 'FPC_VAL_INT64_'
  648. else
  649. procedureprefix := 'FPC_VAL_QWORD_';
  650. end
  651. else
  652. begin
  653. if is_signed(dest_para^.resulttype) then
  654. begin
  655. {if we are converting to a signed number, we have to include the
  656. size of the destination, so the Val function can extend the sign
  657. of the result to allow proper range checking}
  658. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,dest_para^.resulttype^.size)));
  659. procedureprefix := 'FPC_VAL_SINT_'
  660. end
  661. else
  662. procedureprefix := 'FPC_VAL_UINT_';
  663. end;
  664. End;
  665. emitcall(procedureprefix+pstringdef(node^.resulttype)^.stringtypname);
  666. { before disposing node we need to ungettemp !! PM }
  667. if node^.left^.location.loc in [LOC_REFERENCE,LOC_MEM] then
  668. ungetiftemp(node^.left^.location.reference);
  669. disposetree(node);
  670. p^.left := nil;
  671. {reload esi in case the dest_para/code_para is a class variable or so}
  672. maybe_loadesi;
  673. If (dest_para^.resulttype^.deftype = orddef) Then
  674. Begin
  675. {store the result in a safe place, because EAX may be used by a
  676. register variable}
  677. hreg := getexplicitregister32(R_EAX);
  678. emit_reg_reg(A_MOV,S_L,R_EAX,hreg);
  679. if is_64bitint(dest_para^.resulttype) then
  680. begin
  681. hreg2:=getexplicitregister32(R_EDX);
  682. emit_reg_reg(A_MOV,S_L,R_EDX,hreg2);
  683. end;
  684. {as of now, hreg now holds the location of the result, if it was
  685. integer}
  686. End;
  687. { restore the register vars}
  688. popusedregisters(pushed);
  689. If has_code and Not(has_32bit_code) Then
  690. {only 16bit code is possible}
  691. Begin
  692. {load the address of the code parameter}
  693. secondpass(code_para^.left);
  694. {move the code to its destination}
  695. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,NewReference(hr),R_EDI)));
  696. emit_mov_reg_loc(R_DI,code_para^.left^.location);
  697. Disposetree(code_para);
  698. End;
  699. {restore the address of the result}
  700. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  701. {set up hr2 to a refernce with EDI as base register}
  702. reset_reference(hr2);
  703. hr2.base := R_EDI;
  704. {save the function result in the destination variable}
  705. Case dest_para^.left^.resulttype^.deftype of
  706. floatdef:
  707. floatstore(PFloatDef(dest_para^.left^.resulttype)^.typ, hr2);
  708. orddef:
  709. Case PordDef(dest_para^.left^.resulttype)^.typ of
  710. u8bit,s8bit:
  711. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV, S_B,
  712. RegToReg8(hreg),newreference(hr2))));
  713. u16bit,s16bit:
  714. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV, S_W,
  715. RegToReg16(hreg),newreference(hr2))));
  716. u32bit,s32bit:
  717. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV, S_L,
  718. hreg,newreference(hr2))));
  719. u64bit,s64bitint:
  720. begin
  721. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV, S_L,
  722. hreg,newreference(hr2))));
  723. r:=newreference(hr2);
  724. inc(r^.offset,4);
  725. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV, S_L,
  726. hreg2,r)));
  727. end;
  728. End;
  729. End;
  730. If (cs_check_range in aktlocalswitches) and
  731. (dest_para^.left^.resulttype^.deftype = orddef) and
  732. (not(is_64bitint(dest_para^.left^.resulttype))) and
  733. {the following has to be changed to 64bit checking, once Val
  734. returns 64 bit values (unless a special Val function is created
  735. for that)}
  736. {no need to rangecheck longints or cardinals on 32bit processors}
  737. not((porddef(dest_para^.left^.resulttype)^.typ = s32bit) and
  738. (porddef(dest_para^.left^.resulttype)^.low = $80000000) and
  739. (porddef(dest_para^.left^.resulttype)^.high = $7fffffff)) and
  740. not((porddef(dest_para^.left^.resulttype)^.typ = u32bit) and
  741. (porddef(dest_para^.left^.resulttype)^.low = 0) and
  742. (porddef(dest_para^.left^.resulttype)^.high = $ffffffff)) then
  743. Begin
  744. hp := getcopy(dest_para^.left);
  745. hp^.location.loc := LOC_REGISTER;
  746. hp^.location.register := hreg;
  747. {do not register this temporary def}
  748. OldRegisterDef := RegisterDef;
  749. RegisterDef := False;
  750. Case PordDef(dest_para^.left^.resulttype)^.typ of
  751. u8bit,u16bit,u32bit: new(hdef,init(u32bit,0,$ffffffff));
  752. s8bit,s16bit,s32bit: new(hdef,init(s32bit,$80000000,$7fffffff));
  753. end;
  754. hp^.resulttype := hdef;
  755. emitrangecheck(hp,dest_para^.left^.resulttype);
  756. hp^.right := nil;
  757. Dispose(hp^.resulttype, Done);
  758. RegisterDef := OldRegisterDef;
  759. disposetree(hp);
  760. End;
  761. {dest_para^.right is already nil}
  762. disposetree(dest_para);
  763. UnGetIfTemp(hr);
  764. end;
  765. {$EndIf OLDVAL}
  766. var
  767. r : preference;
  768. hp : ptree;
  769. l : longint;
  770. ispushed : boolean;
  771. hregister : tregister;
  772. otlabel,oflabel : pasmlabel;
  773. oldpushedparasize : longint;
  774. begin
  775. { save & reset pushedparasize }
  776. oldpushedparasize:=pushedparasize;
  777. pushedparasize:=0;
  778. case p^.inlinenumber of
  779. in_assert_x_y:
  780. begin
  781. otlabel:=truelabel;
  782. oflabel:=falselabel;
  783. getlabel(truelabel);
  784. getlabel(falselabel);
  785. secondpass(p^.left^.left);
  786. if cs_do_assertion in aktlocalswitches then
  787. begin
  788. maketojumpbool(p^.left^.left);
  789. emitlab(falselabel);
  790. { erroraddr }
  791. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EBP)));
  792. { lineno }
  793. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,aktfilepos.line)));
  794. { filename string }
  795. hp:=genstringconstnode(current_module^.sourcefiles^.get_file_name(aktfilepos.fileindex));
  796. secondpass(hp);
  797. if codegenerror then
  798. exit;
  799. emitpushreferenceaddr(hp^.location.reference);
  800. disposetree(hp);
  801. { push msg }
  802. secondpass(p^.left^.right^.left);
  803. emitpushreferenceaddr(p^.left^.right^.left^.location.reference);
  804. { call }
  805. emitcall('FPC_ASSERT');
  806. emitlab(truelabel);
  807. end;
  808. freelabel(truelabel);
  809. freelabel(falselabel);
  810. truelabel:=otlabel;
  811. falselabel:=oflabel;
  812. end;
  813. in_lo_word,
  814. in_hi_word :
  815. begin
  816. secondpass(p^.left);
  817. p^.location.loc:=LOC_REGISTER;
  818. if p^.left^.location.loc<>LOC_REGISTER then
  819. begin
  820. if p^.left^.location.loc=LOC_CREGISTER then
  821. begin
  822. p^.location.register:=reg32toreg16(getregister32);
  823. emit_reg_reg(A_MOV,S_W,p^.left^.location.register,
  824. p^.location.register);
  825. end
  826. else
  827. begin
  828. del_reference(p^.left^.location.reference);
  829. p^.location.register:=reg32toreg16(getregister32);
  830. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,newreference(p^.left^.location.reference),
  831. p^.location.register)));
  832. end;
  833. end
  834. else p^.location.register:=p^.left^.location.register;
  835. if p^.inlinenumber=in_hi_word then
  836. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_W,8,p^.location.register)));
  837. p^.location.register:=reg16toreg8(p^.location.register);
  838. end;
  839. in_sizeof_x,
  840. in_typeof_x :
  841. begin
  842. { for both cases load vmt }
  843. if p^.left^.treetype=typen then
  844. begin
  845. p^.location.register:=getregister32;
  846. exprasmlist^.concat(new(pai386,op_sym_ofs_reg(A_MOV,
  847. S_L,newasmsymbol(pobjectdef(p^.left^.resulttype)^.vmt_mangledname),0,
  848. p^.location.register)));
  849. end
  850. else
  851. begin
  852. secondpass(p^.left);
  853. del_reference(p^.left^.location.reference);
  854. p^.location.loc:=LOC_REGISTER;
  855. p^.location.register:=getregister32;
  856. { load VMT pointer }
  857. inc(p^.left^.location.reference.offset,
  858. pobjectdef(p^.left^.resulttype)^.vmt_offset);
  859. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  860. newreference(p^.left^.location.reference),
  861. p^.location.register)));
  862. end;
  863. { in sizeof load size }
  864. if p^.inlinenumber=in_sizeof_x then
  865. begin
  866. new(r);
  867. reset_reference(r^);
  868. r^.base:=p^.location.register;
  869. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,
  870. p^.location.register)));
  871. end;
  872. end;
  873. in_lo_long,
  874. in_hi_long :
  875. begin
  876. secondpass(p^.left);
  877. p^.location.loc:=LOC_REGISTER;
  878. if p^.left^.location.loc<>LOC_REGISTER then
  879. begin
  880. if p^.left^.location.loc=LOC_CREGISTER then
  881. begin
  882. p^.location.register:=getregister32;
  883. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,
  884. p^.location.register);
  885. end
  886. else
  887. begin
  888. del_reference(p^.left^.location.reference);
  889. p^.location.register:=getregister32;
  890. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  891. p^.location.register)));
  892. end;
  893. end
  894. else p^.location.register:=p^.left^.location.register;
  895. if p^.inlinenumber=in_hi_long then
  896. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_L,16,p^.location.register)));
  897. p^.location.register:=reg32toreg16(p^.location.register);
  898. end;
  899. in_lo_qword,
  900. in_hi_qword:
  901. begin
  902. secondpass(p^.left);
  903. p^.location.loc:=LOC_REGISTER;
  904. case p^.left^.location.loc of
  905. LOC_CREGISTER:
  906. begin
  907. p^.location.register:=getregister32;
  908. if p^.inlinenumber=in_hi_qword then
  909. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerhigh,
  910. p^.location.register)
  911. else
  912. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerlow,
  913. p^.location.register)
  914. end;
  915. LOC_MEM,LOC_REFERENCE:
  916. begin
  917. del_reference(p^.left^.location.reference);
  918. p^.location.register:=getregister32;
  919. r:=newreference(p^.left^.location.reference);
  920. if p^.inlinenumber=in_hi_qword then
  921. inc(r^.offset,4);
  922. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  923. r,p^.location.register)));
  924. end;
  925. LOC_REGISTER:
  926. begin
  927. if p^.inlinenumber=in_hi_qword then
  928. begin
  929. p^.location.register:=p^.left^.location.registerhigh;
  930. ungetregister32(p^.left^.location.registerlow);
  931. end
  932. else
  933. begin
  934. p^.location.register:=p^.left^.location.registerlow;
  935. ungetregister32(p^.left^.location.registerhigh);
  936. end;
  937. end;
  938. end;
  939. end;
  940. in_length_string :
  941. begin
  942. secondpass(p^.left);
  943. set_location(p^.location,p^.left^.location);
  944. { length in ansi strings is at offset -8 }
  945. if is_ansistring(p^.left^.resulttype) then
  946. dec(p^.location.reference.offset,8)
  947. { char is always 1, so make it a constant value }
  948. else if is_char(p^.left^.resulttype) then
  949. begin
  950. clear_location(p^.location);
  951. p^.location.loc:=LOC_MEM;
  952. p^.location.reference.is_immediate:=true;
  953. p^.location.reference.offset:=1;
  954. end;
  955. end;
  956. in_pred_x,
  957. in_succ_x:
  958. begin
  959. secondpass(p^.left);
  960. if not (cs_check_overflow in aktlocalswitches) then
  961. if p^.inlinenumber=in_pred_x then
  962. asmop:=A_DEC
  963. else
  964. asmop:=A_INC
  965. else
  966. if p^.inlinenumber=in_pred_x then
  967. asmop:=A_SUB
  968. else
  969. asmop:=A_ADD;
  970. case p^.resulttype^.size of
  971. 4 : opsize:=S_L;
  972. 2 : opsize:=S_W;
  973. 1 : opsize:=S_B;
  974. else
  975. internalerror(10080);
  976. end;
  977. p^.location.loc:=LOC_REGISTER;
  978. if p^.left^.location.loc<>LOC_REGISTER then
  979. begin
  980. p^.location.register:=getregister32;
  981. if (p^.resulttype^.size=2) then
  982. p^.location.register:=reg32toreg16(p^.location.register);
  983. if (p^.resulttype^.size=1) then
  984. p^.location.register:=reg32toreg8(p^.location.register);
  985. if p^.left^.location.loc=LOC_CREGISTER then
  986. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,
  987. p^.location.register)
  988. else
  989. if p^.left^.location.loc=LOC_FLAGS then
  990. emit_flag2reg(p^.left^.location.resflags,p^.location.register)
  991. else
  992. begin
  993. del_reference(p^.left^.location.reference);
  994. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(p^.left^.location.reference),
  995. p^.location.register)));
  996. end;
  997. end
  998. else p^.location.register:=p^.left^.location.register;
  999. if not (cs_check_overflow in aktlocalswitches) then
  1000. exprasmlist^.concat(new(pai386,op_reg(asmop,opsize,
  1001. p^.location.register)))
  1002. else
  1003. exprasmlist^.concat(new(pai386,op_const_reg(asmop,opsize,1,
  1004. p^.location.register)));
  1005. emitoverflowcheck(p);
  1006. emitrangecheck(p,p^.resulttype);
  1007. end;
  1008. in_dec_x,
  1009. in_inc_x :
  1010. begin
  1011. { set defaults }
  1012. addvalue:=1;
  1013. addconstant:=true;
  1014. { load first parameter, must be a reference }
  1015. secondpass(p^.left^.left);
  1016. case p^.left^.left^.resulttype^.deftype of
  1017. orddef,
  1018. enumdef : begin
  1019. case p^.left^.left^.resulttype^.size of
  1020. 1 : opsize:=S_B;
  1021. 2 : opsize:=S_W;
  1022. 4 : opsize:=S_L;
  1023. end;
  1024. end;
  1025. pointerdef : begin
  1026. opsize:=S_L;
  1027. if porddef(ppointerdef(p^.left^.left^.resulttype)^.definition)=voiddef then
  1028. addvalue:=1
  1029. else
  1030. addvalue:=ppointerdef(p^.left^.left^.resulttype)^.definition^.savesize;
  1031. end;
  1032. else
  1033. internalerror(10081);
  1034. end;
  1035. { second argument specified?, must be a s32bit in register }
  1036. if assigned(p^.left^.right) then
  1037. begin
  1038. secondpass(p^.left^.right^.left);
  1039. { when constant, just multiply the addvalue }
  1040. if is_constintnode(p^.left^.right^.left) then
  1041. addvalue:=addvalue*get_ordinal_value(p^.left^.right^.left)
  1042. else
  1043. begin
  1044. case p^.left^.right^.left^.location.loc of
  1045. LOC_REGISTER,
  1046. LOC_CREGISTER : hregister:=p^.left^.right^.left^.location.register;
  1047. LOC_MEM,
  1048. LOC_REFERENCE : begin
  1049. del_reference(p^.left^.right^.left^.location.reference);
  1050. hregister:=getregister32;
  1051. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1052. newreference(p^.left^.right^.left^.location.reference),hregister)));
  1053. end;
  1054. else
  1055. internalerror(10082);
  1056. end;
  1057. { insert multiply with addvalue if its >1 }
  1058. if addvalue>1 then
  1059. exprasmlist^.concat(new(pai386,op_const_reg(A_IMUL,opsize,
  1060. addvalue,hregister)));
  1061. addconstant:=false;
  1062. end;
  1063. end;
  1064. { write the add instruction }
  1065. if addconstant then
  1066. begin
  1067. if (addvalue=1) and not(cs_check_overflow in aktlocalswitches) then
  1068. begin
  1069. if p^.left^.left^.location.loc=LOC_CREGISTER then
  1070. exprasmlist^.concat(new(pai386,op_reg(incdecop[p^.inlinenumber],opsize,
  1071. p^.left^.left^.location.register)))
  1072. else
  1073. exprasmlist^.concat(new(pai386,op_ref(incdecop[p^.inlinenumber],opsize,
  1074. newreference(p^.left^.left^.location.reference))))
  1075. end
  1076. else
  1077. begin
  1078. if p^.left^.left^.location.loc=LOC_CREGISTER then
  1079. exprasmlist^.concat(new(pai386,op_const_reg(addsubop[p^.inlinenumber],opsize,
  1080. addvalue,p^.left^.left^.location.register)))
  1081. else
  1082. exprasmlist^.concat(new(pai386,op_const_ref(addsubop[p^.inlinenumber],opsize,
  1083. addvalue,newreference(p^.left^.left^.location.reference))));
  1084. end
  1085. end
  1086. else
  1087. begin
  1088. { BUG HERE : detected with nasm :
  1089. hregister is allways 32 bit
  1090. it should be converted to 16 or 8 bit depending on op_size PM }
  1091. { still not perfect :
  1092. if hregister is already a 16 bit reg ?? PM }
  1093. { makeregXX is the solution (FK) }
  1094. case opsize of
  1095. S_B : hregister:=makereg8(hregister);
  1096. S_W : hregister:=makereg16(hregister);
  1097. end;
  1098. if p^.left^.left^.location.loc=LOC_CREGISTER then
  1099. exprasmlist^.concat(new(pai386,op_reg_reg(addsubop[p^.inlinenumber],opsize,
  1100. hregister,p^.left^.left^.location.register)))
  1101. else
  1102. exprasmlist^.concat(new(pai386,op_reg_ref(addsubop[p^.inlinenumber],opsize,
  1103. hregister,newreference(p^.left^.left^.location.reference))));
  1104. case opsize of
  1105. S_B : hregister:=reg8toreg32(hregister);
  1106. S_W : hregister:=reg16toreg32(hregister);
  1107. end;
  1108. ungetregister32(hregister);
  1109. end;
  1110. emitoverflowcheck(p^.left^.left);
  1111. emitrangecheck(p^.left^.left,p^.left^.left^.resulttype);
  1112. end;
  1113. in_assigned_x :
  1114. begin
  1115. secondpass(p^.left^.left);
  1116. p^.location.loc:=LOC_FLAGS;
  1117. if (p^.left^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1118. begin
  1119. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,
  1120. p^.left^.left^.location.register,
  1121. p^.left^.left^.location.register)));
  1122. ungetregister32(p^.left^.left^.location.register);
  1123. end
  1124. else
  1125. begin
  1126. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_L,0,
  1127. newreference(p^.left^.left^.location.reference))));
  1128. del_reference(p^.left^.left^.location.reference);
  1129. end;
  1130. p^.location.resflags:=F_NE;
  1131. end;
  1132. in_reset_typedfile,in_rewrite_typedfile :
  1133. begin
  1134. pushusedregisters(pushed,$ff);
  1135. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,pfiledef(p^.left^.resulttype)^.typed_as^.size)));
  1136. secondpass(p^.left);
  1137. emitpushreferenceaddr(p^.left^.location.reference);
  1138. if p^.inlinenumber=in_reset_typedfile then
  1139. emitcall('FPC_RESET_TYPED')
  1140. else
  1141. emitcall('FPC_REWRITE_TYPED');
  1142. popusedregisters(pushed);
  1143. end;
  1144. in_write_x :
  1145. handlereadwrite(false,false);
  1146. in_writeln_x :
  1147. handlereadwrite(false,true);
  1148. in_read_x :
  1149. handlereadwrite(true,false);
  1150. in_readln_x :
  1151. handlereadwrite(true,true);
  1152. in_str_x_string :
  1153. begin
  1154. handle_str;
  1155. maybe_loadesi;
  1156. end;
  1157. {$IfnDef OLDVAL}
  1158. in_val_x :
  1159. Begin
  1160. handle_val;
  1161. End;
  1162. {$EndIf OLDVAL}
  1163. in_include_x_y,
  1164. in_exclude_x_y:
  1165. begin
  1166. secondpass(p^.left^.left);
  1167. if p^.left^.right^.left^.treetype=ordconstn then
  1168. begin
  1169. { calculate bit position }
  1170. l:=1 shl (p^.left^.right^.left^.value mod 32);
  1171. { determine operator }
  1172. if p^.inlinenumber=in_include_x_y then
  1173. asmop:=A_OR
  1174. else
  1175. begin
  1176. asmop:=A_AND;
  1177. l:=not(l);
  1178. end;
  1179. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  1180. begin
  1181. inc(p^.left^.left^.location.reference.offset,(p^.left^.right^.left^.value div 32)*4);
  1182. exprasmlist^.concat(new(pai386,op_const_ref(asmop,S_L,
  1183. l,newreference(p^.left^.left^.location.reference))));
  1184. del_reference(p^.left^.left^.location.reference);
  1185. end
  1186. else
  1187. { LOC_CREGISTER }
  1188. exprasmlist^.concat(new(pai386,op_const_reg(asmop,S_L,
  1189. l,p^.left^.left^.location.register)));
  1190. end
  1191. else
  1192. begin
  1193. { generate code for the element to set }
  1194. ispushed:=maybe_push(p^.left^.right^.left^.registers32,p^.left^.left,false);
  1195. secondpass(p^.left^.right^.left);
  1196. if ispushed then
  1197. restore(p^.left^.left,false);
  1198. { determine asm operator }
  1199. if p^.inlinenumber=in_include_x_y then
  1200. asmop:=A_BTS
  1201. else
  1202. asmop:=A_BTR;
  1203. if psetdef(p^.left^.resulttype)^.settype=smallset then
  1204. begin
  1205. if p^.left^.right^.left^.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  1206. hregister:=p^.left^.right^.left^.location.register
  1207. else
  1208. begin
  1209. hregister:=R_EDI;
  1210. opsize:=def2def_opsize(p^.left^.right^.left^.resulttype,u32bitdef);
  1211. if opsize in [S_B,S_W,S_L] then
  1212. op:=A_MOV
  1213. else
  1214. op:=A_MOVZX;
  1215. exprasmlist^.concat(new(pai386,op_ref_reg(op,opsize,
  1216. newreference(p^.left^.right^.left^.location.reference),R_EDI)));
  1217. end;
  1218. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  1219. exprasmlist^.concat(new(pai386,op_reg_ref(asmop,S_L,hregister,
  1220. newreference(p^.left^.left^.location.reference))))
  1221. else
  1222. exprasmlist^.concat(new(pai386,op_reg_reg(asmop,S_L,hregister,
  1223. p^.left^.left^.location.register)));
  1224. end
  1225. else
  1226. begin
  1227. pushsetelement(p^.left^.right^.left);
  1228. { normset is allways a ref }
  1229. emitpushreferenceaddr(p^.left^.left^.location.reference);
  1230. if p^.inlinenumber=in_include_x_y then
  1231. emitcall('FPC_SET_SET_BYTE')
  1232. else
  1233. emitcall('FPC_SET_UNSET_BYTE');
  1234. {CGMessage(cg_e_include_not_implemented);}
  1235. end;
  1236. end;
  1237. end;
  1238. else internalerror(9);
  1239. end;
  1240. { reset pushedparasize }
  1241. pushedparasize:=oldpushedparasize;
  1242. end;
  1243. end.
  1244. {
  1245. $Log$
  1246. Revision 1.61 1999-07-03 14:14:27 florian
  1247. + start of val(int64/qword)
  1248. * longbool, wordbool constants weren't written, fixed
  1249. Revision 1.60 1999/07/01 15:49:09 florian
  1250. * int64/qword type release
  1251. + lo/hi for int64/qword
  1252. Revision 1.59 1999/06/21 16:33:27 jonas
  1253. * fixed include() with smallsets
  1254. Revision 1.58 1999/06/11 11:44:56 peter
  1255. *** empty log message ***
  1256. Revision 1.57 1999/06/02 10:11:43 florian
  1257. * make cycle fixed i.e. compilation with 0.99.10
  1258. * some fixes for qword
  1259. * start of register calling conventions
  1260. Revision 1.56 1999/05/31 12:43:32 peter
  1261. * fixed register allocation for storefuncresult
  1262. Revision 1.55 1999/05/27 19:44:13 peter
  1263. * removed oldasm
  1264. * plabel -> pasmlabel
  1265. * -a switches to source writing automaticly
  1266. * assembler readers OOPed
  1267. * asmsymbol automaticly external
  1268. * jumptables and other label fixes for asm readers
  1269. Revision 1.54 1999/05/23 19:55:11 florian
  1270. * qword/int64 multiplication fixed
  1271. + qword/int64 subtraction
  1272. Revision 1.53 1999/05/23 18:42:01 florian
  1273. * better error recovering in typed constants
  1274. * some problems with arrays of const fixed, some problems
  1275. due my previous
  1276. - the location type of array constructor is now LOC_MEM
  1277. - the pushing of high fixed
  1278. - parameter copying fixed
  1279. - zero temp. allocation removed
  1280. * small problem in the assembler writers fixed:
  1281. ref to nil wasn't written correctly
  1282. Revision 1.52 1999/05/21 13:54:50 peter
  1283. * NEWLAB for label as symbol
  1284. Revision 1.51 1999/05/18 21:58:27 florian
  1285. * fixed some bugs related to temp. ansistrings and functions results
  1286. which return records/objects/arrays which need init/final.
  1287. Revision 1.50 1999/05/17 21:57:03 florian
  1288. * new temporary ansistring handling
  1289. Revision 1.49 1999/05/12 15:46:26 pierre
  1290. * handle_str disposetree was badly placed
  1291. Revision 1.48 1999/05/12 00:19:42 peter
  1292. * removed R_DEFAULT_SEG
  1293. * uniform float names
  1294. Revision 1.47 1999/05/06 09:05:13 peter
  1295. * generic write_float and str_float
  1296. * fixed constant float conversions
  1297. Revision 1.46 1999/05/05 16:18:20 jonas
  1298. * changes to handle_val so register vars are pushed/poped only once
  1299. Revision 1.45 1999/05/01 13:24:08 peter
  1300. * merged nasm compiler
  1301. * old asm moved to oldasm/
  1302. Revision 1.44 1999/04/26 18:28:13 peter
  1303. * better read/write array
  1304. Revision 1.43 1999/04/19 09:45:48 pierre
  1305. + cdecl or stdcall push all args with longint size
  1306. * tempansi stuff cleaned up
  1307. Revision 1.42 1999/04/14 09:11:59 peter
  1308. * fixed include
  1309. Revision 1.41 1999/04/08 23:59:49 pierre
  1310. * temp string for val code freed
  1311. Revision 1.40 1999/04/08 15:57:46 peter
  1312. + subrange checking for readln()
  1313. Revision 1.39 1999/04/07 15:31:16 pierre
  1314. * all formaldefs are now a sinlge definition
  1315. cformaldef (this was necessary for double_checksum)
  1316. + small part of double_checksum code
  1317. Revision 1.38 1999/04/05 11:07:26 jonas
  1318. * fixed some typos in the constants of the range checking for Val
  1319. Revision 1.37 1999/04/01 22:07:51 peter
  1320. * universal string names (ansistr instead of stransi) for val/str
  1321. Revision 1.36 1999/04/01 06:21:04 jonas
  1322. * added initialization for has_32bit_code (caused problems with Val statement
  1323. without code parameter)
  1324. Revision 1.35 1999/03/31 20:30:49 michael
  1325. * fixed typo: odlval to oldval
  1326. Revision 1.34 1999/03/31 17:13:09 jonas
  1327. * bugfix for -Ox with internal val code
  1328. * internal val code now requires less free registers
  1329. * internal val code no longer needs a temp var for range checking
  1330. Revision 1.33 1999/03/26 00:24:15 peter
  1331. * last para changed to long for easier pushing with 4 byte aligns
  1332. Revision 1.32 1999/03/26 00:05:26 peter
  1333. * released valintern
  1334. + deffile is now removed when compiling is finished
  1335. * ^( compiles now correct
  1336. + static directive
  1337. * shrd fixed
  1338. Revision 1.31 1999/03/24 23:16:49 peter
  1339. * fixed bugs 212,222,225,227,229,231,233
  1340. Revision 1.30 1999/03/16 17:52:56 jonas
  1341. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  1342. * in cgi386inl: also range checking for subrange types (compile with "-dreadrangecheck")
  1343. * in cgai386: also small fixes to emitrangecheck
  1344. Revision 1.29 1999/02/25 21:02:27 peter
  1345. * ag386bin updates
  1346. + coff writer
  1347. Revision 1.28 1999/02/22 02:15:11 peter
  1348. * updates for ag386bin
  1349. Revision 1.27 1999/02/17 14:21:40 pierre
  1350. * unused local removed
  1351. Revision 1.26 1999/02/15 11:40:21 pierre
  1352. * pred/succ with overflow check must use ADD DEC !!
  1353. Revision 1.25 1999/02/05 10:56:19 florian
  1354. * in some cases a writeln of temp. ansistrings cause a memory leak, fixed
  1355. Revision 1.24 1999/01/21 22:10:39 peter
  1356. * fixed array of const
  1357. * generic platform independent high() support
  1358. Revision 1.23 1999/01/06 12:23:29 florian
  1359. * str(...) for ansi/long and widestrings fixed
  1360. Revision 1.22 1998/12/11 23:36:07 florian
  1361. + again more stuff for int64/qword:
  1362. - comparision operators
  1363. - code generation for: str, read(ln), write(ln)
  1364. Revision 1.21 1998/12/11 00:02:50 peter
  1365. + globtype,tokens,version unit splitted from globals
  1366. Revision 1.20 1998/11/27 14:50:32 peter
  1367. + open strings, $P switch support
  1368. Revision 1.19 1998/11/26 13:10:40 peter
  1369. * new int - int conversion -dNEWCNV
  1370. * some function renamings
  1371. Revision 1.18 1998/11/24 17:04:27 peter
  1372. * fixed length(char) when char is a variable
  1373. Revision 1.17 1998/11/05 12:02:33 peter
  1374. * released useansistring
  1375. * removed -Sv, its now available in fpc modes
  1376. Revision 1.16 1998/10/22 17:11:13 pierre
  1377. + terminated the include exclude implementation for i386
  1378. * enums inside records fixed
  1379. Revision 1.15 1998/10/21 15:12:50 pierre
  1380. * bug fix for IOCHECK inside a procedure with iocheck modifier
  1381. * removed the GPF for unexistant overloading
  1382. (firstcall was called with procedinition=nil !)
  1383. * changed typen to what Florian proposed
  1384. gentypenode(p : pdef) sets the typenodetype field
  1385. and resulttype is only set if inside bt_type block !
  1386. Revision 1.14 1998/10/20 08:06:40 pierre
  1387. * several memory corruptions due to double freemem solved
  1388. => never use p^.loc.location:=p^.left^.loc.location;
  1389. + finally I added now by default
  1390. that ra386dir translates global and unit symbols
  1391. + added a first field in tsymtable and
  1392. a nextsym field in tsym
  1393. (this allows to obtain ordered type info for
  1394. records and objects in gdb !)
  1395. Revision 1.13 1998/10/13 16:50:02 pierre
  1396. * undid some changes of Peter that made the compiler wrong
  1397. for m68k (I had to reinsert some ifdefs)
  1398. * removed several memory leaks under m68k
  1399. * removed the meory leaks for assembler readers
  1400. * cross compiling shoud work again better
  1401. ( crosscompiling sysamiga works
  1402. but as68k still complain about some code !)
  1403. Revision 1.12 1998/10/08 17:17:12 pierre
  1404. * current_module old scanner tagged as invalid if unit is recompiled
  1405. + added ppheap for better info on tracegetmem of heaptrc
  1406. (adds line column and file index)
  1407. * several memory leaks removed ith help of heaptrc !!
  1408. Revision 1.11 1998/10/05 21:33:15 peter
  1409. * fixed 161,165,166,167,168
  1410. Revision 1.10 1998/10/05 12:32:44 peter
  1411. + assert() support
  1412. Revision 1.8 1998/10/02 10:35:09 peter
  1413. * support for inc(pointer,value) which now increases with value instead
  1414. of 0*value :)
  1415. Revision 1.7 1998/09/21 08:45:07 pierre
  1416. + added vmt_offset in tobjectdef.write for fututre use
  1417. (first steps to have objects without vmt if no virtual !!)
  1418. + added fpu_used field for tabstractprocdef :
  1419. sets this level to 2 if the functions return with value in FPU
  1420. (is then set to correct value at parsing of implementation)
  1421. THIS MIGHT refuse some code with FPU expression too complex
  1422. that were accepted before and even in some cases
  1423. that don't overflow in fact
  1424. ( like if f : float; is a forward that finally in implementation
  1425. only uses one fpu register !!)
  1426. Nevertheless I think that it will improve security on
  1427. FPU operations !!
  1428. * most other changes only for UseBrowser code
  1429. (added symtable references for record and objects)
  1430. local switch for refs to args and local of each function
  1431. (static symtable still missing)
  1432. UseBrowser still not stable and probably broken by
  1433. the definition hash array !!
  1434. Revision 1.6 1998/09/20 12:26:37 peter
  1435. * merged fixes
  1436. Revision 1.5 1998/09/17 09:42:15 peter
  1437. + pass_2 for cg386
  1438. * Message() -> CGMessage() for pass_1/pass_2
  1439. Revision 1.4 1998/09/14 10:43:49 peter
  1440. * all internal RTL functions start with FPC_
  1441. Revision 1.3.2.1 1998/09/20 12:20:07 peter
  1442. * Fixed stack not on 4 byte boundary when doing a call
  1443. Revision 1.3 1998/09/05 23:03:57 florian
  1444. * some fixes to get -Or work:
  1445. - inc/dec didn't take care of CREGISTER
  1446. - register calculcation of inc/dec was wrong
  1447. - var/const parameters get now assigned 32 bit register, but
  1448. const parameters only if they are passed by reference !
  1449. Revision 1.2 1998/09/04 08:41:40 peter
  1450. * updated some error CGMessages
  1451. Revision 1.1 1998/08/31 12:22:14 peter
  1452. * secondinline moved to cg386inl
  1453. Revision 1.19 1998/08/31 08:52:03 peter
  1454. * fixed error 10 with succ() and pref()
  1455. Revision 1.18 1998/08/20 21:36:38 peter
  1456. * fixed 'with object do' bug
  1457. Revision 1.17 1998/08/19 16:07:36 jonas
  1458. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1459. Revision 1.16 1998/08/18 09:24:36 pierre
  1460. * small warning position bug fixed
  1461. * support_mmx switches splitting was missing
  1462. * rhide error and warning output corrected
  1463. Revision 1.15 1998/08/13 11:00:09 peter
  1464. * fixed procedure<>procedure construct
  1465. Revision 1.14 1998/08/11 14:05:33 peter
  1466. * fixed sizeof(array of char)
  1467. Revision 1.13 1998/08/10 14:49:45 peter
  1468. + localswitches, moduleswitches, globalswitches splitting
  1469. Revision 1.12 1998/07/30 13:30:31 florian
  1470. * final implemenation of exception support, maybe it needs
  1471. some fixes :)
  1472. Revision 1.11 1998/07/24 22:16:52 florian
  1473. * internal error 10 together with array access fixed. I hope
  1474. that's the final fix.
  1475. Revision 1.10 1998/07/18 22:54:23 florian
  1476. * some ansi/wide/longstring support fixed:
  1477. o parameter passing
  1478. o returning as result from functions
  1479. Revision 1.9 1998/07/07 17:40:37 peter
  1480. * packrecords 4 works
  1481. * word aligning of parameters
  1482. Revision 1.8 1998/07/06 15:51:15 michael
  1483. Added length checking for string reading
  1484. Revision 1.7 1998/07/06 14:19:51 michael
  1485. + Added calls for reading/writing ansistrings
  1486. Revision 1.6 1998/07/01 15:28:48 peter
  1487. + better writeln/readln handling, now 100% like tp7
  1488. Revision 1.5 1998/06/25 14:04:17 peter
  1489. + internal inc/dec
  1490. Revision 1.4 1998/06/25 08:48:06 florian
  1491. * first version of rtti support
  1492. Revision 1.3 1998/06/09 16:01:33 pierre
  1493. + added procedure directive parsing for procvars
  1494. (accepted are popstack cdecl and pascal)
  1495. + added C vars with the following syntax
  1496. var C calias 'true_c_name';(can be followed by external)
  1497. reason is that you must add the Cprefix
  1498. which is target dependent
  1499. Revision 1.2 1998/06/08 13:13:29 pierre
  1500. + temporary variables now in temp_gen.pas unit
  1501. because it is processor independent
  1502. * mppc68k.bat modified to undefine i386 and support_mmx
  1503. (which are defaults for i386)
  1504. Revision 1.1 1998/06/05 17:44:10 peter
  1505. * splitted cgi386
  1506. }