cg386inl.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 inline nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cg386inl;
  19. interface
  20. uses
  21. tree;
  22. procedure secondinline(var p : ptree);
  23. implementation
  24. uses
  25. globtype,systems,
  26. cobjects,verbose,globals,files,
  27. symtable,aasm,types,
  28. hcodegen,temp_gen,pass_2,
  29. i386,cgai386,tgeni386,cg386ld,cg386cal;
  30. {*****************************************************************************
  31. Helpers
  32. *****************************************************************************}
  33. { reverts the parameter list }
  34. var nb_para : integer;
  35. function reversparameter(p : ptree) : ptree;
  36. var
  37. hp1,hp2 : ptree;
  38. begin
  39. hp1:=nil;
  40. nb_para := 0;
  41. while assigned(p) do
  42. begin
  43. { pull out }
  44. hp2:=p;
  45. p:=p^.right;
  46. inc(nb_para);
  47. { pull in }
  48. hp2^.right:=hp1;
  49. hp1:=hp2;
  50. end;
  51. reversparameter:=hp1;
  52. end;
  53. {*****************************************************************************
  54. SecondInLine
  55. *****************************************************************************}
  56. procedure secondinline(var p : ptree);
  57. const
  58. { tfloattype = (f32bit,s32real,s64real,s80real,s64bit); }
  59. float_name: array[tfloattype] of string[8]=
  60. ('FIXED','SINGLE','REAL','EXTENDED','COMP','FIXED16');
  61. incdecop:array[in_inc_x..in_dec_x] of tasmop=(A_INC,A_DEC);
  62. addsubop:array[in_inc_x..in_dec_x] of tasmop=(A_ADD,A_SUB);
  63. var
  64. aktfile : treference;
  65. ft : tfiletype;
  66. opsize : topsize;
  67. asmop : tasmop;
  68. pushed : tpushed;
  69. {inc/dec}
  70. addconstant : boolean;
  71. addvalue : longint;
  72. procedure handlereadwrite(doread,doln : boolean);
  73. { produces code for READ(LN) and WRITE(LN) }
  74. procedure loadstream;
  75. const
  76. io:array[0..1] of string[7]=('_OUTPUT','_INPUT');
  77. var
  78. r : preference;
  79. begin
  80. new(r);
  81. reset_reference(r^);
  82. r^.symbol:=stringdup('U_'+upper(target_info.system_unit)+io[byte(doread)]);
  83. concat_external(r^.symbol^,EXT_NEAR);
  84. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,r,R_EDI)))
  85. end;
  86. var
  87. node,hp : ptree;
  88. typedtyp,
  89. pararesult : pdef;
  90. has_length : boolean;
  91. dummycoll : tdefcoll;
  92. iolabel : plabel;
  93. npara : longint;
  94. begin
  95. { I/O check }
  96. if (cs_check_io in aktlocalswitches) and
  97. ((aktprocsym^.definition^.options and poiocheck)=0) then
  98. begin
  99. getlabel(iolabel);
  100. emitl(A_LABEL,iolabel);
  101. end
  102. else
  103. iolabel:=nil;
  104. { for write of real with the length specified }
  105. has_length:=false;
  106. hp:=nil;
  107. { reserve temporary pointer to data variable }
  108. aktfile.symbol:=nil;
  109. gettempofsizereference(4,aktfile);
  110. { first state text data }
  111. ft:=ft_text;
  112. { and state a parameter ? }
  113. if p^.left=nil then
  114. begin
  115. { the following instructions are for "writeln;" }
  116. loadstream;
  117. { save @aktfile in temporary variable }
  118. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,newreference(aktfile))));
  119. end
  120. else
  121. begin
  122. { revers paramters }
  123. node:=reversparameter(p^.left);
  124. p^.left := node;
  125. npara := nb_para;
  126. { calculate data variable }
  127. { is first parameter a file type ? }
  128. if node^.left^.resulttype^.deftype=filedef then
  129. begin
  130. ft:=pfiledef(node^.left^.resulttype)^.filetype;
  131. if ft=ft_typed then
  132. typedtyp:=pfiledef(node^.left^.resulttype)^.typed_as;
  133. secondpass(node^.left);
  134. if codegenerror then
  135. exit;
  136. { save reference in temporary variables }
  137. if node^.left^.location.loc<>LOC_REFERENCE then
  138. begin
  139. CGMessage(cg_e_illegal_expression);
  140. exit;
  141. end;
  142. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(node^.left^.location.reference),R_EDI)));
  143. { skip to the next parameter }
  144. node:=node^.right;
  145. end
  146. else
  147. begin
  148. { load stdin/stdout stream }
  149. loadstream;
  150. end;
  151. { save @aktfile in temporary variable }
  152. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,newreference(aktfile))));
  153. if doread then
  154. { parameter by READ gives call by reference }
  155. dummycoll.paratyp:=vs_var
  156. { an WRITE Call by "Const" }
  157. else
  158. dummycoll.paratyp:=vs_const;
  159. { because of secondcallparan, which otherwise attaches }
  160. if ft=ft_typed then
  161. { this is to avoid copy of simple const parameters }
  162. dummycoll.data:=new(pformaldef,init)
  163. else
  164. { I think, this isn't a good solution (FK) }
  165. dummycoll.data:=nil;
  166. while assigned(node) do
  167. begin
  168. pushusedregisters(pushed,$ff);
  169. hp:=node;
  170. node:=node^.right;
  171. hp^.right:=nil;
  172. if hp^.is_colon_para then
  173. CGMessage(parser_e_illegal_colon_qualifier);
  174. if ft=ft_typed then
  175. never_copy_const_param:=true;
  176. { reset data type }
  177. dummycoll.data:=nil;
  178. { support openstring calling for readln(shortstring) }
  179. if doread and (is_shortstring(hp^.resulttype)) then
  180. dummycoll.data:=openshortstringdef;
  181. secondcallparan(hp,@dummycoll,false,false,0);
  182. if ft=ft_typed then
  183. never_copy_const_param:=false;
  184. hp^.right:=node;
  185. if codegenerror then
  186. exit;
  187. emit_push_mem(aktfile);
  188. if (ft=ft_typed) then
  189. begin
  190. { OK let's try this }
  191. { first we must only allow the right type }
  192. { we have to call blockread or blockwrite }
  193. { but the real problem is that }
  194. { reset and rewrite should have set }
  195. { the type size }
  196. { as recordsize for that file !!!! }
  197. { how can we make that }
  198. { I think that is only possible by adding }
  199. { reset and rewrite to the inline list a call }
  200. { allways read only one record by element }
  201. push_int(typedtyp^.size);
  202. if doread then
  203. emitcall('FPC_TYPED_READ',true)
  204. else
  205. emitcall('FPC_TYPED_WRITE',true);
  206. end
  207. else
  208. begin
  209. { save current position }
  210. pararesult:=hp^.left^.resulttype;
  211. { handle possible field width }
  212. { of course only for write(ln) }
  213. if not doread then
  214. begin
  215. { handle total width parameter }
  216. if assigned(node) and node^.is_colon_para then
  217. begin
  218. hp:=node;
  219. node:=node^.right;
  220. hp^.right:=nil;
  221. secondcallparan(hp,@dummycoll,false,false,0);
  222. hp^.right:=node;
  223. if codegenerror then
  224. exit;
  225. has_length:=true;
  226. end
  227. else
  228. if pararesult^.deftype<>floatdef then
  229. push_int(0)
  230. else
  231. push_int(-32767);
  232. { a second colon para for a float ? }
  233. if assigned(node) and node^.is_colon_para then
  234. begin
  235. hp:=node;
  236. node:=node^.right;
  237. hp^.right:=nil;
  238. secondcallparan(hp,@dummycoll,false,false,0);
  239. hp^.right:=node;
  240. if pararesult^.deftype<>floatdef then
  241. CGMessage(parser_e_illegal_colon_qualifier);
  242. if codegenerror then
  243. exit;
  244. end
  245. else
  246. begin
  247. if pararesult^.deftype=floatdef then
  248. push_int(-1);
  249. end
  250. end;
  251. case pararesult^.deftype of
  252. stringdef : begin
  253. if doread then
  254. begin
  255. { push maximum string length }
  256. case pstringdef(pararesult)^.string_typ of
  257. st_shortstring:
  258. emitcall ('FPC_READ_TEXT_STRING',true);
  259. st_ansistring:
  260. emitcall ('FPC_READ_TEXT_ANSISTRING',true);
  261. st_longstring:
  262. emitcall ('FPC_READ_TEXT_LONGSTRING',true);
  263. st_widestring:
  264. emitcall ('FPC_READ_TEXT_ANSISTRING',true);
  265. end
  266. end
  267. else
  268. Case pstringdef(Pararesult)^.string_typ of
  269. st_shortstring:
  270. emitcall ('FPC_WRITE_TEXT_STRING',true);
  271. st_ansistring:
  272. emitcall ('FPC_WRITE_TEXT_ANSISTRING',true);
  273. st_longstring:
  274. emitcall ('FPC_WRITE_TEXT_LONGSTRING',true);
  275. st_widestring:
  276. emitcall ('FPC_WRITE_TEXT_ANSISTRING',true);
  277. end;
  278. end;
  279. pointerdef : begin
  280. if is_equal(ppointerdef(pararesult)^.definition,cchardef) then
  281. begin
  282. if doread then
  283. emitcall('FPC_READ_TEXT_PCHAR_AS_POINTER',true)
  284. else
  285. emitcall('FPC_WRITE_TEXT_PCHAR_AS_POINTER',true);
  286. end;
  287. end;
  288. arraydef : begin
  289. if is_chararray(pararesult) then
  290. begin
  291. if doread then
  292. emitcall('FPC_READ_TEXT_PCHAR_AS_ARRAY',true)
  293. else
  294. emitcall('FPC_WRITE_TEXT_PCHAR_AS_ARRAY',true);
  295. end;
  296. end;
  297. floatdef : begin
  298. if doread then
  299. emitcall('FPC_READ_TEXT_'+float_name[pfloatdef(pararesult)^.typ],true)
  300. else
  301. emitcall('FPC_WRITE_TEXT_'+float_name[pfloatdef(pararesult)^.typ],true);
  302. end;
  303. orddef : begin
  304. case porddef(pararesult)^.typ of
  305. u8bit : if doread then
  306. emitcall('FPC_READ_TEXT_BYTE',true);
  307. s8bit : if doread then
  308. emitcall('FPC_READ_TEXT_SHORTINT',true);
  309. u16bit : if doread then
  310. emitcall('FPC_READ_TEXT_WORD',true);
  311. s16bit : if doread then
  312. emitcall('FPC_READ_TEXT_INTEGER',true);
  313. s32bit : if doread then
  314. emitcall('FPC_READ_TEXT_LONGINT',true)
  315. else
  316. emitcall('FPC_WRITE_TEXT_LONGINT',true);
  317. u32bit : if doread then
  318. emitcall('FPC_READ_TEXT_CARDINAL',true)
  319. else
  320. emitcall('FPC_WRITE_TEXT_CARDINAL',true);
  321. uchar : if doread then
  322. emitcall('FPC_READ_TEXT_CHAR',true)
  323. else
  324. emitcall('FPC_WRITE_TEXT_CHAR',true);
  325. s64bitint:
  326. if doread then
  327. emitcall('FPC_READ_TEXT_INT64',true)
  328. else
  329. emitcall('FPC_WRITE_TEXT_INT64',true);
  330. u64bit : if doread then
  331. emitcall('FPC_READ_TEXT_QWORD',true)
  332. else
  333. emitcall('FPC_WRITE_TEXT_QWORD',true);
  334. bool8bit,
  335. bool16bit,
  336. bool32bit : if doread then
  337. CGMessage(parser_e_illegal_parameter_list)
  338. else
  339. emitcall('FPC_WRITE_TEXT_BOOLEAN',true);
  340. end;
  341. end;
  342. end;
  343. end;
  344. { load ESI in methods again }
  345. popusedregisters(pushed);
  346. maybe_loadesi;
  347. end;
  348. end;
  349. { Insert end of writing for textfiles }
  350. if ft=ft_text then
  351. begin
  352. pushusedregisters(pushed,$ff);
  353. emit_push_mem(aktfile);
  354. if doread then
  355. begin
  356. if doln then
  357. emitcall('FPC_READLN_END',true)
  358. else
  359. emitcall('FPC_READ_END',true);
  360. end
  361. else
  362. begin
  363. if doln then
  364. emitcall('FPC_WRITELN_END',true)
  365. else
  366. emitcall('FPC_WRITE_END',true);
  367. end;
  368. popusedregisters(pushed);
  369. maybe_loadesi;
  370. end;
  371. { Insert IOCheck if set }
  372. if assigned(iolabel) then
  373. begin
  374. { registers are saved in the procedure }
  375. exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,newcsymbol(lab2str(iolabel),0))));
  376. emitcall('FPC_IOCHECK',true);
  377. end;
  378. { Freeup all used temps }
  379. ungetiftemp(aktfile);
  380. if assigned(p^.left) then
  381. begin
  382. p^.left:=reversparameter(p^.left);
  383. if npara<>nb_para then
  384. CGMessage(cg_f_internal_error_in_secondinline);
  385. hp:=p^.left;
  386. while assigned(hp) do
  387. begin
  388. if assigned(hp^.left) then
  389. if (hp^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  390. ungetiftemp(hp^.left^.location.reference);
  391. hp:=hp^.right;
  392. end;
  393. end;
  394. end;
  395. procedure handle_str;
  396. var
  397. hp,node : ptree;
  398. dummycoll : tdefcoll;
  399. is_real,has_length : boolean;
  400. procedureprefix : string;
  401. begin
  402. pushusedregisters(pushed,$ff);
  403. node:=p^.left;
  404. is_real:=false;
  405. has_length:=false;
  406. while assigned(node^.right) do node:=node^.right;
  407. { if a real parameter somewhere then call REALSTR }
  408. if (node^.left^.resulttype^.deftype=floatdef) then
  409. is_real:=true;
  410. node:=p^.left;
  411. { we have at least two args }
  412. { with at max 2 colon_para in between }
  413. { string arg }
  414. hp:=node;
  415. node:=node^.right;
  416. hp^.right:=nil;
  417. dummycoll.paratyp:=vs_var;
  418. if is_shortstring(hp^.resulttype) then
  419. dummycoll.data:=openshortstringdef
  420. else
  421. dummycoll.data:=hp^.resulttype;
  422. case pstringdef(hp^.resulttype)^.string_typ of
  423. st_widestring:
  424. procedureprefix:='FPC_STRWIDE_';
  425. st_ansistring:
  426. procedureprefix:='FPC_STRANSI_';
  427. st_shortstring:
  428. procedureprefix:='FPC_STR_';
  429. st_longstring:
  430. procedureprefix:='FPC_STRLONG_';
  431. end;
  432. secondcallparan(hp,@dummycoll,false,false,0);
  433. if codegenerror then
  434. exit;
  435. dummycoll.paratyp:=vs_const;
  436. disposetree(p^.left);
  437. p^.left:=nil;
  438. { second arg }
  439. hp:=node;
  440. node:=node^.right;
  441. hp^.right:=nil;
  442. { frac para }
  443. if hp^.is_colon_para and assigned(node) and
  444. node^.is_colon_para then
  445. begin
  446. dummycoll.data:=hp^.resulttype;
  447. secondcallparan(hp,@dummycoll,false
  448. ,false,0
  449. );
  450. if codegenerror then
  451. exit;
  452. disposetree(hp);
  453. hp:=node;
  454. node:=node^.right;
  455. hp^.right:=nil;
  456. has_length:=true;
  457. end
  458. else
  459. if is_real then
  460. push_int(-1);
  461. { third arg, length only if is_real }
  462. if hp^.is_colon_para then
  463. begin
  464. dummycoll.data:=hp^.resulttype;
  465. secondcallparan(hp,@dummycoll,false
  466. ,false,0
  467. );
  468. if codegenerror then
  469. exit;
  470. disposetree(hp);
  471. hp:=node;
  472. node:=node^.right;
  473. hp^.right:=nil;
  474. end
  475. else
  476. if is_real then
  477. push_int(-32767)
  478. else
  479. push_int(-1);
  480. { last arg longint or real }
  481. secondcallparan(hp,@dummycoll,false
  482. ,false,0
  483. );
  484. disposetree(hp);
  485. if codegenerror then
  486. exit;
  487. if is_real then
  488. emitcall(procedureprefix++float_name[pfloatdef(hp^.resulttype)^.typ],true)
  489. else
  490. case porddef(hp^.resulttype)^.typ of
  491. u32bit:
  492. emitcall(procedureprefix+'CARDINAL',true);
  493. u64bit:
  494. emitcall(procedureprefix+'QWORD',true);
  495. s64bitint:
  496. emitcall(procedureprefix+'INT64',true);
  497. else
  498. emitcall(procedureprefix+'LONGINT',true);
  499. end;
  500. popusedregisters(pushed);
  501. end;
  502. var
  503. r : preference;
  504. hp : ptree;
  505. l : longint;
  506. ispushed : boolean;
  507. hregister : tregister;
  508. otlabel,oflabel : plabel;
  509. oldpushedparasize : longint;
  510. begin
  511. { save & reset pushedparasize }
  512. oldpushedparasize:=pushedparasize;
  513. pushedparasize:=0;
  514. case p^.inlinenumber of
  515. in_assert_x_y:
  516. begin
  517. otlabel:=truelabel;
  518. oflabel:=falselabel;
  519. getlabel(truelabel);
  520. getlabel(falselabel);
  521. secondpass(p^.left^.left);
  522. if cs_do_assertion in aktlocalswitches then
  523. begin
  524. maketojumpbool(p^.left^.left);
  525. emitl(A_LABEL,falselabel);
  526. { erroraddr }
  527. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EBP)));
  528. { lineno }
  529. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,aktfilepos.line)));
  530. { filename string }
  531. hp:=genstringconstnode(current_module^.sourcefiles^.get_file_name(aktfilepos.fileindex));
  532. secondpass(hp);
  533. if codegenerror then
  534. exit;
  535. emitpushreferenceaddr(exprasmlist,hp^.location.reference);
  536. disposetree(hp);
  537. { push msg }
  538. secondpass(p^.left^.right^.left);
  539. emitpushreferenceaddr(exprasmlist,p^.left^.right^.left^.location.reference);
  540. { call }
  541. emitcall('FPC_ASSERT',true);
  542. emitl(A_LABEL,truelabel);
  543. end;
  544. freelabel(truelabel);
  545. freelabel(falselabel);
  546. truelabel:=otlabel;
  547. falselabel:=oflabel;
  548. end;
  549. in_lo_word,
  550. in_hi_word :
  551. begin
  552. secondpass(p^.left);
  553. p^.location.loc:=LOC_REGISTER;
  554. if p^.left^.location.loc<>LOC_REGISTER then
  555. begin
  556. if p^.left^.location.loc=LOC_CREGISTER then
  557. begin
  558. p^.location.register:=reg32toreg16(getregister32);
  559. emit_reg_reg(A_MOV,S_W,p^.left^.location.register,
  560. p^.location.register);
  561. end
  562. else
  563. begin
  564. del_reference(p^.left^.location.reference);
  565. p^.location.register:=reg32toreg16(getregister32);
  566. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,newreference(p^.left^.location.reference),
  567. p^.location.register)));
  568. end;
  569. end
  570. else p^.location.register:=p^.left^.location.register;
  571. if p^.inlinenumber=in_hi_word then
  572. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_W,8,p^.location.register)));
  573. p^.location.register:=reg16toreg8(p^.location.register);
  574. end;
  575. {$ifdef OLDHIGH}
  576. in_high_x :
  577. begin
  578. if is_open_array(p^.left^.resulttype) or
  579. is_open_string(p^.left^.resulttype) then
  580. begin
  581. secondpass(p^.left);
  582. del_reference(p^.left^.location.reference);
  583. p^.location.register:=getregister32;
  584. r:=new_reference(highframepointer,highoffset+4);
  585. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  586. r,p^.location.register)));
  587. end
  588. end;
  589. {$endif OLDHIGH}
  590. in_sizeof_x,
  591. in_typeof_x :
  592. begin
  593. {$ifdef OLDHIGH}
  594. { sizeof(openarray) handling }
  595. if (p^.inlinenumber=in_sizeof_x) and
  596. (is_open_array(p^.left^.resulttype) or
  597. is_open_string(p^.left^.resulttype)) then
  598. begin
  599. { sizeof(openarray)=high(openarray)+1 }
  600. secondpass(p^.left);
  601. del_reference(p^.left^.location.reference);
  602. p^.location.register:=getregister32;
  603. r:=new_reference(highframepointer,highoffset+4);
  604. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  605. r,p^.location.register)));
  606. exprasmlist^.concat(new(pai386,op_reg(A_INC,S_L,
  607. p^.location.register)));
  608. if (p^.left^.resulttype^.deftype=arraydef) and
  609. (parraydef(p^.left^.resulttype)^.elesize<>1) then
  610. exprasmlist^.concat(new(pai386,op_const_reg(A_IMUL,S_L,
  611. parraydef(p^.left^.resulttype)^.elesize,p^.location.register)));
  612. end
  613. else
  614. {$endif OLDHIGH}
  615. begin
  616. { for both cases load vmt }
  617. if p^.left^.treetype=typen then
  618. begin
  619. p^.location.register:=getregister32;
  620. exprasmlist^.concat(new(pai386,op_csymbol_reg(A_MOV,
  621. S_L,newcsymbol(pobjectdef(p^.left^.resulttype)^.vmt_mangledname,0),
  622. p^.location.register)));
  623. end
  624. else
  625. begin
  626. secondpass(p^.left);
  627. del_reference(p^.left^.location.reference);
  628. p^.location.loc:=LOC_REGISTER;
  629. p^.location.register:=getregister32;
  630. { load VMT pointer }
  631. inc(p^.left^.location.reference.offset,
  632. pobjectdef(p^.left^.resulttype)^.vmt_offset);
  633. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  634. newreference(p^.left^.location.reference),
  635. p^.location.register)));
  636. end;
  637. { in sizeof load size }
  638. if p^.inlinenumber=in_sizeof_x then
  639. begin
  640. new(r);
  641. reset_reference(r^);
  642. r^.base:=p^.location.register;
  643. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,
  644. p^.location.register)));
  645. end;
  646. end;
  647. end;
  648. in_lo_long,
  649. in_hi_long :
  650. begin
  651. secondpass(p^.left);
  652. p^.location.loc:=LOC_REGISTER;
  653. if p^.left^.location.loc<>LOC_REGISTER then
  654. begin
  655. if p^.left^.location.loc=LOC_CREGISTER then
  656. begin
  657. p^.location.register:=getregister32;
  658. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,
  659. p^.location.register);
  660. end
  661. else
  662. begin
  663. del_reference(p^.left^.location.reference);
  664. p^.location.register:=getregister32;
  665. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  666. p^.location.register)));
  667. end;
  668. end
  669. else p^.location.register:=p^.left^.location.register;
  670. if p^.inlinenumber=in_hi_long then
  671. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_L,16,p^.location.register)));
  672. p^.location.register:=reg32toreg16(p^.location.register);
  673. end;
  674. in_length_string :
  675. begin
  676. secondpass(p^.left);
  677. set_location(p^.location,p^.left^.location);
  678. { length in ansi strings is at offset -8 }
  679. if is_ansistring(p^.left^.resulttype) then
  680. dec(p^.location.reference.offset,8)
  681. { char is always 1, so make it a constant value }
  682. else if is_char(p^.left^.resulttype) then
  683. begin
  684. clear_location(p^.location);
  685. p^.location.loc:=LOC_MEM;
  686. p^.location.reference.isintvalue:=true;
  687. p^.location.reference.offset:=1;
  688. end;
  689. end;
  690. in_pred_x,
  691. in_succ_x:
  692. begin
  693. secondpass(p^.left);
  694. if p^.inlinenumber=in_pred_x then
  695. asmop:=A_DEC
  696. else
  697. asmop:=A_INC;
  698. case p^.resulttype^.size of
  699. 4 : opsize:=S_L;
  700. 2 : opsize:=S_W;
  701. 1 : opsize:=S_B;
  702. else
  703. internalerror(10080);
  704. end;
  705. p^.location.loc:=LOC_REGISTER;
  706. if p^.left^.location.loc<>LOC_REGISTER then
  707. begin
  708. p^.location.register:=getregister32;
  709. if (p^.resulttype^.size=2) then
  710. p^.location.register:=reg32toreg16(p^.location.register);
  711. if (p^.resulttype^.size=1) then
  712. p^.location.register:=reg32toreg8(p^.location.register);
  713. if p^.left^.location.loc=LOC_CREGISTER then
  714. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,
  715. p^.location.register)
  716. else
  717. if p^.left^.location.loc=LOC_FLAGS then
  718. exprasmlist^.concat(new(pai386,op_reg(flag_2_set[p^.left^.location.resflags],S_B,
  719. p^.location.register)))
  720. else
  721. begin
  722. del_reference(p^.left^.location.reference);
  723. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(p^.left^.location.reference),
  724. p^.location.register)));
  725. end;
  726. end
  727. else p^.location.register:=p^.left^.location.register;
  728. exprasmlist^.concat(new(pai386,op_reg(asmop,opsize,
  729. p^.location.register)));
  730. emitoverflowcheck(p);
  731. emitrangecheck(p,p^.resulttype);
  732. end;
  733. in_dec_x,
  734. in_inc_x :
  735. begin
  736. { set defaults }
  737. addvalue:=1;
  738. addconstant:=true;
  739. { load first parameter, must be a reference }
  740. secondpass(p^.left^.left);
  741. case p^.left^.left^.resulttype^.deftype of
  742. orddef,
  743. enumdef : begin
  744. case p^.left^.left^.resulttype^.size of
  745. 1 : opsize:=S_B;
  746. 2 : opsize:=S_W;
  747. 4 : opsize:=S_L;
  748. end;
  749. end;
  750. pointerdef : begin
  751. opsize:=S_L;
  752. if porddef(ppointerdef(p^.left^.left^.resulttype)^.definition)=voiddef then
  753. addvalue:=1
  754. else
  755. addvalue:=ppointerdef(p^.left^.left^.resulttype)^.definition^.savesize;
  756. end;
  757. else
  758. internalerror(10081);
  759. end;
  760. { second argument specified?, must be a s32bit in register }
  761. if assigned(p^.left^.right) then
  762. begin
  763. secondpass(p^.left^.right^.left);
  764. { when constant, just multiply the addvalue }
  765. if is_constintnode(p^.left^.right^.left) then
  766. addvalue:=addvalue*get_ordinal_value(p^.left^.right^.left)
  767. else
  768. begin
  769. case p^.left^.right^.left^.location.loc of
  770. LOC_REGISTER,
  771. LOC_CREGISTER : hregister:=p^.left^.right^.left^.location.register;
  772. LOC_MEM,
  773. LOC_REFERENCE : begin
  774. del_reference(p^.left^.right^.left^.location.reference);
  775. hregister:=getregister32;
  776. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  777. newreference(p^.left^.right^.left^.location.reference),hregister)));
  778. end;
  779. else
  780. internalerror(10082);
  781. end;
  782. { insert multiply with addvalue if its >1 }
  783. if addvalue>1 then
  784. exprasmlist^.concat(new(pai386,op_const_reg(A_IMUL,opsize,
  785. addvalue,hregister)));
  786. addconstant:=false;
  787. end;
  788. end;
  789. { write the add instruction }
  790. if addconstant then
  791. begin
  792. if (addvalue=1) and not(cs_check_overflow in aktlocalswitches) then
  793. begin
  794. if p^.left^.left^.location.loc=LOC_CREGISTER then
  795. exprasmlist^.concat(new(pai386,op_reg(incdecop[p^.inlinenumber],opsize,
  796. p^.left^.left^.location.register)))
  797. else
  798. exprasmlist^.concat(new(pai386,op_ref(incdecop[p^.inlinenumber],opsize,
  799. newreference(p^.left^.left^.location.reference))))
  800. end
  801. else
  802. begin
  803. if p^.left^.left^.location.loc=LOC_CREGISTER then
  804. exprasmlist^.concat(new(pai386,op_const_reg(addsubop[p^.inlinenumber],opsize,
  805. addvalue,p^.left^.left^.location.register)))
  806. else
  807. exprasmlist^.concat(new(pai386,op_const_ref(addsubop[p^.inlinenumber],opsize,
  808. addvalue,newreference(p^.left^.left^.location.reference))));
  809. end
  810. end
  811. else
  812. begin
  813. { BUG HERE : detected with nasm :
  814. hregister is allways 32 bit
  815. it should be converted to 16 or 8 bit depending on op_size PM }
  816. { still not perfect :
  817. if hregister is already a 16 bit reg ?? PM }
  818. case opsize of
  819. S_B : hregister:=reg32toreg8(hregister);
  820. S_W : hregister:=reg32toreg16(hregister);
  821. end;
  822. if p^.left^.left^.location.loc=LOC_CREGISTER then
  823. exprasmlist^.concat(new(pai386,op_reg_reg(addsubop[p^.inlinenumber],opsize,
  824. hregister,p^.left^.left^.location.register)))
  825. else
  826. exprasmlist^.concat(new(pai386,op_reg_ref(addsubop[p^.inlinenumber],opsize,
  827. hregister,newreference(p^.left^.left^.location.reference))));
  828. case opsize of
  829. S_B : hregister:=reg8toreg32(hregister);
  830. S_W : hregister:=reg16toreg32(hregister);
  831. end;
  832. ungetregister32(hregister);
  833. end;
  834. emitoverflowcheck(p^.left^.left);
  835. emitrangecheck(p^.left^.left,p^.left^.left^.resulttype);
  836. end;
  837. in_assigned_x :
  838. begin
  839. secondpass(p^.left^.left);
  840. p^.location.loc:=LOC_FLAGS;
  841. if (p^.left^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  842. begin
  843. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,
  844. p^.left^.left^.location.register,
  845. p^.left^.left^.location.register)));
  846. ungetregister32(p^.left^.left^.location.register);
  847. end
  848. else
  849. begin
  850. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_L,0,
  851. newreference(p^.left^.left^.location.reference))));
  852. del_reference(p^.left^.left^.location.reference);
  853. end;
  854. p^.location.resflags:=F_NE;
  855. end;
  856. in_reset_typedfile,in_rewrite_typedfile :
  857. begin
  858. pushusedregisters(pushed,$ff);
  859. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,pfiledef(p^.left^.resulttype)^.typed_as^.size)));
  860. secondload(p^.left);
  861. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  862. if p^.inlinenumber=in_reset_typedfile then
  863. emitcall('FPC_RESET_TYPED',true)
  864. else
  865. emitcall('FPC_REWRITE_TYPED',true);
  866. popusedregisters(pushed);
  867. end;
  868. in_write_x :
  869. handlereadwrite(false,false);
  870. in_writeln_x :
  871. handlereadwrite(false,true);
  872. in_read_x :
  873. handlereadwrite(true,false);
  874. in_readln_x :
  875. handlereadwrite(true,true);
  876. in_str_x_string :
  877. begin
  878. handle_str;
  879. maybe_loadesi;
  880. end;
  881. in_include_x_y,
  882. in_exclude_x_y:
  883. begin
  884. secondpass(p^.left^.left);
  885. if p^.left^.right^.left^.treetype=ordconstn then
  886. begin
  887. { calculate bit position }
  888. l:=1 shl (p^.left^.right^.left^.value mod 32);
  889. { determine operator }
  890. if p^.inlinenumber=in_include_x_y then
  891. asmop:=A_OR
  892. else
  893. begin
  894. asmop:=A_AND;
  895. l:=not(l);
  896. end;
  897. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  898. begin
  899. inc(p^.left^.left^.location.reference.offset,(p^.left^.right^.left^.value div 32)*4);
  900. exprasmlist^.concat(new(pai386,op_const_ref(asmop,S_L,
  901. l,newreference(p^.left^.left^.location.reference))));
  902. del_reference(p^.left^.left^.location.reference);
  903. end
  904. else
  905. { LOC_CREGISTER }
  906. exprasmlist^.concat(new(pai386,op_const_reg(asmop,S_L,
  907. l,p^.left^.left^.location.register)));
  908. end
  909. else
  910. begin
  911. { generate code for the element to set }
  912. ispushed:=maybe_push(p^.left^.right^.left^.registers32,p^.left^.left);
  913. secondpass(p^.left^.right^.left);
  914. if ispushed then
  915. restore(p^.left^.left);
  916. { determine asm operator }
  917. if p^.inlinenumber=in_include_x_y then
  918. asmop:=A_BTS
  919. else
  920. asmop:=A_BTR;
  921. if psetdef(p^.left^.resulttype)^.settype=smallset then
  922. begin
  923. if p^.left^.right^.left^.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  924. hregister:=p^.left^.right^.left^.location.register
  925. else
  926. begin
  927. hregister:=R_EDI;
  928. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  929. newreference(p^.left^.right^.left^.location.reference),R_EDI)));
  930. end;
  931. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  932. exprasmlist^.concat(new(pai386,op_reg_ref(asmop,S_L,hregister,
  933. newreference(p^.left^.right^.left^.location.reference))))
  934. else
  935. exprasmlist^.concat(new(pai386,op_reg_reg(asmop,S_L,hregister,
  936. p^.left^.right^.left^.location.register)));
  937. end
  938. else
  939. begin
  940. pushsetelement(p^.left^.right^.left);
  941. { normset is allways a ref }
  942. emitpushreferenceaddr(exprasmlist,
  943. p^.left^.left^.location.reference);
  944. if p^.inlinenumber=in_include_x_y then
  945. emitcall('FPC_SET_SET_BYTE',true)
  946. else
  947. emitcall('FPC_SET_UNSET_BYTE',true);
  948. {CGMessage(cg_e_include_not_implemented);}
  949. end;
  950. end;
  951. end;
  952. else internalerror(9);
  953. end;
  954. { reset pushedparasize }
  955. pushedparasize:=oldpushedparasize;
  956. end;
  957. end.
  958. {
  959. $Log$
  960. Revision 1.24 1999-01-21 22:10:39 peter
  961. * fixed array of const
  962. * generic platform independent high() support
  963. Revision 1.23 1999/01/06 12:23:29 florian
  964. * str(...) for ansi/long and widestrings fixed
  965. Revision 1.22 1998/12/11 23:36:07 florian
  966. + again more stuff for int64/qword:
  967. - comparision operators
  968. - code generation for: str, read(ln), write(ln)
  969. Revision 1.21 1998/12/11 00:02:50 peter
  970. + globtype,tokens,version unit splitted from globals
  971. Revision 1.20 1998/11/27 14:50:32 peter
  972. + open strings, $P switch support
  973. Revision 1.19 1998/11/26 13:10:40 peter
  974. * new int - int conversion -dNEWCNV
  975. * some function renamings
  976. Revision 1.18 1998/11/24 17:04:27 peter
  977. * fixed length(char) when char is a variable
  978. Revision 1.17 1998/11/05 12:02:33 peter
  979. * released useansistring
  980. * removed -Sv, its now available in fpc modes
  981. Revision 1.16 1998/10/22 17:11:13 pierre
  982. + terminated the include exclude implementation for i386
  983. * enums inside records fixed
  984. Revision 1.15 1998/10/21 15:12:50 pierre
  985. * bug fix for IOCHECK inside a procedure with iocheck modifier
  986. * removed the GPF for unexistant overloading
  987. (firstcall was called with procedinition=nil !)
  988. * changed typen to what Florian proposed
  989. gentypenode(p : pdef) sets the typenodetype field
  990. and resulttype is only set if inside bt_type block !
  991. Revision 1.14 1998/10/20 08:06:40 pierre
  992. * several memory corruptions due to double freemem solved
  993. => never use p^.loc.location:=p^.left^.loc.location;
  994. + finally I added now by default
  995. that ra386dir translates global and unit symbols
  996. + added a first field in tsymtable and
  997. a nextsym field in tsym
  998. (this allows to obtain ordered type info for
  999. records and objects in gdb !)
  1000. Revision 1.13 1998/10/13 16:50:02 pierre
  1001. * undid some changes of Peter that made the compiler wrong
  1002. for m68k (I had to reinsert some ifdefs)
  1003. * removed several memory leaks under m68k
  1004. * removed the meory leaks for assembler readers
  1005. * cross compiling shoud work again better
  1006. ( crosscompiling sysamiga works
  1007. but as68k still complain about some code !)
  1008. Revision 1.12 1998/10/08 17:17:12 pierre
  1009. * current_module old scanner tagged as invalid if unit is recompiled
  1010. + added ppheap for better info on tracegetmem of heaptrc
  1011. (adds line column and file index)
  1012. * several memory leaks removed ith help of heaptrc !!
  1013. Revision 1.11 1998/10/05 21:33:15 peter
  1014. * fixed 161,165,166,167,168
  1015. Revision 1.10 1998/10/05 12:32:44 peter
  1016. + assert() support
  1017. Revision 1.8 1998/10/02 10:35:09 peter
  1018. * support for inc(pointer,value) which now increases with value instead
  1019. of 0*value :)
  1020. Revision 1.7 1998/09/21 08:45:07 pierre
  1021. + added vmt_offset in tobjectdef.write for fututre use
  1022. (first steps to have objects without vmt if no virtual !!)
  1023. + added fpu_used field for tabstractprocdef :
  1024. sets this level to 2 if the functions return with value in FPU
  1025. (is then set to correct value at parsing of implementation)
  1026. THIS MIGHT refuse some code with FPU expression too complex
  1027. that were accepted before and even in some cases
  1028. that don't overflow in fact
  1029. ( like if f : float; is a forward that finally in implementation
  1030. only uses one fpu register !!)
  1031. Nevertheless I think that it will improve security on
  1032. FPU operations !!
  1033. * most other changes only for UseBrowser code
  1034. (added symtable references for record and objects)
  1035. local switch for refs to args and local of each function
  1036. (static symtable still missing)
  1037. UseBrowser still not stable and probably broken by
  1038. the definition hash array !!
  1039. Revision 1.6 1998/09/20 12:26:37 peter
  1040. * merged fixes
  1041. Revision 1.5 1998/09/17 09:42:15 peter
  1042. + pass_2 for cg386
  1043. * Message() -> CGMessage() for pass_1/pass_2
  1044. Revision 1.4 1998/09/14 10:43:49 peter
  1045. * all internal RTL functions start with FPC_
  1046. Revision 1.3.2.1 1998/09/20 12:20:07 peter
  1047. * Fixed stack not on 4 byte boundary when doing a call
  1048. Revision 1.3 1998/09/05 23:03:57 florian
  1049. * some fixes to get -Or work:
  1050. - inc/dec didn't take care of CREGISTER
  1051. - register calculcation of inc/dec was wrong
  1052. - var/const parameters get now assigned 32 bit register, but
  1053. const parameters only if they are passed by reference !
  1054. Revision 1.2 1998/09/04 08:41:40 peter
  1055. * updated some error CGMessages
  1056. Revision 1.1 1998/08/31 12:22:14 peter
  1057. * secondinline moved to cg386inl
  1058. Revision 1.19 1998/08/31 08:52:03 peter
  1059. * fixed error 10 with succ() and pref()
  1060. Revision 1.18 1998/08/20 21:36:38 peter
  1061. * fixed 'with object do' bug
  1062. Revision 1.17 1998/08/19 16:07:36 jonas
  1063. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1064. Revision 1.16 1998/08/18 09:24:36 pierre
  1065. * small warning position bug fixed
  1066. * support_mmx switches splitting was missing
  1067. * rhide error and warning output corrected
  1068. Revision 1.15 1998/08/13 11:00:09 peter
  1069. * fixed procedure<>procedure construct
  1070. Revision 1.14 1998/08/11 14:05:33 peter
  1071. * fixed sizeof(array of char)
  1072. Revision 1.13 1998/08/10 14:49:45 peter
  1073. + localswitches, moduleswitches, globalswitches splitting
  1074. Revision 1.12 1998/07/30 13:30:31 florian
  1075. * final implemenation of exception support, maybe it needs
  1076. some fixes :)
  1077. Revision 1.11 1998/07/24 22:16:52 florian
  1078. * internal error 10 together with array access fixed. I hope
  1079. that's the final fix.
  1080. Revision 1.10 1998/07/18 22:54:23 florian
  1081. * some ansi/wide/longstring support fixed:
  1082. o parameter passing
  1083. o returning as result from functions
  1084. Revision 1.9 1998/07/07 17:40:37 peter
  1085. * packrecords 4 works
  1086. * word aligning of parameters
  1087. Revision 1.8 1998/07/06 15:51:15 michael
  1088. Added length checking for string reading
  1089. Revision 1.7 1998/07/06 14:19:51 michael
  1090. + Added calls for reading/writing ansistrings
  1091. Revision 1.6 1998/07/01 15:28:48 peter
  1092. + better writeln/readln handling, now 100% like tp7
  1093. Revision 1.5 1998/06/25 14:04:17 peter
  1094. + internal inc/dec
  1095. Revision 1.4 1998/06/25 08:48:06 florian
  1096. * first version of rtti support
  1097. Revision 1.3 1998/06/09 16:01:33 pierre
  1098. + added procedure directive parsing for procvars
  1099. (accepted are popstack cdecl and pascal)
  1100. + added C vars with the following syntax
  1101. var C calias 'true_c_name';(can be followed by external)
  1102. reason is that you must add the Cprefix
  1103. which is target dependent
  1104. Revision 1.2 1998/06/08 13:13:29 pierre
  1105. + temporary variables now in temp_gen.pas unit
  1106. because it is processor independent
  1107. * mppc68k.bat modified to undefine i386 and support_mmx
  1108. (which are defaults for i386)
  1109. Revision 1.1 1998/06/05 17:44:10 peter
  1110. * splitted cgi386
  1111. }