cg386inl.pas 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 inline nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cg386inl;
  19. interface
  20. uses
  21. tree;
  22. procedure secondinline(var p : ptree);
  23. implementation
  24. uses
  25. globtype,systems,
  26. cobjects,verbose,globals,files,
  27. symconst,symtable,aasm,types,
  28. hcodegen,temp_gen,pass_1,pass_2,
  29. 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,s64bit] 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. not(po_iocheck in aktprocsym^.definition^.procoptions) 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. s64bit :
  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. s64bit:
  566. emitcall(procedureprefix+'INT64');
  567. else
  568. emitcall(procedureprefix+'LONGINT');
  569. end;
  570. disposetree(hp);
  571. popusedregisters(pushed);
  572. end;
  573. Procedure Handle_Val;
  574. var
  575. hp,node, code_para, dest_para : ptree;
  576. hreg,hreg2: TRegister;
  577. hdef: POrdDef;
  578. procedureprefix : string;
  579. hr, hr2: TReference;
  580. dummycoll : tdefcoll;
  581. has_code, has_32bit_code, oldregisterdef: boolean;
  582. r : preference;
  583. begin
  584. dummycoll.register:=R_NO;
  585. node:=p^.left;
  586. hp:=node;
  587. node:=node^.right;
  588. hp^.right:=nil;
  589. {if we have 3 parameters, we have a code parameter}
  590. has_code := Assigned(node^.right);
  591. has_32bit_code := false;
  592. reset_reference(hr);
  593. hreg := R_NO;
  594. If has_code then
  595. Begin
  596. {code is an orddef, that's checked in tcinl}
  597. code_para := hp;
  598. hp := node;
  599. node := node^.right;
  600. hp^.right := nil;
  601. has_32bit_code := (porddef(code_para^.left^.resulttype)^.typ in [u32bit,s32bit]);
  602. End;
  603. {hp = destination now, save for later use}
  604. dest_para := hp;
  605. {if EAX is already in use, it's a register variable. Since we don't
  606. need another register besides EAX, release the one we got}
  607. If hreg <> R_EAX Then ungetregister32(hreg);
  608. {load and push the address of the destination}
  609. dummycoll.paratyp:=vs_var;
  610. dummycoll.data:=dest_para^.resulttype;
  611. secondcallparan(dest_para,@dummycoll,false,false,false,0);
  612. if codegenerror then
  613. exit;
  614. {save the regvars}
  615. pushusedregisters(pushed,$ff);
  616. {now that we've already pushed the addres of dest_para^.left on the
  617. stack, we can put the real parameters on the stack}
  618. If has_32bit_code Then
  619. Begin
  620. dummycoll.paratyp:=vs_var;
  621. dummycoll.data:=code_para^.resulttype;
  622. secondcallparan(code_para,@dummycoll,false,false,false,0);
  623. if codegenerror then
  624. exit;
  625. Disposetree(code_para);
  626. End
  627. Else
  628. Begin
  629. {only 32bit code parameter is supported, so fake one}
  630. GetTempOfSizeReference(4,hr);
  631. emitpushreferenceaddr(hr);
  632. End;
  633. {node = first parameter = string}
  634. dummycoll.paratyp:=vs_const;
  635. dummycoll.data:=node^.resulttype;
  636. secondcallparan(node,@dummycoll,false,false,false,0);
  637. if codegenerror then
  638. exit;
  639. Case dest_para^.resulttype^.deftype of
  640. floatdef:
  641. procedureprefix := 'FPC_VAL_REAL_';
  642. orddef:
  643. if is_64bitint(dest_para^.resulttype) then
  644. begin
  645. if is_signed(dest_para^.resulttype) then
  646. procedureprefix := 'FPC_VAL_INT64_'
  647. else
  648. procedureprefix := 'FPC_VAL_QWORD_';
  649. end
  650. else
  651. begin
  652. if is_signed(dest_para^.resulttype) then
  653. begin
  654. {if we are converting to a signed number, we have to include the
  655. size of the destination, so the Val function can extend the sign
  656. of the result to allow proper range checking}
  657. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,dest_para^.resulttype^.size)));
  658. procedureprefix := 'FPC_VAL_SINT_'
  659. end
  660. else
  661. procedureprefix := 'FPC_VAL_UINT_';
  662. end;
  663. End;
  664. emitcall(procedureprefix+pstringdef(node^.resulttype)^.stringtypname);
  665. { before disposing node we need to ungettemp !! PM }
  666. if node^.left^.location.loc in [LOC_REFERENCE,LOC_MEM] then
  667. ungetiftemp(node^.left^.location.reference);
  668. disposetree(node);
  669. p^.left := nil;
  670. {reload esi in case the dest_para/code_para is a class variable or so}
  671. maybe_loadesi;
  672. If (dest_para^.resulttype^.deftype = orddef) Then
  673. Begin
  674. {store the result in a safe place, because EAX may be used by a
  675. register variable}
  676. hreg := getexplicitregister32(R_EAX);
  677. emit_reg_reg(A_MOV,S_L,R_EAX,hreg);
  678. if is_64bitint(dest_para^.resulttype) then
  679. begin
  680. hreg2:=getexplicitregister32(R_EDX);
  681. emit_reg_reg(A_MOV,S_L,R_EDX,hreg2);
  682. end;
  683. {as of now, hreg now holds the location of the result, if it was
  684. integer}
  685. End;
  686. { restore the register vars}
  687. popusedregisters(pushed);
  688. If has_code and Not(has_32bit_code) Then
  689. {only 16bit code is possible}
  690. Begin
  691. {load the address of the code parameter}
  692. secondpass(code_para^.left);
  693. {move the code to its destination}
  694. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,NewReference(hr),R_EDI)));
  695. emit_mov_reg_loc(R_DI,code_para^.left^.location);
  696. Disposetree(code_para);
  697. End;
  698. {restore the address of the result}
  699. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  700. {set up hr2 to a refernce with EDI as base register}
  701. reset_reference(hr2);
  702. hr2.base := R_EDI;
  703. {save the function result in the destination variable}
  704. Case dest_para^.left^.resulttype^.deftype of
  705. floatdef:
  706. floatstore(PFloatDef(dest_para^.left^.resulttype)^.typ, hr2);
  707. orddef:
  708. Case PordDef(dest_para^.left^.resulttype)^.typ of
  709. u8bit,s8bit:
  710. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV, S_B,
  711. RegToReg8(hreg),newreference(hr2))));
  712. u16bit,s16bit:
  713. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV, S_W,
  714. RegToReg16(hreg),newreference(hr2))));
  715. u32bit,s32bit:
  716. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV, S_L,
  717. hreg,newreference(hr2))));
  718. u64bit,s64bit:
  719. begin
  720. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV, S_L,
  721. hreg,newreference(hr2))));
  722. r:=newreference(hr2);
  723. inc(r^.offset,4);
  724. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV, S_L,
  725. hreg2,r)));
  726. end;
  727. End;
  728. End;
  729. If (cs_check_range in aktlocalswitches) and
  730. (dest_para^.left^.resulttype^.deftype = orddef) and
  731. (not(is_64bitint(dest_para^.left^.resulttype))) and
  732. {the following has to be changed to 64bit checking, once Val
  733. returns 64 bit values (unless a special Val function is created
  734. for that)}
  735. {no need to rangecheck longints or cardinals on 32bit processors}
  736. not((porddef(dest_para^.left^.resulttype)^.typ = s32bit) and
  737. (porddef(dest_para^.left^.resulttype)^.low = $80000000) and
  738. (porddef(dest_para^.left^.resulttype)^.high = $7fffffff)) and
  739. not((porddef(dest_para^.left^.resulttype)^.typ = u32bit) and
  740. (porddef(dest_para^.left^.resulttype)^.low = 0) and
  741. (porddef(dest_para^.left^.resulttype)^.high = $ffffffff)) then
  742. Begin
  743. hp := getcopy(dest_para^.left);
  744. hp^.location.loc := LOC_REGISTER;
  745. hp^.location.register := hreg;
  746. {do not register this temporary def}
  747. OldRegisterDef := RegisterDef;
  748. RegisterDef := False;
  749. Case PordDef(dest_para^.left^.resulttype)^.typ of
  750. u8bit,u16bit,u32bit: new(hdef,init(u32bit,0,$ffffffff));
  751. s8bit,s16bit,s32bit: new(hdef,init(s32bit,$80000000,$7fffffff));
  752. end;
  753. hp^.resulttype := hdef;
  754. emitrangecheck(hp,dest_para^.left^.resulttype);
  755. hp^.right := nil;
  756. Dispose(hp^.resulttype, Done);
  757. RegisterDef := OldRegisterDef;
  758. disposetree(hp);
  759. End;
  760. {dest_para^.right is already nil}
  761. disposetree(dest_para);
  762. UnGetIfTemp(hr);
  763. end;
  764. var
  765. r : preference;
  766. hp : ptree;
  767. l : longint;
  768. ispushed : boolean;
  769. hregister : tregister;
  770. otlabel,oflabel : pasmlabel;
  771. oldpushedparasize : longint;
  772. begin
  773. { save & reset pushedparasize }
  774. oldpushedparasize:=pushedparasize;
  775. pushedparasize:=0;
  776. case p^.inlinenumber of
  777. in_assert_x_y:
  778. begin
  779. otlabel:=truelabel;
  780. oflabel:=falselabel;
  781. getlabel(truelabel);
  782. getlabel(falselabel);
  783. secondpass(p^.left^.left);
  784. if cs_do_assertion in aktlocalswitches then
  785. begin
  786. maketojumpbool(p^.left^.left);
  787. emitlab(falselabel);
  788. { erroraddr }
  789. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EBP)));
  790. { lineno }
  791. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,aktfilepos.line)));
  792. { filename string }
  793. hp:=genstringconstnode(current_module^.sourcefiles^.get_file_name(aktfilepos.fileindex));
  794. secondpass(hp);
  795. if codegenerror then
  796. exit;
  797. emitpushreferenceaddr(hp^.location.reference);
  798. disposetree(hp);
  799. { push msg }
  800. secondpass(p^.left^.right^.left);
  801. emitpushreferenceaddr(p^.left^.right^.left^.location.reference);
  802. { call }
  803. emitcall('FPC_ASSERT');
  804. emitlab(truelabel);
  805. end;
  806. freelabel(truelabel);
  807. freelabel(falselabel);
  808. truelabel:=otlabel;
  809. falselabel:=oflabel;
  810. end;
  811. in_lo_word,
  812. in_hi_word :
  813. begin
  814. secondpass(p^.left);
  815. p^.location.loc:=LOC_REGISTER;
  816. if p^.left^.location.loc<>LOC_REGISTER then
  817. begin
  818. if p^.left^.location.loc=LOC_CREGISTER then
  819. begin
  820. p^.location.register:=reg32toreg16(getregister32);
  821. emit_reg_reg(A_MOV,S_W,p^.left^.location.register,
  822. p^.location.register);
  823. end
  824. else
  825. begin
  826. del_reference(p^.left^.location.reference);
  827. p^.location.register:=reg32toreg16(getregister32);
  828. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,newreference(p^.left^.location.reference),
  829. p^.location.register)));
  830. end;
  831. end
  832. else p^.location.register:=p^.left^.location.register;
  833. if p^.inlinenumber=in_hi_word then
  834. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_W,8,p^.location.register)));
  835. p^.location.register:=reg16toreg8(p^.location.register);
  836. end;
  837. in_sizeof_x,
  838. in_typeof_x :
  839. begin
  840. { for both cases load vmt }
  841. if p^.left^.treetype=typen then
  842. begin
  843. p^.location.register:=getregister32;
  844. exprasmlist^.concat(new(pai386,op_sym_ofs_reg(A_MOV,
  845. S_L,newasmsymbol(pobjectdef(p^.left^.resulttype)^.vmt_mangledname),0,
  846. p^.location.register)));
  847. end
  848. else
  849. begin
  850. secondpass(p^.left);
  851. del_reference(p^.left^.location.reference);
  852. p^.location.loc:=LOC_REGISTER;
  853. p^.location.register:=getregister32;
  854. { load VMT pointer }
  855. inc(p^.left^.location.reference.offset,
  856. pobjectdef(p^.left^.resulttype)^.vmt_offset);
  857. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  858. newreference(p^.left^.location.reference),
  859. p^.location.register)));
  860. end;
  861. { in sizeof load size }
  862. if p^.inlinenumber=in_sizeof_x then
  863. begin
  864. new(r);
  865. reset_reference(r^);
  866. r^.base:=p^.location.register;
  867. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,
  868. p^.location.register)));
  869. end;
  870. end;
  871. in_lo_long,
  872. in_hi_long :
  873. begin
  874. secondpass(p^.left);
  875. p^.location.loc:=LOC_REGISTER;
  876. if p^.left^.location.loc<>LOC_REGISTER then
  877. begin
  878. if p^.left^.location.loc=LOC_CREGISTER then
  879. begin
  880. p^.location.register:=getregister32;
  881. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,
  882. p^.location.register);
  883. end
  884. else
  885. begin
  886. del_reference(p^.left^.location.reference);
  887. p^.location.register:=getregister32;
  888. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  889. p^.location.register)));
  890. end;
  891. end
  892. else p^.location.register:=p^.left^.location.register;
  893. if p^.inlinenumber=in_hi_long then
  894. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_L,16,p^.location.register)));
  895. p^.location.register:=reg32toreg16(p^.location.register);
  896. end;
  897. in_lo_qword,
  898. in_hi_qword:
  899. begin
  900. secondpass(p^.left);
  901. p^.location.loc:=LOC_REGISTER;
  902. case p^.left^.location.loc of
  903. LOC_CREGISTER:
  904. begin
  905. p^.location.register:=getregister32;
  906. if p^.inlinenumber=in_hi_qword then
  907. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerhigh,
  908. p^.location.register)
  909. else
  910. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerlow,
  911. p^.location.register)
  912. end;
  913. LOC_MEM,LOC_REFERENCE:
  914. begin
  915. del_reference(p^.left^.location.reference);
  916. p^.location.register:=getregister32;
  917. r:=newreference(p^.left^.location.reference);
  918. if p^.inlinenumber=in_hi_qword then
  919. inc(r^.offset,4);
  920. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  921. r,p^.location.register)));
  922. end;
  923. LOC_REGISTER:
  924. begin
  925. if p^.inlinenumber=in_hi_qword then
  926. begin
  927. p^.location.register:=p^.left^.location.registerhigh;
  928. ungetregister32(p^.left^.location.registerlow);
  929. end
  930. else
  931. begin
  932. p^.location.register:=p^.left^.location.registerlow;
  933. ungetregister32(p^.left^.location.registerhigh);
  934. end;
  935. end;
  936. end;
  937. end;
  938. in_length_string :
  939. begin
  940. secondpass(p^.left);
  941. set_location(p^.location,p^.left^.location);
  942. { length in ansi strings is at offset -8 }
  943. if is_ansistring(p^.left^.resulttype) then
  944. dec(p^.location.reference.offset,8)
  945. { char is always 1, so make it a constant value }
  946. else if is_char(p^.left^.resulttype) then
  947. begin
  948. clear_location(p^.location);
  949. p^.location.loc:=LOC_MEM;
  950. p^.location.reference.is_immediate:=true;
  951. p^.location.reference.offset:=1;
  952. end;
  953. end;
  954. in_pred_x,
  955. in_succ_x:
  956. begin
  957. secondpass(p^.left);
  958. if not (cs_check_overflow in aktlocalswitches) then
  959. if p^.inlinenumber=in_pred_x then
  960. asmop:=A_DEC
  961. else
  962. asmop:=A_INC
  963. else
  964. if p^.inlinenumber=in_pred_x then
  965. asmop:=A_SUB
  966. else
  967. asmop:=A_ADD;
  968. case p^.resulttype^.size of
  969. 4 : opsize:=S_L;
  970. 2 : opsize:=S_W;
  971. 1 : opsize:=S_B;
  972. else
  973. internalerror(10080);
  974. end;
  975. p^.location.loc:=LOC_REGISTER;
  976. if p^.left^.location.loc<>LOC_REGISTER then
  977. begin
  978. p^.location.register:=getregister32;
  979. if (p^.resulttype^.size=2) then
  980. p^.location.register:=reg32toreg16(p^.location.register);
  981. if (p^.resulttype^.size=1) then
  982. p^.location.register:=reg32toreg8(p^.location.register);
  983. if p^.left^.location.loc=LOC_CREGISTER then
  984. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,
  985. p^.location.register)
  986. else
  987. if p^.left^.location.loc=LOC_FLAGS then
  988. emit_flag2reg(p^.left^.location.resflags,p^.location.register)
  989. else
  990. begin
  991. del_reference(p^.left^.location.reference);
  992. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(p^.left^.location.reference),
  993. p^.location.register)));
  994. end;
  995. end
  996. else p^.location.register:=p^.left^.location.register;
  997. if not (cs_check_overflow in aktlocalswitches) then
  998. exprasmlist^.concat(new(pai386,op_reg(asmop,opsize,
  999. p^.location.register)))
  1000. else
  1001. exprasmlist^.concat(new(pai386,op_const_reg(asmop,opsize,1,
  1002. p^.location.register)));
  1003. emitoverflowcheck(p);
  1004. emitrangecheck(p,p^.resulttype);
  1005. end;
  1006. in_dec_x,
  1007. in_inc_x :
  1008. begin
  1009. { set defaults }
  1010. addvalue:=1;
  1011. addconstant:=true;
  1012. { load first parameter, must be a reference }
  1013. secondpass(p^.left^.left);
  1014. case p^.left^.left^.resulttype^.deftype of
  1015. orddef,
  1016. enumdef : begin
  1017. case p^.left^.left^.resulttype^.size of
  1018. 1 : opsize:=S_B;
  1019. 2 : opsize:=S_W;
  1020. 4 : opsize:=S_L;
  1021. end;
  1022. end;
  1023. pointerdef : begin
  1024. opsize:=S_L;
  1025. if porddef(ppointerdef(p^.left^.left^.resulttype)^.definition)=voiddef then
  1026. addvalue:=1
  1027. else
  1028. addvalue:=ppointerdef(p^.left^.left^.resulttype)^.definition^.size;
  1029. end;
  1030. else
  1031. internalerror(10081);
  1032. end;
  1033. { second argument specified?, must be a s32bit in register }
  1034. if assigned(p^.left^.right) then
  1035. begin
  1036. secondpass(p^.left^.right^.left);
  1037. { when constant, just multiply the addvalue }
  1038. if is_constintnode(p^.left^.right^.left) then
  1039. addvalue:=addvalue*get_ordinal_value(p^.left^.right^.left)
  1040. else
  1041. begin
  1042. case p^.left^.right^.left^.location.loc of
  1043. LOC_REGISTER,
  1044. LOC_CREGISTER : hregister:=p^.left^.right^.left^.location.register;
  1045. LOC_MEM,
  1046. LOC_REFERENCE : begin
  1047. del_reference(p^.left^.right^.left^.location.reference);
  1048. hregister:=getregister32;
  1049. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1050. newreference(p^.left^.right^.left^.location.reference),hregister)));
  1051. end;
  1052. else
  1053. internalerror(10082);
  1054. end;
  1055. { insert multiply with addvalue if its >1 }
  1056. if addvalue>1 then
  1057. exprasmlist^.concat(new(pai386,op_const_reg(A_IMUL,opsize,
  1058. addvalue,hregister)));
  1059. addconstant:=false;
  1060. end;
  1061. end;
  1062. { write the add instruction }
  1063. if addconstant then
  1064. begin
  1065. if (addvalue=1) and not(cs_check_overflow in aktlocalswitches) then
  1066. begin
  1067. if p^.left^.left^.location.loc=LOC_CREGISTER then
  1068. exprasmlist^.concat(new(pai386,op_reg(incdecop[p^.inlinenumber],opsize,
  1069. p^.left^.left^.location.register)))
  1070. else
  1071. exprasmlist^.concat(new(pai386,op_ref(incdecop[p^.inlinenumber],opsize,
  1072. newreference(p^.left^.left^.location.reference))))
  1073. end
  1074. else
  1075. begin
  1076. if p^.left^.left^.location.loc=LOC_CREGISTER then
  1077. exprasmlist^.concat(new(pai386,op_const_reg(addsubop[p^.inlinenumber],opsize,
  1078. addvalue,p^.left^.left^.location.register)))
  1079. else
  1080. exprasmlist^.concat(new(pai386,op_const_ref(addsubop[p^.inlinenumber],opsize,
  1081. addvalue,newreference(p^.left^.left^.location.reference))));
  1082. end
  1083. end
  1084. else
  1085. begin
  1086. { BUG HERE : detected with nasm :
  1087. hregister is allways 32 bit
  1088. it should be converted to 16 or 8 bit depending on op_size PM }
  1089. { still not perfect :
  1090. if hregister is already a 16 bit reg ?? PM }
  1091. { makeregXX is the solution (FK) }
  1092. case opsize of
  1093. S_B : hregister:=makereg8(hregister);
  1094. S_W : hregister:=makereg16(hregister);
  1095. end;
  1096. if p^.left^.left^.location.loc=LOC_CREGISTER then
  1097. exprasmlist^.concat(new(pai386,op_reg_reg(addsubop[p^.inlinenumber],opsize,
  1098. hregister,p^.left^.left^.location.register)))
  1099. else
  1100. exprasmlist^.concat(new(pai386,op_reg_ref(addsubop[p^.inlinenumber],opsize,
  1101. hregister,newreference(p^.left^.left^.location.reference))));
  1102. case opsize of
  1103. S_B : hregister:=reg8toreg32(hregister);
  1104. S_W : hregister:=reg16toreg32(hregister);
  1105. end;
  1106. ungetregister32(hregister);
  1107. end;
  1108. emitoverflowcheck(p^.left^.left);
  1109. emitrangecheck(p^.left^.left,p^.left^.left^.resulttype);
  1110. end;
  1111. in_assigned_x :
  1112. begin
  1113. secondpass(p^.left^.left);
  1114. p^.location.loc:=LOC_FLAGS;
  1115. if (p^.left^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1116. begin
  1117. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,
  1118. p^.left^.left^.location.register,
  1119. p^.left^.left^.location.register)));
  1120. ungetregister32(p^.left^.left^.location.register);
  1121. end
  1122. else
  1123. begin
  1124. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_L,0,
  1125. newreference(p^.left^.left^.location.reference))));
  1126. del_reference(p^.left^.left^.location.reference);
  1127. end;
  1128. p^.location.resflags:=F_NE;
  1129. end;
  1130. in_reset_typedfile,in_rewrite_typedfile :
  1131. begin
  1132. pushusedregisters(pushed,$ff);
  1133. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,pfiledef(p^.left^.resulttype)^.typed_as^.size)));
  1134. secondpass(p^.left);
  1135. emitpushreferenceaddr(p^.left^.location.reference);
  1136. if p^.inlinenumber=in_reset_typedfile then
  1137. emitcall('FPC_RESET_TYPED')
  1138. else
  1139. emitcall('FPC_REWRITE_TYPED');
  1140. popusedregisters(pushed);
  1141. end;
  1142. in_write_x :
  1143. handlereadwrite(false,false);
  1144. in_writeln_x :
  1145. handlereadwrite(false,true);
  1146. in_read_x :
  1147. handlereadwrite(true,false);
  1148. in_readln_x :
  1149. handlereadwrite(true,true);
  1150. in_str_x_string :
  1151. begin
  1152. handle_str;
  1153. maybe_loadesi;
  1154. end;
  1155. in_val_x :
  1156. Begin
  1157. handle_val;
  1158. End;
  1159. in_include_x_y,
  1160. in_exclude_x_y:
  1161. begin
  1162. secondpass(p^.left^.left);
  1163. if p^.left^.right^.left^.treetype=ordconstn then
  1164. begin
  1165. { calculate bit position }
  1166. l:=1 shl (p^.left^.right^.left^.value mod 32);
  1167. { determine operator }
  1168. if p^.inlinenumber=in_include_x_y then
  1169. asmop:=A_OR
  1170. else
  1171. begin
  1172. asmop:=A_AND;
  1173. l:=not(l);
  1174. end;
  1175. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  1176. begin
  1177. inc(p^.left^.left^.location.reference.offset,(p^.left^.right^.left^.value div 32)*4);
  1178. exprasmlist^.concat(new(pai386,op_const_ref(asmop,S_L,
  1179. l,newreference(p^.left^.left^.location.reference))));
  1180. del_reference(p^.left^.left^.location.reference);
  1181. end
  1182. else
  1183. { LOC_CREGISTER }
  1184. exprasmlist^.concat(new(pai386,op_const_reg(asmop,S_L,
  1185. l,p^.left^.left^.location.register)));
  1186. end
  1187. else
  1188. begin
  1189. { generate code for the element to set }
  1190. ispushed:=maybe_push(p^.left^.right^.left^.registers32,p^.left^.left,false);
  1191. secondpass(p^.left^.right^.left);
  1192. if ispushed then
  1193. restore(p^.left^.left,false);
  1194. { determine asm operator }
  1195. if p^.inlinenumber=in_include_x_y then
  1196. asmop:=A_BTS
  1197. else
  1198. asmop:=A_BTR;
  1199. if psetdef(p^.left^.resulttype)^.settype=smallset then
  1200. begin
  1201. if p^.left^.right^.left^.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  1202. hregister:=p^.left^.right^.left^.location.register
  1203. else
  1204. begin
  1205. hregister:=R_EDI;
  1206. opsize:=def2def_opsize(p^.left^.right^.left^.resulttype,u32bitdef);
  1207. if opsize in [S_B,S_W,S_L] then
  1208. op:=A_MOV
  1209. else
  1210. op:=A_MOVZX;
  1211. exprasmlist^.concat(new(pai386,op_ref_reg(op,opsize,
  1212. newreference(p^.left^.right^.left^.location.reference),R_EDI)));
  1213. end;
  1214. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  1215. exprasmlist^.concat(new(pai386,op_reg_ref(asmop,S_L,hregister,
  1216. newreference(p^.left^.left^.location.reference))))
  1217. else
  1218. exprasmlist^.concat(new(pai386,op_reg_reg(asmop,S_L,hregister,
  1219. p^.left^.left^.location.register)));
  1220. end
  1221. else
  1222. begin
  1223. pushsetelement(p^.left^.right^.left);
  1224. { normset is allways a ref }
  1225. emitpushreferenceaddr(p^.left^.left^.location.reference);
  1226. if p^.inlinenumber=in_include_x_y then
  1227. emitcall('FPC_SET_SET_BYTE')
  1228. else
  1229. emitcall('FPC_SET_UNSET_BYTE');
  1230. {CGMessage(cg_e_include_not_implemented);}
  1231. end;
  1232. end;
  1233. end;
  1234. else internalerror(9);
  1235. end;
  1236. { reset pushedparasize }
  1237. pushedparasize:=oldpushedparasize;
  1238. end;
  1239. end.
  1240. {
  1241. $Log$
  1242. Revision 1.64 1999-08-03 22:02:42 peter
  1243. * moved bitmask constants to sets
  1244. * some other type/const renamings
  1245. Revision 1.63 1999/07/23 16:05:18 peter
  1246. * alignment is now saved in the symtable
  1247. * C alignment added for records
  1248. * PPU version increased to solve .12 <-> .13 probs
  1249. Revision 1.62 1999/07/05 20:13:10 peter
  1250. * removed temp defines
  1251. Revision 1.61 1999/07/03 14:14:27 florian
  1252. + start of val(int64/qword)
  1253. * longbool, wordbool constants weren't written, fixed
  1254. Revision 1.60 1999/07/01 15:49:09 florian
  1255. * int64/qword type release
  1256. + lo/hi for int64/qword
  1257. Revision 1.59 1999/06/21 16:33:27 jonas
  1258. * fixed include() with smallsets
  1259. Revision 1.58 1999/06/11 11:44:56 peter
  1260. *** empty log message ***
  1261. Revision 1.57 1999/06/02 10:11:43 florian
  1262. * make cycle fixed i.e. compilation with 0.99.10
  1263. * some fixes for qword
  1264. * start of register calling conventions
  1265. Revision 1.56 1999/05/31 12:43:32 peter
  1266. * fixed register allocation for storefuncresult
  1267. Revision 1.55 1999/05/27 19:44:13 peter
  1268. * removed oldasm
  1269. * plabel -> pasmlabel
  1270. * -a switches to source writing automaticly
  1271. * assembler readers OOPed
  1272. * asmsymbol automaticly external
  1273. * jumptables and other label fixes for asm readers
  1274. Revision 1.54 1999/05/23 19:55:11 florian
  1275. * qword/int64 multiplication fixed
  1276. + qword/int64 subtraction
  1277. Revision 1.53 1999/05/23 18:42:01 florian
  1278. * better error recovering in typed constants
  1279. * some problems with arrays of const fixed, some problems
  1280. due my previous
  1281. - the location type of array constructor is now LOC_MEM
  1282. - the pushing of high fixed
  1283. - parameter copying fixed
  1284. - zero temp. allocation removed
  1285. * small problem in the assembler writers fixed:
  1286. ref to nil wasn't written correctly
  1287. Revision 1.52 1999/05/21 13:54:50 peter
  1288. * NEWLAB for label as symbol
  1289. Revision 1.51 1999/05/18 21:58:27 florian
  1290. * fixed some bugs related to temp. ansistrings and functions results
  1291. which return records/objects/arrays which need init/final.
  1292. Revision 1.50 1999/05/17 21:57:03 florian
  1293. * new temporary ansistring handling
  1294. Revision 1.49 1999/05/12 15:46:26 pierre
  1295. * handle_str disposetree was badly placed
  1296. Revision 1.48 1999/05/12 00:19:42 peter
  1297. * removed R_DEFAULT_SEG
  1298. * uniform float names
  1299. Revision 1.47 1999/05/06 09:05:13 peter
  1300. * generic write_float and str_float
  1301. * fixed constant float conversions
  1302. Revision 1.46 1999/05/05 16:18:20 jonas
  1303. * changes to handle_val so register vars are pushed/poped only once
  1304. Revision 1.45 1999/05/01 13:24:08 peter
  1305. * merged nasm compiler
  1306. * old asm moved to oldasm/
  1307. Revision 1.44 1999/04/26 18:28:13 peter
  1308. * better read/write array
  1309. Revision 1.43 1999/04/19 09:45:48 pierre
  1310. + cdecl or stdcall push all args with longint size
  1311. * tempansi stuff cleaned up
  1312. Revision 1.42 1999/04/14 09:11:59 peter
  1313. * fixed include
  1314. Revision 1.41 1999/04/08 23:59:49 pierre
  1315. * temp string for val code freed
  1316. Revision 1.40 1999/04/08 15:57:46 peter
  1317. + subrange checking for readln()
  1318. Revision 1.39 1999/04/07 15:31:16 pierre
  1319. * all formaldefs are now a sinlge definition
  1320. cformaldef (this was necessary for double_checksum)
  1321. + small part of double_checksum code
  1322. Revision 1.38 1999/04/05 11:07:26 jonas
  1323. * fixed some typos in the constants of the range checking for Val
  1324. Revision 1.37 1999/04/01 22:07:51 peter
  1325. * universal string names (ansistr instead of stransi) for val/str
  1326. Revision 1.36 1999/04/01 06:21:04 jonas
  1327. * added initialization for has_32bit_code (caused problems with Val statement
  1328. without code parameter)
  1329. Revision 1.35 1999/03/31 20:30:49 michael
  1330. * fixed typo: odlval to oldval
  1331. Revision 1.34 1999/03/31 17:13:09 jonas
  1332. * bugfix for -Ox with internal val code
  1333. * internal val code now requires less free registers
  1334. * internal val code no longer needs a temp var for range checking
  1335. Revision 1.33 1999/03/26 00:24:15 peter
  1336. * last para changed to long for easier pushing with 4 byte aligns
  1337. Revision 1.32 1999/03/26 00:05:26 peter
  1338. * released valintern
  1339. + deffile is now removed when compiling is finished
  1340. * ^( compiles now correct
  1341. + static directive
  1342. * shrd fixed
  1343. Revision 1.31 1999/03/24 23:16:49 peter
  1344. * fixed bugs 212,222,225,227,229,231,233
  1345. Revision 1.30 1999/03/16 17:52:56 jonas
  1346. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  1347. * in cgi386inl: also range checking for subrange types (compile with "-dreadrangecheck")
  1348. * in cgai386: also small fixes to emitrangecheck
  1349. Revision 1.29 1999/02/25 21:02:27 peter
  1350. * ag386bin updates
  1351. + coff writer
  1352. Revision 1.28 1999/02/22 02:15:11 peter
  1353. * updates for ag386bin
  1354. Revision 1.27 1999/02/17 14:21:40 pierre
  1355. * unused local removed
  1356. Revision 1.26 1999/02/15 11:40:21 pierre
  1357. * pred/succ with overflow check must use ADD DEC !!
  1358. Revision 1.25 1999/02/05 10:56:19 florian
  1359. * in some cases a writeln of temp. ansistrings cause a memory leak, fixed
  1360. Revision 1.24 1999/01/21 22:10:39 peter
  1361. * fixed array of const
  1362. * generic platform independent high() support
  1363. Revision 1.23 1999/01/06 12:23:29 florian
  1364. * str(...) for ansi/long and widestrings fixed
  1365. Revision 1.22 1998/12/11 23:36:07 florian
  1366. + again more stuff for int64/qword:
  1367. - comparision operators
  1368. - code generation for: str, read(ln), write(ln)
  1369. Revision 1.21 1998/12/11 00:02:50 peter
  1370. + globtype,tokens,version unit splitted from globals
  1371. Revision 1.20 1998/11/27 14:50:32 peter
  1372. + open strings, $P switch support
  1373. Revision 1.19 1998/11/26 13:10:40 peter
  1374. * new int - int conversion -dNEWCNV
  1375. * some function renamings
  1376. Revision 1.18 1998/11/24 17:04:27 peter
  1377. * fixed length(char) when char is a variable
  1378. Revision 1.17 1998/11/05 12:02:33 peter
  1379. * released useansistring
  1380. * removed -Sv, its now available in fpc modes
  1381. Revision 1.16 1998/10/22 17:11:13 pierre
  1382. + terminated the include exclude implementation for i386
  1383. * enums inside records fixed
  1384. Revision 1.15 1998/10/21 15:12:50 pierre
  1385. * bug fix for IOCHECK inside a procedure with iocheck modifier
  1386. * removed the GPF for unexistant overloading
  1387. (firstcall was called with procedinition=nil !)
  1388. * changed typen to what Florian proposed
  1389. gentypenode(p : pdef) sets the typenodetype field
  1390. and resulttype is only set if inside bt_type block !
  1391. Revision 1.14 1998/10/20 08:06:40 pierre
  1392. * several memory corruptions due to double freemem solved
  1393. => never use p^.loc.location:=p^.left^.loc.location;
  1394. + finally I added now by default
  1395. that ra386dir translates global and unit symbols
  1396. + added a first field in tsymtable and
  1397. a nextsym field in tsym
  1398. (this allows to obtain ordered type info for
  1399. records and objects in gdb !)
  1400. Revision 1.13 1998/10/13 16:50:02 pierre
  1401. * undid some changes of Peter that made the compiler wrong
  1402. for m68k (I had to reinsert some ifdefs)
  1403. * removed several memory leaks under m68k
  1404. * removed the meory leaks for assembler readers
  1405. * cross compiling shoud work again better
  1406. ( crosscompiling sysamiga works
  1407. but as68k still complain about some code !)
  1408. Revision 1.12 1998/10/08 17:17:12 pierre
  1409. * current_module old scanner tagged as invalid if unit is recompiled
  1410. + added ppheap for better info on tracegetmem of heaptrc
  1411. (adds line column and file index)
  1412. * several memory leaks removed ith help of heaptrc !!
  1413. Revision 1.11 1998/10/05 21:33:15 peter
  1414. * fixed 161,165,166,167,168
  1415. Revision 1.10 1998/10/05 12:32:44 peter
  1416. + assert() support
  1417. Revision 1.8 1998/10/02 10:35:09 peter
  1418. * support for inc(pointer,value) which now increases with value instead
  1419. of 0*value :)
  1420. Revision 1.7 1998/09/21 08:45:07 pierre
  1421. + added vmt_offset in tobjectdef.write for fututre use
  1422. (first steps to have objects without vmt if no virtual !!)
  1423. + added fpu_used field for tabstractprocdef :
  1424. sets this level to 2 if the functions return with value in FPU
  1425. (is then set to correct value at parsing of implementation)
  1426. THIS MIGHT refuse some code with FPU expression too complex
  1427. that were accepted before and even in some cases
  1428. that don't overflow in fact
  1429. ( like if f : float; is a forward that finally in implementation
  1430. only uses one fpu register !!)
  1431. Nevertheless I think that it will improve security on
  1432. FPU operations !!
  1433. * most other changes only for UseBrowser code
  1434. (added symtable references for record and objects)
  1435. local switch for refs to args and local of each function
  1436. (static symtable still missing)
  1437. UseBrowser still not stable and probably broken by
  1438. the definition hash array !!
  1439. Revision 1.6 1998/09/20 12:26:37 peter
  1440. * merged fixes
  1441. Revision 1.5 1998/09/17 09:42:15 peter
  1442. + pass_2 for cg386
  1443. * Message() -> CGMessage() for pass_1/pass_2
  1444. Revision 1.4 1998/09/14 10:43:49 peter
  1445. * all internal RTL functions start with FPC_
  1446. Revision 1.3.2.1 1998/09/20 12:20:07 peter
  1447. * Fixed stack not on 4 byte boundary when doing a call
  1448. Revision 1.3 1998/09/05 23:03:57 florian
  1449. * some fixes to get -Or work:
  1450. - inc/dec didn't take care of CREGISTER
  1451. - register calculcation of inc/dec was wrong
  1452. - var/const parameters get now assigned 32 bit register, but
  1453. const parameters only if they are passed by reference !
  1454. Revision 1.2 1998/09/04 08:41:40 peter
  1455. * updated some error CGMessages
  1456. Revision 1.1 1998/08/31 12:22:14 peter
  1457. * secondinline moved to cg386inl
  1458. Revision 1.19 1998/08/31 08:52:03 peter
  1459. * fixed error 10 with succ() and pref()
  1460. Revision 1.18 1998/08/20 21:36:38 peter
  1461. * fixed 'with object do' bug
  1462. Revision 1.17 1998/08/19 16:07:36 jonas
  1463. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1464. Revision 1.16 1998/08/18 09:24:36 pierre
  1465. * small warning position bug fixed
  1466. * support_mmx switches splitting was missing
  1467. * rhide error and warning output corrected
  1468. Revision 1.15 1998/08/13 11:00:09 peter
  1469. * fixed procedure<>procedure construct
  1470. Revision 1.14 1998/08/11 14:05:33 peter
  1471. * fixed sizeof(array of char)
  1472. Revision 1.13 1998/08/10 14:49:45 peter
  1473. + localswitches, moduleswitches, globalswitches splitting
  1474. Revision 1.12 1998/07/30 13:30:31 florian
  1475. * final implemenation of exception support, maybe it needs
  1476. some fixes :)
  1477. Revision 1.11 1998/07/24 22:16:52 florian
  1478. * internal error 10 together with array access fixed. I hope
  1479. that's the final fix.
  1480. Revision 1.10 1998/07/18 22:54:23 florian
  1481. * some ansi/wide/longstring support fixed:
  1482. o parameter passing
  1483. o returning as result from functions
  1484. Revision 1.9 1998/07/07 17:40:37 peter
  1485. * packrecords 4 works
  1486. * word aligning of parameters
  1487. Revision 1.8 1998/07/06 15:51:15 michael
  1488. Added length checking for string reading
  1489. Revision 1.7 1998/07/06 14:19:51 michael
  1490. + Added calls for reading/writing ansistrings
  1491. Revision 1.6 1998/07/01 15:28:48 peter
  1492. + better writeln/readln handling, now 100% like tp7
  1493. Revision 1.5 1998/06/25 14:04:17 peter
  1494. + internal inc/dec
  1495. Revision 1.4 1998/06/25 08:48:06 florian
  1496. * first version of rtti support
  1497. Revision 1.3 1998/06/09 16:01:33 pierre
  1498. + added procedure directive parsing for procvars
  1499. (accepted are popstack cdecl and pascal)
  1500. + added C vars with the following syntax
  1501. var C calias 'true_c_name';(can be followed by external)
  1502. reason is that you must add the Cprefix
  1503. which is target dependent
  1504. Revision 1.2 1998/06/08 13:13:29 pierre
  1505. + temporary variables now in temp_gen.pas unit
  1506. because it is processor independent
  1507. * mppc68k.bat modified to undefine i386 and support_mmx
  1508. (which are defaults for i386)
  1509. Revision 1.1 1998/06/05 17:44:10 peter
  1510. * splitted cgi386
  1511. }