cg386inl.pas 64 KB

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