cg386inl.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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,files,
  26. symtable,aasm,types,
  27. hcodegen,temp_gen,pass_2,
  28. i386,cgai386,tgeni386,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. CGMessage(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. CGMessage(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. CGMessage(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 is_chararray(pararesult) then
  284. begin
  285. if doread then
  286. emitcall('FPC_READ_TEXT_PCHAR_AS_ARRAY',true)
  287. else
  288. emitcall('FPC_WRITE_TEXT_PCHAR_AS_ARRAY',true);
  289. end;
  290. end;
  291. floatdef : begin
  292. if doread then
  293. emitcall('FPC_READ_TEXT_'+float_name[pfloatdef(pararesult)^.typ],true)
  294. else
  295. emitcall('FPC_WRITE_TEXT_'+float_name[pfloatdef(pararesult)^.typ],true);
  296. end;
  297. orddef : begin
  298. case porddef(pararesult)^.typ of
  299. u8bit : if doread then
  300. emitcall('FPC_READ_TEXT_BYTE',true);
  301. s8bit : if doread then
  302. emitcall('FPC_READ_TEXT_SHORTINT',true);
  303. u16bit : if doread then
  304. emitcall('FPC_READ_TEXT_WORD',true);
  305. s16bit : if doread then
  306. emitcall('FPC_READ_TEXT_INTEGER',true);
  307. s32bit : if doread then
  308. emitcall('FPC_READ_TEXT_LONGINT',true)
  309. else
  310. emitcall('FPC_WRITE_TEXT_LONGINT',true);
  311. u32bit : if doread then
  312. emitcall('FPC_READ_TEXT_CARDINAL',true)
  313. else
  314. emitcall('FPC_WRITE_TEXT_CARDINAL',true);
  315. uchar : if doread then
  316. emitcall('FPC_READ_TEXT_CHAR',true)
  317. else
  318. emitcall('FPC_WRITE_TEXT_CHAR',true);
  319. bool8bit,
  320. bool16bit,
  321. bool32bit : if doread then
  322. CGMessage(parser_e_illegal_parameter_list)
  323. else
  324. emitcall('FPC_WRITE_TEXT_BOOLEAN',true);
  325. end;
  326. end;
  327. end;
  328. end;
  329. { load ESI in methods again }
  330. popusedregisters(pushed);
  331. maybe_loadesi;
  332. end;
  333. end;
  334. { Insert end of writing for textfiles }
  335. if ft=ft_text then
  336. begin
  337. pushusedregisters(pushed,$ff);
  338. emit_push_mem(aktfile);
  339. if doread then
  340. begin
  341. if doln then
  342. emitcall('FPC_READLN_END',true)
  343. else
  344. emitcall('FPC_READ_END',true);
  345. end
  346. else
  347. begin
  348. if doln then
  349. emitcall('FPC_WRITELN_END',true)
  350. else
  351. emitcall('FPC_WRITE_END',true);
  352. end;
  353. popusedregisters(pushed);
  354. maybe_loadesi;
  355. end;
  356. { Insert IOCheck if set }
  357. if assigned(iolabel) then
  358. begin
  359. { registers are saved in the procedure }
  360. exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,newcsymbol(lab2str(iolabel),0))));
  361. emitcall('FPC_IOCHECK',true);
  362. end;
  363. { Freeup all used temps }
  364. ungetiftemp(aktfile);
  365. if assigned(p^.left) then
  366. begin
  367. p^.left:=reversparameter(p^.left);
  368. if npara<>nb_para then
  369. CGMessage(cg_f_internal_error_in_secondinline);
  370. hp:=p^.left;
  371. while assigned(hp) do
  372. begin
  373. if assigned(hp^.left) then
  374. if (hp^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  375. ungetiftemp(hp^.left^.location.reference);
  376. hp:=hp^.right;
  377. end;
  378. end;
  379. end;
  380. procedure handle_str;
  381. var
  382. hp,node : ptree;
  383. dummycoll : tdefcoll;
  384. is_real,has_length : boolean;
  385. begin
  386. pushusedregisters(pushed,$ff);
  387. node:=p^.left;
  388. is_real:=false;
  389. has_length:=false;
  390. while assigned(node^.right) do node:=node^.right;
  391. { if a real parameter somewhere then call REALSTR }
  392. if (node^.left^.resulttype^.deftype=floatdef) then
  393. is_real:=true;
  394. node:=p^.left;
  395. { we have at least two args }
  396. { with at max 2 colon_para in between }
  397. { first arg longint or float }
  398. hp:=node;
  399. node:=node^.right;
  400. hp^.right:=nil;
  401. dummycoll.data:=hp^.resulttype;
  402. { string arg }
  403. dummycoll.paratyp:=vs_var;
  404. secondcallparan(hp,@dummycoll,false
  405. ,false,0
  406. );
  407. if codegenerror then
  408. exit;
  409. dummycoll.paratyp:=vs_const;
  410. disposetree(p^.left);
  411. p^.left:=nil;
  412. { second arg }
  413. hp:=node;
  414. node:=node^.right;
  415. hp^.right:=nil;
  416. { frac para }
  417. if hp^.is_colon_para and assigned(node) and
  418. node^.is_colon_para then
  419. begin
  420. dummycoll.data:=hp^.resulttype;
  421. secondcallparan(hp,@dummycoll,false
  422. ,false,0
  423. );
  424. if codegenerror then
  425. exit;
  426. disposetree(hp);
  427. hp:=node;
  428. node:=node^.right;
  429. hp^.right:=nil;
  430. has_length:=true;
  431. end
  432. else
  433. if is_real then
  434. push_int(-1);
  435. { third arg, length only if is_real }
  436. if hp^.is_colon_para then
  437. begin
  438. dummycoll.data:=hp^.resulttype;
  439. secondcallparan(hp,@dummycoll,false
  440. ,false,0
  441. );
  442. if codegenerror then
  443. exit;
  444. disposetree(hp);
  445. hp:=node;
  446. node:=node^.right;
  447. hp^.right:=nil;
  448. end
  449. else
  450. if is_real then
  451. push_int(-32767)
  452. else
  453. push_int(-1);
  454. { last arg longint or real }
  455. secondcallparan(hp,@dummycoll,false
  456. ,false,0
  457. );
  458. disposetree(hp);
  459. if codegenerror then
  460. exit;
  461. if is_real then
  462. emitcall('FPC_STR_'+float_name[pfloatdef(hp^.resulttype)^.typ],true)
  463. else if porddef(hp^.resulttype)^.typ=u32bit then
  464. emitcall('FPC_STR_CARDINAL',true)
  465. else
  466. emitcall('FPC_STR_LONGINT',true);
  467. popusedregisters(pushed);
  468. end;
  469. var
  470. r : preference;
  471. hp : ptree;
  472. l : longint;
  473. ispushed : boolean;
  474. hregister : tregister;
  475. otlabel,oflabel : plabel;
  476. oldpushedparasize : longint;
  477. begin
  478. { save & reset pushedparasize }
  479. oldpushedparasize:=pushedparasize;
  480. pushedparasize:=0;
  481. case p^.inlinenumber of
  482. in_assert_x_y:
  483. begin
  484. otlabel:=truelabel;
  485. oflabel:=falselabel;
  486. getlabel(truelabel);
  487. getlabel(falselabel);
  488. secondpass(p^.left^.left);
  489. if cs_do_assertion in aktlocalswitches then
  490. begin
  491. maketojumpbool(p^.left^.left);
  492. emitl(A_LABEL,falselabel);
  493. { erroraddr }
  494. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EBP)));
  495. { lineno }
  496. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,aktfilepos.line)));
  497. { filename string }
  498. hp:=genstringconstnode(current_module^.sourcefiles^.get_file_name(aktfilepos.fileindex));
  499. secondpass(hp);
  500. if codegenerror then
  501. exit;
  502. emitpushreferenceaddr(exprasmlist,hp^.location.reference);
  503. disposetree(hp);
  504. { push msg }
  505. secondpass(p^.left^.right^.left);
  506. emitpushreferenceaddr(exprasmlist,p^.left^.right^.left^.location.reference);
  507. { call }
  508. emitcall('FPC_ASSERT',true);
  509. emitl(A_LABEL,truelabel);
  510. end;
  511. truelabel:=otlabel;
  512. falselabel:=oflabel;
  513. end;
  514. in_lo_word,
  515. in_hi_word :
  516. begin
  517. secondpass(p^.left);
  518. p^.location.loc:=LOC_REGISTER;
  519. if p^.left^.location.loc<>LOC_REGISTER then
  520. begin
  521. if p^.left^.location.loc=LOC_CREGISTER then
  522. begin
  523. p^.location.register:=reg32toreg16(getregister32);
  524. emit_reg_reg(A_MOV,S_W,p^.left^.location.register,
  525. p^.location.register);
  526. end
  527. else
  528. begin
  529. del_reference(p^.left^.location.reference);
  530. p^.location.register:=reg32toreg16(getregister32);
  531. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,newreference(p^.left^.location.reference),
  532. p^.location.register)));
  533. end;
  534. end
  535. else p^.location.register:=p^.left^.location.register;
  536. if p^.inlinenumber=in_hi_word then
  537. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_W,8,p^.location.register)));
  538. p^.location.register:=reg16toreg8(p^.location.register);
  539. end;
  540. in_high_x :
  541. begin
  542. if is_open_array(p^.left^.resulttype) then
  543. begin
  544. secondpass(p^.left);
  545. del_reference(p^.left^.location.reference);
  546. p^.location.register:=getregister32;
  547. new(r);
  548. reset_reference(r^);
  549. r^.base:=highframepointer;
  550. r^.offset:=highoffset+4;
  551. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  552. r,p^.location.register)));
  553. end
  554. end;
  555. in_sizeof_x,
  556. in_typeof_x :
  557. begin
  558. { sizeof(openarray) handling }
  559. if (p^.inlinenumber=in_sizeof_x) and
  560. is_open_array(p^.left^.resulttype) then
  561. begin
  562. { sizeof(openarray)=high(openarray)+1 }
  563. secondpass(p^.left);
  564. del_reference(p^.left^.location.reference);
  565. p^.location.register:=getregister32;
  566. new(r);
  567. reset_reference(r^);
  568. r^.base:=highframepointer;
  569. r^.offset:=highoffset+4;
  570. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  571. r,p^.location.register)));
  572. exprasmlist^.concat(new(pai386,op_reg(A_INC,S_L,
  573. p^.location.register)));
  574. if parraydef(p^.left^.resulttype)^.elesize<>1 then
  575. exprasmlist^.concat(new(pai386,op_const_reg(A_IMUL,S_L,
  576. parraydef(p^.left^.resulttype)^.elesize,p^.location.register)));
  577. end
  578. else
  579. begin
  580. { for both cases load vmt }
  581. if p^.left^.treetype=typen then
  582. begin
  583. p^.location.register:=getregister32;
  584. exprasmlist^.concat(new(pai386,op_csymbol_reg(A_MOV,
  585. S_L,newcsymbol(pobjectdef(p^.left^.resulttype)^.vmt_mangledname,0),
  586. p^.location.register)));
  587. end
  588. else
  589. begin
  590. secondpass(p^.left);
  591. del_reference(p^.left^.location.reference);
  592. p^.location.loc:=LOC_REGISTER;
  593. p^.location.register:=getregister32;
  594. { load VMT pointer }
  595. inc(p^.left^.location.reference.offset,
  596. pobjectdef(p^.left^.resulttype)^.vmt_offset);
  597. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  598. newreference(p^.left^.location.reference),
  599. p^.location.register)));
  600. end;
  601. { in sizeof load size }
  602. if p^.inlinenumber=in_sizeof_x then
  603. begin
  604. new(r);
  605. reset_reference(r^);
  606. r^.base:=p^.location.register;
  607. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,
  608. p^.location.register)));
  609. end;
  610. end;
  611. end;
  612. in_lo_long,
  613. in_hi_long :
  614. begin
  615. secondpass(p^.left);
  616. p^.location.loc:=LOC_REGISTER;
  617. if p^.left^.location.loc<>LOC_REGISTER then
  618. begin
  619. if p^.left^.location.loc=LOC_CREGISTER then
  620. begin
  621. p^.location.register:=getregister32;
  622. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,
  623. p^.location.register);
  624. end
  625. else
  626. begin
  627. del_reference(p^.left^.location.reference);
  628. p^.location.register:=getregister32;
  629. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  630. p^.location.register)));
  631. end;
  632. end
  633. else p^.location.register:=p^.left^.location.register;
  634. if p^.inlinenumber=in_hi_long then
  635. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_L,16,p^.location.register)));
  636. p^.location.register:=reg32toreg16(p^.location.register);
  637. end;
  638. in_length_string :
  639. begin
  640. secondpass(p^.left);
  641. set_location(p^.location,p^.left^.location);
  642. { length in ansi strings is at offset -8 }
  643. {$ifdef UseAnsiString}
  644. if is_ansistring(p^.left^.resulttype) then
  645. dec(p^.location.reference.offset,8);
  646. {$endif UseAnsiString}
  647. end;
  648. in_pred_x,
  649. in_succ_x:
  650. begin
  651. secondpass(p^.left);
  652. if p^.inlinenumber=in_pred_x then
  653. asmop:=A_DEC
  654. else
  655. asmop:=A_INC;
  656. case p^.resulttype^.size of
  657. 4 : opsize:=S_L;
  658. 2 : opsize:=S_W;
  659. 1 : opsize:=S_B;
  660. else
  661. internalerror(10080);
  662. end;
  663. p^.location.loc:=LOC_REGISTER;
  664. if p^.left^.location.loc<>LOC_REGISTER then
  665. begin
  666. p^.location.register:=getregister32;
  667. if (p^.resulttype^.size=2) then
  668. p^.location.register:=reg32toreg16(p^.location.register);
  669. if (p^.resulttype^.size=1) then
  670. p^.location.register:=reg32toreg8(p^.location.register);
  671. if p^.left^.location.loc=LOC_CREGISTER then
  672. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,
  673. p^.location.register)
  674. else
  675. if p^.left^.location.loc=LOC_FLAGS then
  676. exprasmlist^.concat(new(pai386,op_reg(flag_2_set[p^.left^.location.resflags],S_B,
  677. p^.location.register)))
  678. else
  679. begin
  680. del_reference(p^.left^.location.reference);
  681. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(p^.left^.location.reference),
  682. p^.location.register)));
  683. end;
  684. end
  685. else p^.location.register:=p^.left^.location.register;
  686. exprasmlist^.concat(new(pai386,op_reg(asmop,opsize,
  687. p^.location.register)));
  688. emitoverflowcheck(p);
  689. emitrangecheck(p);
  690. end;
  691. in_dec_x,
  692. in_inc_x :
  693. begin
  694. { set defaults }
  695. addvalue:=1;
  696. addconstant:=true;
  697. { load first parameter, must be a reference }
  698. secondpass(p^.left^.left);
  699. case p^.left^.left^.resulttype^.deftype of
  700. orddef,
  701. enumdef : begin
  702. case p^.left^.left^.resulttype^.size of
  703. 1 : opsize:=S_B;
  704. 2 : opsize:=S_W;
  705. 4 : opsize:=S_L;
  706. end;
  707. end;
  708. pointerdef : begin
  709. opsize:=S_L;
  710. if porddef(ppointerdef(p^.left^.left^.resulttype)^.definition)=voiddef then
  711. addvalue:=1
  712. else
  713. addvalue:=ppointerdef(p^.left^.left^.resulttype)^.definition^.savesize;
  714. end;
  715. else
  716. internalerror(10081);
  717. end;
  718. { second argument specified?, must be a s32bit in register }
  719. if assigned(p^.left^.right) then
  720. begin
  721. secondpass(p^.left^.right^.left);
  722. { when constant, just multiply the addvalue }
  723. if is_constintnode(p^.left^.right^.left) then
  724. addvalue:=addvalue*get_ordinal_value(p^.left^.right^.left)
  725. else
  726. begin
  727. case p^.left^.right^.left^.location.loc of
  728. LOC_REGISTER,
  729. LOC_CREGISTER : hregister:=p^.left^.right^.left^.location.register;
  730. LOC_MEM,
  731. LOC_REFERENCE : begin
  732. del_reference(p^.left^.right^.left^.location.reference);
  733. hregister:=getregister32;
  734. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  735. newreference(p^.left^.right^.left^.location.reference),hregister)));
  736. end;
  737. else
  738. internalerror(10082);
  739. end;
  740. { insert multiply with addvalue if its >1 }
  741. if addvalue>1 then
  742. exprasmlist^.concat(new(pai386,op_const_reg(A_IMUL,opsize,
  743. addvalue,hregister)));
  744. addconstant:=false;
  745. end;
  746. end;
  747. { write the add instruction }
  748. if addconstant then
  749. begin
  750. if (addvalue=1) and not(cs_check_overflow in aktlocalswitches) then
  751. begin
  752. if p^.left^.left^.location.loc=LOC_CREGISTER then
  753. exprasmlist^.concat(new(pai386,op_reg(incdecop[p^.inlinenumber],opsize,
  754. p^.left^.left^.location.register)))
  755. else
  756. exprasmlist^.concat(new(pai386,op_ref(incdecop[p^.inlinenumber],opsize,
  757. newreference(p^.left^.left^.location.reference))))
  758. end
  759. else
  760. begin
  761. if p^.left^.left^.location.loc=LOC_CREGISTER then
  762. exprasmlist^.concat(new(pai386,op_const_reg(addsubop[p^.inlinenumber],opsize,
  763. addvalue,p^.left^.left^.location.register)))
  764. else
  765. exprasmlist^.concat(new(pai386,op_const_ref(addsubop[p^.inlinenumber],opsize,
  766. addvalue,newreference(p^.left^.left^.location.reference))));
  767. end
  768. end
  769. else
  770. begin
  771. if p^.left^.left^.location.loc=LOC_CREGISTER then
  772. exprasmlist^.concat(new(pai386,op_reg_reg(addsubop[p^.inlinenumber],opsize,
  773. hregister,p^.left^.left^.location.register)))
  774. else
  775. exprasmlist^.concat(new(pai386,op_reg_ref(addsubop[p^.inlinenumber],opsize,
  776. hregister,newreference(p^.left^.left^.location.reference))));
  777. ungetregister32(hregister);
  778. end;
  779. emitoverflowcheck(p^.left^.left);
  780. emitrangecheck(p^.left^.left);
  781. end;
  782. in_assigned_x :
  783. begin
  784. secondpass(p^.left^.left);
  785. p^.location.loc:=LOC_FLAGS;
  786. if (p^.left^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  787. begin
  788. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,
  789. p^.left^.left^.location.register,
  790. p^.left^.left^.location.register)));
  791. ungetregister32(p^.left^.left^.location.register);
  792. end
  793. else
  794. begin
  795. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_L,0,
  796. newreference(p^.left^.left^.location.reference))));
  797. del_reference(p^.left^.left^.location.reference);
  798. end;
  799. p^.location.resflags:=F_NE;
  800. end;
  801. in_reset_typedfile,in_rewrite_typedfile :
  802. begin
  803. pushusedregisters(pushed,$ff);
  804. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,pfiledef(p^.left^.resulttype)^.typed_as^.size)));
  805. secondload(p^.left);
  806. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  807. if p^.inlinenumber=in_reset_typedfile then
  808. emitcall('FPC_RESET_TYPED',true)
  809. else
  810. emitcall('FPC_REWRITE_TYPED',true);
  811. popusedregisters(pushed);
  812. end;
  813. in_write_x :
  814. handlereadwrite(false,false);
  815. in_writeln_x :
  816. handlereadwrite(false,true);
  817. in_read_x :
  818. handlereadwrite(true,false);
  819. in_readln_x :
  820. handlereadwrite(true,true);
  821. in_str_x_string :
  822. begin
  823. handle_str;
  824. maybe_loadesi;
  825. end;
  826. in_include_x_y,
  827. in_exclude_x_y:
  828. begin
  829. secondpass(p^.left^.left);
  830. if p^.left^.right^.left^.treetype=ordconstn then
  831. begin
  832. { calculate bit position }
  833. l:=1 shl (p^.left^.right^.left^.value mod 32);
  834. { determine operator }
  835. if p^.inlinenumber=in_include_x_y then
  836. asmop:=A_OR
  837. else
  838. begin
  839. asmop:=A_AND;
  840. l:=not(l);
  841. end;
  842. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  843. begin
  844. inc(p^.left^.left^.location.reference.offset,(p^.left^.right^.left^.value div 32)*4);
  845. exprasmlist^.concat(new(pai386,op_const_ref(asmop,S_L,
  846. l,newreference(p^.left^.left^.location.reference))));
  847. del_reference(p^.left^.left^.location.reference);
  848. end
  849. else
  850. { LOC_CREGISTER }
  851. exprasmlist^.concat(new(pai386,op_const_reg(asmop,S_L,
  852. l,p^.left^.left^.location.register)));
  853. end
  854. else
  855. begin
  856. { generate code for the element to set }
  857. ispushed:=maybe_push(p^.left^.right^.left^.registers32,p^.left^.left);
  858. secondpass(p^.left^.right^.left);
  859. if ispushed then
  860. restore(p^.left^.left);
  861. { determine asm operator }
  862. if p^.inlinenumber=in_include_x_y then
  863. asmop:=A_BTS
  864. else
  865. asmop:=A_BTR;
  866. if psetdef(p^.left^.resulttype)^.settype=smallset then
  867. begin
  868. if p^.left^.right^.left^.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  869. hregister:=p^.left^.right^.left^.location.register
  870. else
  871. begin
  872. hregister:=R_EDI;
  873. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  874. newreference(p^.left^.right^.left^.location.reference),R_EDI)));
  875. end;
  876. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  877. exprasmlist^.concat(new(pai386,op_reg_ref(asmop,S_L,R_EDI,
  878. newreference(p^.left^.right^.left^.location.reference))))
  879. else
  880. exprasmlist^.concat(new(pai386,op_reg_reg(asmop,S_L,R_EDI,
  881. p^.left^.right^.left^.location.register)));
  882. end
  883. else
  884. begin
  885. end;
  886. end;
  887. end;
  888. else internalerror(9);
  889. end;
  890. { reset pushedparasize }
  891. pushedparasize:=oldpushedparasize;
  892. end;
  893. end.
  894. {
  895. $Log$
  896. Revision 1.12 1998-10-08 17:17:12 pierre
  897. * current_module old scanner tagged as invalid if unit is recompiled
  898. + added ppheap for better info on tracegetmem of heaptrc
  899. (adds line column and file index)
  900. * several memory leaks removed ith help of heaptrc !!
  901. Revision 1.11 1998/10/05 21:33:15 peter
  902. * fixed 161,165,166,167,168
  903. Revision 1.10 1998/10/05 12:32:44 peter
  904. + assert() support
  905. Revision 1.8 1998/10/02 10:35:09 peter
  906. * support for inc(pointer,value) which now increases with value instead
  907. of 0*value :)
  908. Revision 1.7 1998/09/21 08:45:07 pierre
  909. + added vmt_offset in tobjectdef.write for fututre use
  910. (first steps to have objects without vmt if no virtual !!)
  911. + added fpu_used field for tabstractprocdef :
  912. sets this level to 2 if the functions return with value in FPU
  913. (is then set to correct value at parsing of implementation)
  914. THIS MIGHT refuse some code with FPU expression too complex
  915. that were accepted before and even in some cases
  916. that don't overflow in fact
  917. ( like if f : float; is a forward that finally in implementation
  918. only uses one fpu register !!)
  919. Nevertheless I think that it will improve security on
  920. FPU operations !!
  921. * most other changes only for UseBrowser code
  922. (added symtable references for record and objects)
  923. local switch for refs to args and local of each function
  924. (static symtable still missing)
  925. UseBrowser still not stable and probably broken by
  926. the definition hash array !!
  927. Revision 1.6 1998/09/20 12:26:37 peter
  928. * merged fixes
  929. Revision 1.5 1998/09/17 09:42:15 peter
  930. + pass_2 for cg386
  931. * Message() -> CGMessage() for pass_1/pass_2
  932. Revision 1.4 1998/09/14 10:43:49 peter
  933. * all internal RTL functions start with FPC_
  934. Revision 1.3.2.1 1998/09/20 12:20:07 peter
  935. * Fixed stack not on 4 byte boundary when doing a call
  936. Revision 1.3 1998/09/05 23:03:57 florian
  937. * some fixes to get -Or work:
  938. - inc/dec didn't take care of CREGISTER
  939. - register calculcation of inc/dec was wrong
  940. - var/const parameters get now assigned 32 bit register, but
  941. const parameters only if they are passed by reference !
  942. Revision 1.2 1998/09/04 08:41:40 peter
  943. * updated some error CGMessages
  944. Revision 1.1 1998/08/31 12:22:14 peter
  945. * secondinline moved to cg386inl
  946. Revision 1.19 1998/08/31 08:52:03 peter
  947. * fixed error 10 with succ() and pref()
  948. Revision 1.18 1998/08/20 21:36:38 peter
  949. * fixed 'with object do' bug
  950. Revision 1.17 1998/08/19 16:07:36 jonas
  951. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  952. Revision 1.16 1998/08/18 09:24:36 pierre
  953. * small warning position bug fixed
  954. * support_mmx switches splitting was missing
  955. * rhide error and warning output corrected
  956. Revision 1.15 1998/08/13 11:00:09 peter
  957. * fixed procedure<>procedure construct
  958. Revision 1.14 1998/08/11 14:05:33 peter
  959. * fixed sizeof(array of char)
  960. Revision 1.13 1998/08/10 14:49:45 peter
  961. + localswitches, moduleswitches, globalswitches splitting
  962. Revision 1.12 1998/07/30 13:30:31 florian
  963. * final implemenation of exception support, maybe it needs
  964. some fixes :)
  965. Revision 1.11 1998/07/24 22:16:52 florian
  966. * internal error 10 together with array access fixed. I hope
  967. that's the final fix.
  968. Revision 1.10 1998/07/18 22:54:23 florian
  969. * some ansi/wide/longstring support fixed:
  970. o parameter passing
  971. o returning as result from functions
  972. Revision 1.9 1998/07/07 17:40:37 peter
  973. * packrecords 4 works
  974. * word aligning of parameters
  975. Revision 1.8 1998/07/06 15:51:15 michael
  976. Added length checking for string reading
  977. Revision 1.7 1998/07/06 14:19:51 michael
  978. + Added calls for reading/writing ansistrings
  979. Revision 1.6 1998/07/01 15:28:48 peter
  980. + better writeln/readln handling, now 100% like tp7
  981. Revision 1.5 1998/06/25 14:04:17 peter
  982. + internal inc/dec
  983. Revision 1.4 1998/06/25 08:48:06 florian
  984. * first version of rtti support
  985. Revision 1.3 1998/06/09 16:01:33 pierre
  986. + added procedure directive parsing for procvars
  987. (accepted are popstack cdecl and pascal)
  988. + added C vars with the following syntax
  989. var C calias 'true_c_name';(can be followed by external)
  990. reason is that you must add the Cprefix
  991. which is target dependent
  992. Revision 1.2 1998/06/08 13:13:29 pierre
  993. + temporary variables now in temp_gen.pas unit
  994. because it is processor independent
  995. * mppc68k.bat modified to undefine i386 and support_mmx
  996. (which are defaults for i386)
  997. Revision 1.1 1998/06/05 17:44:10 peter
  998. * splitted cgi386
  999. }