cg386inl.pas 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. cobjects,verbose,globals,systems,
  26. aasm,i386,types,symtable,
  27. cgi386,cgai386,temp_gen,tgeni386,hcodegen,
  28. cg386ld,cg386cal;
  29. {*****************************************************************************
  30. Helpers
  31. *****************************************************************************}
  32. { reverts the parameter list }
  33. var nb_para : integer;
  34. function reversparameter(p : ptree) : ptree;
  35. var
  36. hp1,hp2 : ptree;
  37. begin
  38. hp1:=nil;
  39. nb_para := 0;
  40. while assigned(p) do
  41. begin
  42. { pull out }
  43. hp2:=p;
  44. p:=p^.right;
  45. inc(nb_para);
  46. { pull in }
  47. hp2^.right:=hp1;
  48. hp1:=hp2;
  49. end;
  50. reversparameter:=hp1;
  51. end;
  52. {*****************************************************************************
  53. SecondInLine
  54. *****************************************************************************}
  55. procedure secondinline(var p : ptree);
  56. const
  57. { tfloattype = (f32bit,s32real,s64real,s80real,s64bit); }
  58. float_name: array[tfloattype] of string[8]=
  59. ('FIXED','SINGLE','REAL','EXTENDED','COMP','FIXED16');
  60. incdecop:array[in_inc_x..in_dec_x] of tasmop=(A_INC,A_DEC);
  61. addsubop:array[in_inc_x..in_dec_x] of tasmop=(A_ADD,A_SUB);
  62. var
  63. aktfile : treference;
  64. ft : tfiletype;
  65. opsize : topsize;
  66. asmop : tasmop;
  67. pushed : tpushed;
  68. {inc/dec}
  69. addconstant : boolean;
  70. addvalue : longint;
  71. procedure handlereadwrite(doread,doln : boolean);
  72. { produces code for READ(LN) and WRITE(LN) }
  73. procedure loadstream;
  74. const
  75. io:array[0..1] of string[7]=('_OUTPUT','_INPUT');
  76. var
  77. r : preference;
  78. begin
  79. new(r);
  80. reset_reference(r^);
  81. r^.symbol:=stringdup('U_'+upper(target_info.system_unit)+io[byte(doread)]);
  82. concat_external(r^.symbol^,EXT_NEAR);
  83. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,r,R_EDI)))
  84. end;
  85. var
  86. node,hp : ptree;
  87. typedtyp,
  88. pararesult : pdef;
  89. has_length : boolean;
  90. dummycoll : tdefcoll;
  91. iolabel : plabel;
  92. npara : longint;
  93. begin
  94. { I/O check }
  95. if cs_check_io in aktlocalswitches then
  96. begin
  97. getlabel(iolabel);
  98. emitl(A_LABEL,iolabel);
  99. end
  100. else
  101. iolabel:=nil;
  102. { for write of real with the length specified }
  103. has_length:=false;
  104. hp:=nil;
  105. { reserve temporary pointer to data variable }
  106. aktfile.symbol:=nil;
  107. gettempofsizereference(4,aktfile);
  108. { first state text data }
  109. ft:=ft_text;
  110. { and state a parameter ? }
  111. if p^.left=nil then
  112. begin
  113. { the following instructions are for "writeln;" }
  114. loadstream;
  115. { save @aktfile in temporary variable }
  116. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,newreference(aktfile))));
  117. end
  118. else
  119. begin
  120. { revers paramters }
  121. node:=reversparameter(p^.left);
  122. p^.left := node;
  123. npara := nb_para;
  124. { calculate data variable }
  125. { is first parameter a file type ? }
  126. if node^.left^.resulttype^.deftype=filedef then
  127. begin
  128. ft:=pfiledef(node^.left^.resulttype)^.filetype;
  129. if ft=ft_typed then
  130. typedtyp:=pfiledef(node^.left^.resulttype)^.typed_as;
  131. secondpass(node^.left);
  132. if codegenerror then
  133. exit;
  134. { save reference in temporary variables }
  135. if node^.left^.location.loc<>LOC_REFERENCE then
  136. begin
  137. Message(cg_e_illegal_expression);
  138. exit;
  139. end;
  140. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(node^.left^.location.reference),R_EDI)));
  141. { skip to the next parameter }
  142. node:=node^.right;
  143. end
  144. else
  145. begin
  146. { load stdin/stdout stream }
  147. loadstream;
  148. end;
  149. { save @aktfile in temporary variable }
  150. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,newreference(aktfile))));
  151. if doread then
  152. { parameter by READ gives call by reference }
  153. dummycoll.paratyp:=vs_var
  154. { an WRITE Call by "Const" }
  155. else
  156. dummycoll.paratyp:=vs_const;
  157. { because of secondcallparan, which otherwise attaches }
  158. if ft=ft_typed then
  159. { this is to avoid copy of simple const parameters }
  160. dummycoll.data:=new(pformaldef,init)
  161. else
  162. { I think, this isn't a good solution (FK) }
  163. dummycoll.data:=nil;
  164. while assigned(node) do
  165. begin
  166. pushusedregisters(pushed,$ff);
  167. hp:=node;
  168. node:=node^.right;
  169. hp^.right:=nil;
  170. if hp^.is_colon_para then
  171. Message(parser_e_illegal_colon_qualifier);
  172. if ft=ft_typed then
  173. never_copy_const_param:=true;
  174. secondcallparan(hp,@dummycoll,false,false,0);
  175. if ft=ft_typed then
  176. never_copy_const_param:=false;
  177. hp^.right:=node;
  178. if codegenerror then
  179. exit;
  180. emit_push_mem(aktfile);
  181. if (ft=ft_typed) then
  182. begin
  183. { OK let's try this }
  184. { first we must only allow the right type }
  185. { we have to call blockread or blockwrite }
  186. { but the real problem is that }
  187. { reset and rewrite should have set }
  188. { the type size }
  189. { as recordsize for that file !!!! }
  190. { how can we make that }
  191. { I think that is only possible by adding }
  192. { reset and rewrite to the inline list a call }
  193. { allways read only one record by element }
  194. push_int(typedtyp^.size);
  195. if doread then
  196. emitcall('FPC_TYPED_READ',true)
  197. else
  198. emitcall('FPC_TYPED_WRITE',true);
  199. end
  200. else
  201. begin
  202. { save current position }
  203. pararesult:=hp^.left^.resulttype;
  204. { handle possible field width }
  205. { of course only for write(ln) }
  206. if not doread then
  207. begin
  208. { handle total width parameter }
  209. if assigned(node) and node^.is_colon_para then
  210. begin
  211. hp:=node;
  212. node:=node^.right;
  213. hp^.right:=nil;
  214. secondcallparan(hp,@dummycoll,false,false,0);
  215. hp^.right:=node;
  216. if codegenerror then
  217. exit;
  218. has_length:=true;
  219. end
  220. else
  221. if pararesult^.deftype<>floatdef then
  222. push_int(0)
  223. else
  224. push_int(-32767);
  225. { a second colon para for a float ? }
  226. if assigned(node) and node^.is_colon_para then
  227. begin
  228. hp:=node;
  229. node:=node^.right;
  230. hp^.right:=nil;
  231. secondcallparan(hp,@dummycoll,false,false,0);
  232. hp^.right:=node;
  233. if pararesult^.deftype<>floatdef then
  234. Message(parser_e_illegal_colon_qualifier);
  235. if codegenerror then
  236. exit;
  237. end
  238. else
  239. begin
  240. if pararesult^.deftype=floatdef then
  241. push_int(-1);
  242. end
  243. end;
  244. case pararesult^.deftype of
  245. stringdef : begin
  246. if doread then
  247. begin
  248. { push maximum string length }
  249. push_int(pstringdef(pararesult)^.len);
  250. case pstringdef(pararesult)^.string_typ of
  251. st_shortstring:
  252. emitcall ('FPC_READ_TEXT_STRING',true);
  253. st_ansistring:
  254. emitcall ('FPC_READ_TEXT_ANSISTRING',true);
  255. st_longstring:
  256. emitcall ('FPC_READ_TEXT_LONGSTRING',true);
  257. st_widestring:
  258. emitcall ('FPC_READ_TEXT_ANSISTRING',true);
  259. end
  260. end
  261. else
  262. Case pstringdef(Pararesult)^.string_typ of
  263. st_shortstring:
  264. emitcall ('FPC_WRITE_TEXT_STRING',true);
  265. st_ansistring:
  266. emitcall ('FPC_WRITE_TEXT_ANSISTRING',true);
  267. st_longstring:
  268. emitcall ('FPC_WRITE_TEXT_LONGSTRING',true);
  269. st_widestring:
  270. emitcall ('FPC_WRITE_TEXT_ANSISTRING',true);
  271. end;
  272. end;
  273. pointerdef : begin
  274. if is_equal(ppointerdef(pararesult)^.definition,cchardef) then
  275. begin
  276. if doread then
  277. emitcall('FPC_READ_TEXT_PCHAR_AS_POINTER',true)
  278. else
  279. emitcall('FPC_WRITE_TEXT_PCHAR_AS_POINTER',true);
  280. end;
  281. end;
  282. arraydef : begin
  283. if (parraydef(pararesult)^.lowrange=0) and
  284. is_equal(parraydef(pararesult)^.definition,cchardef) then
  285. begin
  286. if doread then
  287. emitcall('FPC_READ_TEXT_PCHAR_AS_ARRAY',true)
  288. else
  289. emitcall('FPC_WRITE_TEXT_PCHAR_AS_ARRAY',true);
  290. end;
  291. end;
  292. floatdef : begin
  293. if doread then
  294. emitcall('FPC_READ_TEXT_'+float_name[pfloatdef(pararesult)^.typ],true)
  295. else
  296. emitcall('FPC_WRITE_TEXT_'+float_name[pfloatdef(pararesult)^.typ],true);
  297. end;
  298. orddef : begin
  299. case porddef(pararesult)^.typ of
  300. u8bit : if doread then
  301. emitcall('FPC_READ_TEXT_BYTE',true);
  302. s8bit : if doread then
  303. emitcall('FPC_READ_TEXT_SHORTINT',true);
  304. u16bit : if doread then
  305. emitcall('FPC_READ_TEXT_WORD',true);
  306. s16bit : if doread then
  307. emitcall('FPC_READ_TEXT_INTEGER',true);
  308. s32bit : if doread then
  309. emitcall('FPC_READ_TEXT_LONGINT',true)
  310. else
  311. emitcall('FPC_WRITE_TEXT_LONGINT',true);
  312. u32bit : if doread then
  313. emitcall('FPC_READ_TEXT_CARDINAL',true)
  314. else
  315. emitcall('FPC_WRITE_TEXT_CARDINAL',true);
  316. uchar : if doread then
  317. emitcall('FPC_READ_TEXT_CHAR',true)
  318. else
  319. emitcall('FPC_WRITE_TEXT_CHAR',true);
  320. bool8bit,
  321. bool16bit,
  322. bool32bit : if doread then
  323. Message(parser_e_illegal_parameter_list)
  324. else
  325. emitcall('FPC_WRITE_TEXT_BOOLEAN',true);
  326. end;
  327. end;
  328. end;
  329. end;
  330. { load ESI in methods again }
  331. popusedregisters(pushed);
  332. maybe_loadesi;
  333. end;
  334. end;
  335. { Insert end of writing for textfiles }
  336. if ft=ft_text then
  337. begin
  338. pushusedregisters(pushed,$ff);
  339. emit_push_mem(aktfile);
  340. if doread then
  341. begin
  342. if doln then
  343. emitcall('FPC_READLN_END',true)
  344. else
  345. emitcall('FPC_READ_END',true);
  346. end
  347. else
  348. begin
  349. if doln then
  350. emitcall('FPC_WRITELN_END',true)
  351. else
  352. emitcall('FPC_WRITE_END',true);
  353. end;
  354. popusedregisters(pushed);
  355. maybe_loadesi;
  356. end;
  357. { Insert IOCheck if set }
  358. if assigned(iolabel) then
  359. begin
  360. { registers are saved in the procedure }
  361. exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,newcsymbol(lab2str(iolabel),0))));
  362. emitcall('FPC_IOCHECK',true);
  363. end;
  364. { Freeup all used temps }
  365. ungetiftemp(aktfile);
  366. if assigned(p^.left) then
  367. begin
  368. p^.left:=reversparameter(p^.left);
  369. if npara<>nb_para then
  370. Message(cg_f_internal_error_in_secondinline);
  371. hp:=p^.left;
  372. while assigned(hp) do
  373. begin
  374. if assigned(hp^.left) then
  375. if (hp^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  376. ungetiftemp(hp^.left^.location.reference);
  377. hp:=hp^.right;
  378. end;
  379. end;
  380. end;
  381. procedure handle_str;
  382. var
  383. hp,node : ptree;
  384. dummycoll : tdefcoll;
  385. is_real,has_length : boolean;
  386. begin
  387. pushusedregisters(pushed,$ff);
  388. node:=p^.left;
  389. is_real:=false;
  390. has_length:=false;
  391. while assigned(node^.right) do node:=node^.right;
  392. { if a real parameter somewhere then call REALSTR }
  393. if (node^.left^.resulttype^.deftype=floatdef) then
  394. is_real:=true;
  395. node:=p^.left;
  396. { we have at least two args }
  397. { with at max 2 colon_para in between }
  398. { first arg longint or float }
  399. hp:=node;
  400. node:=node^.right;
  401. hp^.right:=nil;
  402. dummycoll.data:=hp^.resulttype;
  403. { string arg }
  404. dummycoll.paratyp:=vs_var;
  405. secondcallparan(hp,@dummycoll,false
  406. ,false,0
  407. );
  408. if codegenerror then
  409. exit;
  410. dummycoll.paratyp:=vs_const;
  411. { second arg }
  412. hp:=node;
  413. node:=node^.right;
  414. hp^.right:=nil;
  415. { frac para }
  416. if hp^.is_colon_para and assigned(node) and
  417. node^.is_colon_para then
  418. begin
  419. dummycoll.data:=hp^.resulttype;
  420. secondcallparan(hp,@dummycoll,false
  421. ,false,0
  422. );
  423. if codegenerror then
  424. exit;
  425. hp:=node;
  426. node:=node^.right;
  427. hp^.right:=nil;
  428. has_length:=true;
  429. end
  430. else
  431. if is_real then
  432. push_int(-1);
  433. { third arg, length only if is_real }
  434. if hp^.is_colon_para then
  435. begin
  436. dummycoll.data:=hp^.resulttype;
  437. secondcallparan(hp,@dummycoll,false
  438. ,false,0
  439. );
  440. if codegenerror then
  441. exit;
  442. hp:=node;
  443. node:=node^.right;
  444. hp^.right:=nil;
  445. end
  446. else
  447. if is_real then
  448. push_int(-32767)
  449. else
  450. push_int(-1);
  451. { last arg longint or real }
  452. secondcallparan(hp,@dummycoll,false
  453. ,false,0
  454. );
  455. if codegenerror then
  456. exit;
  457. if is_real then
  458. emitcall('FPC_STR_'+float_name[pfloatdef(hp^.resulttype)^.typ],true)
  459. else if porddef(hp^.resulttype)^.typ=u32bit then
  460. emitcall('FPC_STR_CARDINAL',true)
  461. else
  462. emitcall('FPC_STR_LONGINT',true);
  463. popusedregisters(pushed);
  464. end;
  465. var
  466. r : preference;
  467. l : longint;
  468. ispushed : boolean;
  469. hregister : tregister;
  470. otlabel,oflabel,filenamestring : plabel;
  471. begin
  472. case p^.inlinenumber of
  473. in_assert_x:
  474. begin
  475. otlabel:=truelabel;
  476. oflabel:=falselabel;
  477. getlabel(truelabel);
  478. getlabel(falselabel);
  479. getlabel(filenamestring);
  480. secondpass(p^.left);
  481. if codegenerror then
  482. exit;
  483. if cs_do_assertion in aktlocalswitches then
  484. begin
  485. maketojumpbool(p^.left);
  486. emitl(A_LABEL,falselabel);
  487. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,
  488. p^.fileinfo.line)));
  489. { generate string }
  490. { push string
  491. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,
  492. p^.fileinfo.line)));
  493. }
  494. emitcall('FPC_DO_ASSERT',true);
  495. emitl(A_LABEL,truelabel);
  496. end;
  497. truelabel:=otlabel;
  498. falselabel:=oflabel;
  499. end;
  500. in_lo_word,
  501. in_hi_word :
  502. begin
  503. secondpass(p^.left);
  504. p^.location.loc:=LOC_REGISTER;
  505. if p^.left^.location.loc<>LOC_REGISTER then
  506. begin
  507. if p^.left^.location.loc=LOC_CREGISTER then
  508. begin
  509. p^.location.register:=reg32toreg16(getregister32);
  510. emit_reg_reg(A_MOV,S_W,p^.left^.location.register,
  511. p^.location.register);
  512. end
  513. else
  514. begin
  515. del_reference(p^.left^.location.reference);
  516. p^.location.register:=reg32toreg16(getregister32);
  517. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,newreference(p^.left^.location.reference),
  518. p^.location.register)));
  519. end;
  520. end
  521. else p^.location.register:=p^.left^.location.register;
  522. if p^.inlinenumber=in_hi_word then
  523. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_W,8,p^.location.register)));
  524. p^.location.register:=reg16toreg8(p^.location.register);
  525. end;
  526. in_high_x :
  527. begin
  528. if is_open_array(p^.left^.resulttype) then
  529. begin
  530. secondpass(p^.left);
  531. del_reference(p^.left^.location.reference);
  532. p^.location.register:=getregister32;
  533. new(r);
  534. reset_reference(r^);
  535. r^.base:=highframepointer;
  536. r^.offset:=highoffset+4;
  537. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  538. r,p^.location.register)));
  539. end
  540. end;
  541. in_sizeof_x,
  542. in_typeof_x :
  543. begin
  544. { sizeof(openarray) handling }
  545. if (p^.inlinenumber=in_sizeof_x) and
  546. is_open_array(p^.left^.resulttype) then
  547. begin
  548. { sizeof(openarray)=high(openarray)+1 }
  549. secondpass(p^.left);
  550. del_reference(p^.left^.location.reference);
  551. p^.location.register:=getregister32;
  552. new(r);
  553. reset_reference(r^);
  554. r^.base:=highframepointer;
  555. r^.offset:=highoffset+4;
  556. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  557. r,p^.location.register)));
  558. exprasmlist^.concat(new(pai386,op_reg(A_INC,S_L,
  559. p^.location.register)));
  560. if parraydef(p^.left^.resulttype)^.elesize<>1 then
  561. exprasmlist^.concat(new(pai386,op_const_reg(A_IMUL,S_L,
  562. parraydef(p^.left^.resulttype)^.elesize,p^.location.register)));
  563. end
  564. else
  565. begin
  566. { for both cases load vmt }
  567. if p^.left^.treetype=typen then
  568. begin
  569. p^.location.register:=getregister32;
  570. exprasmlist^.concat(new(pai386,op_csymbol_reg(A_MOV,
  571. S_L,newcsymbol(pobjectdef(p^.left^.resulttype)^.vmt_mangledname,0),
  572. p^.location.register)));
  573. end
  574. else
  575. begin
  576. secondpass(p^.left);
  577. del_reference(p^.left^.location.reference);
  578. p^.location.loc:=LOC_REGISTER;
  579. p^.location.register:=getregister32;
  580. { load VMT pointer }
  581. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  582. newreference(p^.left^.location.reference),
  583. p^.location.register)));
  584. end;
  585. { in sizeof load size }
  586. if p^.inlinenumber=in_sizeof_x then
  587. begin
  588. new(r);
  589. reset_reference(r^);
  590. r^.base:=p^.location.register;
  591. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,
  592. p^.location.register)));
  593. end;
  594. end;
  595. end;
  596. in_lo_long,
  597. in_hi_long :
  598. begin
  599. secondpass(p^.left);
  600. p^.location.loc:=LOC_REGISTER;
  601. if p^.left^.location.loc<>LOC_REGISTER then
  602. begin
  603. if p^.left^.location.loc=LOC_CREGISTER then
  604. begin
  605. p^.location.register:=getregister32;
  606. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,
  607. p^.location.register);
  608. end
  609. else
  610. begin
  611. del_reference(p^.left^.location.reference);
  612. p^.location.register:=getregister32;
  613. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  614. p^.location.register)));
  615. end;
  616. end
  617. else p^.location.register:=p^.left^.location.register;
  618. if p^.inlinenumber=in_hi_long then
  619. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_L,16,p^.location.register)));
  620. p^.location.register:=reg32toreg16(p^.location.register);
  621. end;
  622. {***CHARBUG}
  623. {We can now comment them out, as they are handled as typecast.
  624. Saves an incredible amount of 8 bytes code.
  625. I'am not lucky about this, because it's _not_ a type cast (FK) }
  626. { in_ord_char,
  627. in_chr_byte,}
  628. {***}
  629. in_length_string :
  630. begin
  631. secondpass(p^.left);
  632. set_location(p^.location,p^.left^.location);
  633. { length in ansi strings is at offset -8 }
  634. {$ifdef UseAnsiString}
  635. if is_ansistring(p^.left^.resulttype) then
  636. dec(p^.location.reference.offset,8);
  637. {$endif UseAnsiString}
  638. end;
  639. in_pred_x,
  640. in_succ_x:
  641. begin
  642. secondpass(p^.left);
  643. if p^.inlinenumber=in_pred_x then
  644. asmop:=A_DEC
  645. else
  646. asmop:=A_INC;
  647. case p^.resulttype^.size of
  648. 4 : opsize:=S_L;
  649. 2 : opsize:=S_W;
  650. 1 : opsize:=S_B;
  651. else
  652. internalerror(10080);
  653. end;
  654. p^.location.loc:=LOC_REGISTER;
  655. if p^.left^.location.loc<>LOC_REGISTER then
  656. begin
  657. p^.location.register:=getregister32;
  658. if (p^.resulttype^.size=2) then
  659. p^.location.register:=reg32toreg16(p^.location.register);
  660. if (p^.resulttype^.size=1) then
  661. p^.location.register:=reg32toreg8(p^.location.register);
  662. if p^.left^.location.loc=LOC_CREGISTER then
  663. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,
  664. p^.location.register)
  665. else
  666. if p^.left^.location.loc=LOC_FLAGS then
  667. exprasmlist^.concat(new(pai386,op_reg(flag_2_set[p^.left^.location.resflags],S_B,
  668. p^.location.register)))
  669. else
  670. begin
  671. del_reference(p^.left^.location.reference);
  672. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(p^.left^.location.reference),
  673. p^.location.register)));
  674. end;
  675. end
  676. else p^.location.register:=p^.left^.location.register;
  677. exprasmlist^.concat(new(pai386,op_reg(asmop,opsize,
  678. p^.location.register)))
  679. { here we should insert bounds check ? }
  680. { and direct call to bounds will crash the program }
  681. { if we are at the limit }
  682. { we could also simply say that pred(first)=first and succ(last)=last }
  683. { could this be usefull I don't think so (PM)
  684. emitoverflowcheck;}
  685. end;
  686. in_dec_x,
  687. in_inc_x :
  688. begin
  689. { set defaults }
  690. addvalue:=1;
  691. addconstant:=true;
  692. { load first parameter, must be a reference }
  693. secondpass(p^.left^.left);
  694. case p^.left^.left^.resulttype^.deftype of
  695. orddef,
  696. enumdef : begin
  697. case p^.left^.left^.resulttype^.size of
  698. 1 : opsize:=S_B;
  699. 2 : opsize:=S_W;
  700. 4 : opsize:=S_L;
  701. end;
  702. end;
  703. pointerdef : begin
  704. opsize:=S_L;
  705. addvalue:=ppointerdef(p^.left^.left^.resulttype)^.definition^.savesize;
  706. end;
  707. else
  708. internalerror(10081);
  709. end;
  710. { second argument specified?, must be a s32bit in register }
  711. if assigned(p^.left^.right) then
  712. begin
  713. secondpass(p^.left^.right^.left);
  714. { when constant, just multiply the addvalue }
  715. if is_constintnode(p^.left^.right^.left) then
  716. addvalue:=addvalue*get_ordinal_value(p^.left^.right^.left)
  717. else
  718. begin
  719. case p^.left^.right^.left^.location.loc of
  720. LOC_REGISTER,
  721. LOC_CREGISTER : hregister:=p^.left^.right^.left^.location.register;
  722. LOC_MEM,
  723. LOC_REFERENCE : begin
  724. del_reference(p^.left^.right^.left^.location.reference);
  725. hregister:=getregister32;
  726. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  727. newreference(p^.left^.right^.left^.location.reference),hregister)));
  728. end;
  729. else
  730. internalerror(10082);
  731. end;
  732. { insert multiply with addvalue if its >1 }
  733. if addvalue>1 then
  734. exprasmlist^.concat(new(pai386,op_const_reg(A_IMUL,opsize,
  735. addvalue,hregister)));
  736. addconstant:=false;
  737. end;
  738. end;
  739. { write the add instruction }
  740. if addconstant then
  741. begin
  742. if (addvalue=1) and not(cs_check_overflow in aktlocalswitches) then
  743. begin
  744. if p^.left^.left^.location.loc=LOC_CREGISTER then
  745. exprasmlist^.concat(new(pai386,op_reg(incdecop[p^.inlinenumber],opsize,
  746. p^.left^.left^.location.register)))
  747. else
  748. exprasmlist^.concat(new(pai386,op_ref(incdecop[p^.inlinenumber],opsize,
  749. newreference(p^.left^.left^.location.reference))))
  750. end
  751. else
  752. begin
  753. if p^.left^.left^.location.loc=LOC_CREGISTER then
  754. exprasmlist^.concat(new(pai386,op_const_reg(addsubop[p^.inlinenumber],opsize,
  755. addvalue,p^.left^.left^.location.register)))
  756. else
  757. exprasmlist^.concat(new(pai386,op_const_ref(addsubop[p^.inlinenumber],opsize,
  758. addvalue,newreference(p^.left^.left^.location.reference))));
  759. end
  760. end
  761. else
  762. begin
  763. if p^.left^.left^.location.loc=LOC_CREGISTER then
  764. exprasmlist^.concat(new(pai386,op_reg_reg(addsubop[p^.inlinenumber],opsize,
  765. hregister,p^.left^.left^.location.register)))
  766. else
  767. exprasmlist^.concat(new(pai386,op_reg_ref(addsubop[p^.inlinenumber],opsize,
  768. hregister,newreference(p^.left^.left^.location.reference))));
  769. ungetregister32(hregister);
  770. end;
  771. emitoverflowcheck(p^.left^.left);
  772. end;
  773. in_assigned_x :
  774. begin
  775. secondpass(p^.left^.left);
  776. p^.location.loc:=LOC_FLAGS;
  777. if (p^.left^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  778. begin
  779. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,
  780. p^.left^.left^.location.register,
  781. p^.left^.left^.location.register)));
  782. ungetregister32(p^.left^.left^.location.register);
  783. end
  784. else
  785. begin
  786. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_L,0,
  787. newreference(p^.left^.left^.location.reference))));
  788. del_reference(p^.left^.left^.location.reference);
  789. end;
  790. p^.location.resflags:=F_NE;
  791. end;
  792. in_reset_typedfile,in_rewrite_typedfile :
  793. begin
  794. pushusedregisters(pushed,$ff);
  795. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,pfiledef(p^.left^.resulttype)^.typed_as^.size)));
  796. secondload(p^.left);
  797. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  798. if p^.inlinenumber=in_reset_typedfile then
  799. emitcall('FPC_RESET_TYPED',true)
  800. else
  801. emitcall('FPC_REWRITE_TYPED',true);
  802. popusedregisters(pushed);
  803. end;
  804. in_write_x :
  805. handlereadwrite(false,false);
  806. in_writeln_x :
  807. handlereadwrite(false,true);
  808. in_read_x :
  809. handlereadwrite(true,false);
  810. in_readln_x :
  811. handlereadwrite(true,true);
  812. in_str_x_string :
  813. begin
  814. handle_str;
  815. maybe_loadesi;
  816. end;
  817. in_include_x_y,
  818. in_exclude_x_y:
  819. begin
  820. secondpass(p^.left^.left);
  821. if p^.left^.right^.left^.treetype=ordconstn then
  822. begin
  823. { calculate bit position }
  824. l:=1 shl (p^.left^.right^.left^.value mod 32);
  825. { determine operator }
  826. if p^.inlinenumber=in_include_x_y then
  827. asmop:=A_OR
  828. else
  829. begin
  830. asmop:=A_AND;
  831. l:=not(l);
  832. end;
  833. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  834. begin
  835. inc(p^.left^.left^.location.reference.offset,(p^.left^.right^.left^.value div 32)*4);
  836. exprasmlist^.concat(new(pai386,op_const_ref(asmop,S_L,
  837. l,newreference(p^.left^.left^.location.reference))));
  838. del_reference(p^.left^.left^.location.reference);
  839. end
  840. else
  841. { LOC_CREGISTER }
  842. exprasmlist^.concat(new(pai386,op_const_reg(asmop,S_L,
  843. l,p^.left^.left^.location.register)));
  844. end
  845. else
  846. begin
  847. { generate code for the element to set }
  848. ispushed:=maybe_push(p^.left^.right^.left^.registers32,p^.left^.left);
  849. secondpass(p^.left^.right^.left);
  850. if ispushed then
  851. restore(p^.left^.left);
  852. { determine asm operator }
  853. if p^.inlinenumber=in_include_x_y then
  854. asmop:=A_BTS
  855. else
  856. asmop:=A_BTR;
  857. if psetdef(p^.left^.resulttype)^.settype=smallset then
  858. begin
  859. if p^.left^.right^.left^.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  860. hregister:=p^.left^.right^.left^.location.register
  861. else
  862. begin
  863. hregister:=R_EDI;
  864. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  865. newreference(p^.left^.right^.left^.location.reference),R_EDI)));
  866. end;
  867. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  868. exprasmlist^.concat(new(pai386,op_reg_ref(asmop,S_L,R_EDI,
  869. newreference(p^.left^.right^.left^.location.reference))))
  870. else
  871. exprasmlist^.concat(new(pai386,op_reg_reg(asmop,S_L,R_EDI,
  872. p^.left^.right^.left^.location.register)));
  873. end
  874. else
  875. begin
  876. end;
  877. end;
  878. end;
  879. else internalerror(9);
  880. end;
  881. end;
  882. end.
  883. {
  884. $Log$
  885. Revision 1.4 1998-09-14 10:43:49 peter
  886. * all internal RTL functions start with FPC_
  887. Revision 1.3 1998/09/05 23:03:57 florian
  888. * some fixes to get -Or work:
  889. - inc/dec didn't take care of CREGISTER
  890. - register calculcation of inc/dec was wrong
  891. - var/const parameters get now assigned 32 bit register, but
  892. const parameters only if they are passed by reference !
  893. Revision 1.2 1998/09/04 08:41:40 peter
  894. * updated some error messages
  895. Revision 1.1 1998/08/31 12:22:14 peter
  896. * secondinline moved to cg386inl
  897. Revision 1.19 1998/08/31 08:52:03 peter
  898. * fixed error 10 with succ() and pref()
  899. Revision 1.18 1998/08/20 21:36:38 peter
  900. * fixed 'with object do' bug
  901. Revision 1.17 1998/08/19 16:07:36 jonas
  902. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  903. Revision 1.16 1998/08/18 09:24:36 pierre
  904. * small warning position bug fixed
  905. * support_mmx switches splitting was missing
  906. * rhide error and warning output corrected
  907. Revision 1.15 1998/08/13 11:00:09 peter
  908. * fixed procedure<>procedure construct
  909. Revision 1.14 1998/08/11 14:05:33 peter
  910. * fixed sizeof(array of char)
  911. Revision 1.13 1998/08/10 14:49:45 peter
  912. + localswitches, moduleswitches, globalswitches splitting
  913. Revision 1.12 1998/07/30 13:30:31 florian
  914. * final implemenation of exception support, maybe it needs
  915. some fixes :)
  916. Revision 1.11 1998/07/24 22:16:52 florian
  917. * internal error 10 together with array access fixed. I hope
  918. that's the final fix.
  919. Revision 1.10 1998/07/18 22:54:23 florian
  920. * some ansi/wide/longstring support fixed:
  921. o parameter passing
  922. o returning as result from functions
  923. Revision 1.9 1998/07/07 17:40:37 peter
  924. * packrecords 4 works
  925. * word aligning of parameters
  926. Revision 1.8 1998/07/06 15:51:15 michael
  927. Added length checking for string reading
  928. Revision 1.7 1998/07/06 14:19:51 michael
  929. + Added calls for reading/writing ansistrings
  930. Revision 1.6 1998/07/01 15:28:48 peter
  931. + better writeln/readln handling, now 100% like tp7
  932. Revision 1.5 1998/06/25 14:04:17 peter
  933. + internal inc/dec
  934. Revision 1.4 1998/06/25 08:48:06 florian
  935. * first version of rtti support
  936. Revision 1.3 1998/06/09 16:01:33 pierre
  937. + added procedure directive parsing for procvars
  938. (accepted are popstack cdecl and pascal)
  939. + added C vars with the following syntax
  940. var C calias 'true_c_name';(can be followed by external)
  941. reason is that you must add the Cprefix
  942. which is target dependent
  943. Revision 1.2 1998/06/08 13:13:29 pierre
  944. + temporary variables now in temp_gen.pas unit
  945. because it is processor independent
  946. * mppc68k.bat modified to undefine i386 and support_mmx
  947. (which are defaults for i386)
  948. Revision 1.1 1998/06/05 17:44:10 peter
  949. * splitted cgi386
  950. }