cg386inl.pas 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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. begin
  401. pushusedregisters(pushed,$ff);
  402. node:=p^.left;
  403. is_real:=false;
  404. has_length:=false;
  405. while assigned(node^.right) do node:=node^.right;
  406. { if a real parameter somewhere then call REALSTR }
  407. if (node^.left^.resulttype^.deftype=floatdef) then
  408. is_real:=true;
  409. node:=p^.left;
  410. { we have at least two args }
  411. { with at max 2 colon_para in between }
  412. { string arg }
  413. hp:=node;
  414. node:=node^.right;
  415. hp^.right:=nil;
  416. dummycoll.paratyp:=vs_var;
  417. if is_shortstring(hp^.resulttype) then
  418. dummycoll.data:=openshortstringdef
  419. else
  420. dummycoll.data:=hp^.resulttype;
  421. secondcallparan(hp,@dummycoll,false,false,0);
  422. if codegenerror then
  423. exit;
  424. dummycoll.paratyp:=vs_const;
  425. disposetree(p^.left);
  426. p^.left:=nil;
  427. { second arg }
  428. hp:=node;
  429. node:=node^.right;
  430. hp^.right:=nil;
  431. { frac para }
  432. if hp^.is_colon_para and assigned(node) and
  433. node^.is_colon_para then
  434. begin
  435. dummycoll.data:=hp^.resulttype;
  436. secondcallparan(hp,@dummycoll,false
  437. ,false,0
  438. );
  439. if codegenerror then
  440. exit;
  441. disposetree(hp);
  442. hp:=node;
  443. node:=node^.right;
  444. hp^.right:=nil;
  445. has_length:=true;
  446. end
  447. else
  448. if is_real then
  449. push_int(-1);
  450. { third arg, length only if is_real }
  451. if hp^.is_colon_para then
  452. begin
  453. dummycoll.data:=hp^.resulttype;
  454. secondcallparan(hp,@dummycoll,false
  455. ,false,0
  456. );
  457. if codegenerror then
  458. exit;
  459. disposetree(hp);
  460. hp:=node;
  461. node:=node^.right;
  462. hp^.right:=nil;
  463. end
  464. else
  465. if is_real then
  466. push_int(-32767)
  467. else
  468. push_int(-1);
  469. { last arg longint or real }
  470. secondcallparan(hp,@dummycoll,false
  471. ,false,0
  472. );
  473. disposetree(hp);
  474. if codegenerror then
  475. exit;
  476. if is_real then
  477. emitcall('FPC_STR_'+float_name[pfloatdef(hp^.resulttype)^.typ],true)
  478. else
  479. case porddef(hp^.resulttype)^.typ of
  480. u32bit:
  481. emitcall('FPC_STR_CARDINAL',true);
  482. u64bit:
  483. emitcall('FPC_STR_QWORD',true);
  484. s64bitint:
  485. emitcall('FPC_STR_INT64',true);
  486. else
  487. emitcall('FPC_STR_LONGINT',true);
  488. end;
  489. popusedregisters(pushed);
  490. end;
  491. var
  492. r : preference;
  493. hp : ptree;
  494. l : longint;
  495. ispushed : boolean;
  496. hregister : tregister;
  497. otlabel,oflabel : plabel;
  498. oldpushedparasize : longint;
  499. begin
  500. { save & reset pushedparasize }
  501. oldpushedparasize:=pushedparasize;
  502. pushedparasize:=0;
  503. case p^.inlinenumber of
  504. in_assert_x_y:
  505. begin
  506. otlabel:=truelabel;
  507. oflabel:=falselabel;
  508. getlabel(truelabel);
  509. getlabel(falselabel);
  510. secondpass(p^.left^.left);
  511. if cs_do_assertion in aktlocalswitches then
  512. begin
  513. maketojumpbool(p^.left^.left);
  514. emitl(A_LABEL,falselabel);
  515. { erroraddr }
  516. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EBP)));
  517. { lineno }
  518. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,aktfilepos.line)));
  519. { filename string }
  520. hp:=genstringconstnode(current_module^.sourcefiles^.get_file_name(aktfilepos.fileindex));
  521. secondpass(hp);
  522. if codegenerror then
  523. exit;
  524. emitpushreferenceaddr(exprasmlist,hp^.location.reference);
  525. disposetree(hp);
  526. { push msg }
  527. secondpass(p^.left^.right^.left);
  528. emitpushreferenceaddr(exprasmlist,p^.left^.right^.left^.location.reference);
  529. { call }
  530. emitcall('FPC_ASSERT',true);
  531. emitl(A_LABEL,truelabel);
  532. end;
  533. freelabel(truelabel);
  534. freelabel(falselabel);
  535. truelabel:=otlabel;
  536. falselabel:=oflabel;
  537. end;
  538. in_lo_word,
  539. in_hi_word :
  540. begin
  541. secondpass(p^.left);
  542. p^.location.loc:=LOC_REGISTER;
  543. if p^.left^.location.loc<>LOC_REGISTER then
  544. begin
  545. if p^.left^.location.loc=LOC_CREGISTER then
  546. begin
  547. p^.location.register:=reg32toreg16(getregister32);
  548. emit_reg_reg(A_MOV,S_W,p^.left^.location.register,
  549. p^.location.register);
  550. end
  551. else
  552. begin
  553. del_reference(p^.left^.location.reference);
  554. p^.location.register:=reg32toreg16(getregister32);
  555. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,newreference(p^.left^.location.reference),
  556. p^.location.register)));
  557. end;
  558. end
  559. else p^.location.register:=p^.left^.location.register;
  560. if p^.inlinenumber=in_hi_word then
  561. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_W,8,p^.location.register)));
  562. p^.location.register:=reg16toreg8(p^.location.register);
  563. end;
  564. in_high_x :
  565. begin
  566. if is_open_array(p^.left^.resulttype) or
  567. is_open_string(p^.left^.resulttype) then
  568. begin
  569. secondpass(p^.left);
  570. del_reference(p^.left^.location.reference);
  571. p^.location.register:=getregister32;
  572. r:=new_reference(highframepointer,highoffset+4);
  573. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  574. r,p^.location.register)));
  575. end
  576. end;
  577. in_sizeof_x,
  578. in_typeof_x :
  579. begin
  580. { sizeof(openarray) handling }
  581. if (p^.inlinenumber=in_sizeof_x) and
  582. (is_open_array(p^.left^.resulttype) or
  583. is_open_string(p^.left^.resulttype)) then
  584. begin
  585. { sizeof(openarray)=high(openarray)+1 }
  586. secondpass(p^.left);
  587. del_reference(p^.left^.location.reference);
  588. p^.location.register:=getregister32;
  589. r:=new_reference(highframepointer,highoffset+4);
  590. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  591. r,p^.location.register)));
  592. exprasmlist^.concat(new(pai386,op_reg(A_INC,S_L,
  593. p^.location.register)));
  594. if (p^.left^.resulttype^.deftype=arraydef) and
  595. (parraydef(p^.left^.resulttype)^.elesize<>1) then
  596. exprasmlist^.concat(new(pai386,op_const_reg(A_IMUL,S_L,
  597. parraydef(p^.left^.resulttype)^.elesize,p^.location.register)));
  598. end
  599. else
  600. begin
  601. { for both cases load vmt }
  602. if p^.left^.treetype=typen then
  603. begin
  604. p^.location.register:=getregister32;
  605. exprasmlist^.concat(new(pai386,op_csymbol_reg(A_MOV,
  606. S_L,newcsymbol(pobjectdef(p^.left^.resulttype)^.vmt_mangledname,0),
  607. p^.location.register)));
  608. end
  609. else
  610. begin
  611. secondpass(p^.left);
  612. del_reference(p^.left^.location.reference);
  613. p^.location.loc:=LOC_REGISTER;
  614. p^.location.register:=getregister32;
  615. { load VMT pointer }
  616. inc(p^.left^.location.reference.offset,
  617. pobjectdef(p^.left^.resulttype)^.vmt_offset);
  618. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  619. newreference(p^.left^.location.reference),
  620. p^.location.register)));
  621. end;
  622. { in sizeof load size }
  623. if p^.inlinenumber=in_sizeof_x then
  624. begin
  625. new(r);
  626. reset_reference(r^);
  627. r^.base:=p^.location.register;
  628. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,
  629. p^.location.register)));
  630. end;
  631. end;
  632. end;
  633. in_lo_long,
  634. in_hi_long :
  635. begin
  636. secondpass(p^.left);
  637. p^.location.loc:=LOC_REGISTER;
  638. if p^.left^.location.loc<>LOC_REGISTER then
  639. begin
  640. if p^.left^.location.loc=LOC_CREGISTER then
  641. begin
  642. p^.location.register:=getregister32;
  643. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,
  644. p^.location.register);
  645. end
  646. else
  647. begin
  648. del_reference(p^.left^.location.reference);
  649. p^.location.register:=getregister32;
  650. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  651. p^.location.register)));
  652. end;
  653. end
  654. else p^.location.register:=p^.left^.location.register;
  655. if p^.inlinenumber=in_hi_long then
  656. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_L,16,p^.location.register)));
  657. p^.location.register:=reg32toreg16(p^.location.register);
  658. end;
  659. in_length_string :
  660. begin
  661. secondpass(p^.left);
  662. set_location(p^.location,p^.left^.location);
  663. { length in ansi strings is at offset -8 }
  664. if is_ansistring(p^.left^.resulttype) then
  665. dec(p^.location.reference.offset,8)
  666. { char is always 1, so make it a constant value }
  667. else if is_char(p^.left^.resulttype) then
  668. begin
  669. clear_location(p^.location);
  670. p^.location.loc:=LOC_MEM;
  671. p^.location.reference.isintvalue:=true;
  672. p^.location.reference.offset:=1;
  673. end;
  674. end;
  675. in_pred_x,
  676. in_succ_x:
  677. begin
  678. secondpass(p^.left);
  679. if p^.inlinenumber=in_pred_x then
  680. asmop:=A_DEC
  681. else
  682. asmop:=A_INC;
  683. case p^.resulttype^.size of
  684. 4 : opsize:=S_L;
  685. 2 : opsize:=S_W;
  686. 1 : opsize:=S_B;
  687. else
  688. internalerror(10080);
  689. end;
  690. p^.location.loc:=LOC_REGISTER;
  691. if p^.left^.location.loc<>LOC_REGISTER then
  692. begin
  693. p^.location.register:=getregister32;
  694. if (p^.resulttype^.size=2) then
  695. p^.location.register:=reg32toreg16(p^.location.register);
  696. if (p^.resulttype^.size=1) then
  697. p^.location.register:=reg32toreg8(p^.location.register);
  698. if p^.left^.location.loc=LOC_CREGISTER then
  699. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,
  700. p^.location.register)
  701. else
  702. if p^.left^.location.loc=LOC_FLAGS then
  703. exprasmlist^.concat(new(pai386,op_reg(flag_2_set[p^.left^.location.resflags],S_B,
  704. p^.location.register)))
  705. else
  706. begin
  707. del_reference(p^.left^.location.reference);
  708. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(p^.left^.location.reference),
  709. p^.location.register)));
  710. end;
  711. end
  712. else p^.location.register:=p^.left^.location.register;
  713. exprasmlist^.concat(new(pai386,op_reg(asmop,opsize,
  714. p^.location.register)));
  715. emitoverflowcheck(p);
  716. emitrangecheck(p,p^.resulttype);
  717. end;
  718. in_dec_x,
  719. in_inc_x :
  720. begin
  721. { set defaults }
  722. addvalue:=1;
  723. addconstant:=true;
  724. { load first parameter, must be a reference }
  725. secondpass(p^.left^.left);
  726. case p^.left^.left^.resulttype^.deftype of
  727. orddef,
  728. enumdef : begin
  729. case p^.left^.left^.resulttype^.size of
  730. 1 : opsize:=S_B;
  731. 2 : opsize:=S_W;
  732. 4 : opsize:=S_L;
  733. end;
  734. end;
  735. pointerdef : begin
  736. opsize:=S_L;
  737. if porddef(ppointerdef(p^.left^.left^.resulttype)^.definition)=voiddef then
  738. addvalue:=1
  739. else
  740. addvalue:=ppointerdef(p^.left^.left^.resulttype)^.definition^.savesize;
  741. end;
  742. else
  743. internalerror(10081);
  744. end;
  745. { second argument specified?, must be a s32bit in register }
  746. if assigned(p^.left^.right) then
  747. begin
  748. secondpass(p^.left^.right^.left);
  749. { when constant, just multiply the addvalue }
  750. if is_constintnode(p^.left^.right^.left) then
  751. addvalue:=addvalue*get_ordinal_value(p^.left^.right^.left)
  752. else
  753. begin
  754. case p^.left^.right^.left^.location.loc of
  755. LOC_REGISTER,
  756. LOC_CREGISTER : hregister:=p^.left^.right^.left^.location.register;
  757. LOC_MEM,
  758. LOC_REFERENCE : begin
  759. del_reference(p^.left^.right^.left^.location.reference);
  760. hregister:=getregister32;
  761. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  762. newreference(p^.left^.right^.left^.location.reference),hregister)));
  763. end;
  764. else
  765. internalerror(10082);
  766. end;
  767. { insert multiply with addvalue if its >1 }
  768. if addvalue>1 then
  769. exprasmlist^.concat(new(pai386,op_const_reg(A_IMUL,opsize,
  770. addvalue,hregister)));
  771. addconstant:=false;
  772. end;
  773. end;
  774. { write the add instruction }
  775. if addconstant then
  776. begin
  777. if (addvalue=1) and not(cs_check_overflow in aktlocalswitches) then
  778. begin
  779. if p^.left^.left^.location.loc=LOC_CREGISTER then
  780. exprasmlist^.concat(new(pai386,op_reg(incdecop[p^.inlinenumber],opsize,
  781. p^.left^.left^.location.register)))
  782. else
  783. exprasmlist^.concat(new(pai386,op_ref(incdecop[p^.inlinenumber],opsize,
  784. newreference(p^.left^.left^.location.reference))))
  785. end
  786. else
  787. begin
  788. if p^.left^.left^.location.loc=LOC_CREGISTER then
  789. exprasmlist^.concat(new(pai386,op_const_reg(addsubop[p^.inlinenumber],opsize,
  790. addvalue,p^.left^.left^.location.register)))
  791. else
  792. exprasmlist^.concat(new(pai386,op_const_ref(addsubop[p^.inlinenumber],opsize,
  793. addvalue,newreference(p^.left^.left^.location.reference))));
  794. end
  795. end
  796. else
  797. begin
  798. { BUG HERE : detected with nasm :
  799. hregister is allways 32 bit
  800. it should be converted to 16 or 8 bit depending on op_size PM }
  801. { still not perfect :
  802. if hregister is already a 16 bit reg ?? PM }
  803. case opsize of
  804. S_B : hregister:=reg32toreg8(hregister);
  805. S_W : hregister:=reg32toreg16(hregister);
  806. end;
  807. if p^.left^.left^.location.loc=LOC_CREGISTER then
  808. exprasmlist^.concat(new(pai386,op_reg_reg(addsubop[p^.inlinenumber],opsize,
  809. hregister,p^.left^.left^.location.register)))
  810. else
  811. exprasmlist^.concat(new(pai386,op_reg_ref(addsubop[p^.inlinenumber],opsize,
  812. hregister,newreference(p^.left^.left^.location.reference))));
  813. case opsize of
  814. S_B : hregister:=reg8toreg32(hregister);
  815. S_W : hregister:=reg16toreg32(hregister);
  816. end;
  817. ungetregister32(hregister);
  818. end;
  819. emitoverflowcheck(p^.left^.left);
  820. emitrangecheck(p^.left^.left,p^.left^.left^.resulttype);
  821. end;
  822. in_assigned_x :
  823. begin
  824. secondpass(p^.left^.left);
  825. p^.location.loc:=LOC_FLAGS;
  826. if (p^.left^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  827. begin
  828. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,
  829. p^.left^.left^.location.register,
  830. p^.left^.left^.location.register)));
  831. ungetregister32(p^.left^.left^.location.register);
  832. end
  833. else
  834. begin
  835. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_L,0,
  836. newreference(p^.left^.left^.location.reference))));
  837. del_reference(p^.left^.left^.location.reference);
  838. end;
  839. p^.location.resflags:=F_NE;
  840. end;
  841. in_reset_typedfile,in_rewrite_typedfile :
  842. begin
  843. pushusedregisters(pushed,$ff);
  844. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,pfiledef(p^.left^.resulttype)^.typed_as^.size)));
  845. secondload(p^.left);
  846. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  847. if p^.inlinenumber=in_reset_typedfile then
  848. emitcall('FPC_RESET_TYPED',true)
  849. else
  850. emitcall('FPC_REWRITE_TYPED',true);
  851. popusedregisters(pushed);
  852. end;
  853. in_write_x :
  854. handlereadwrite(false,false);
  855. in_writeln_x :
  856. handlereadwrite(false,true);
  857. in_read_x :
  858. handlereadwrite(true,false);
  859. in_readln_x :
  860. handlereadwrite(true,true);
  861. in_str_x_string :
  862. begin
  863. handle_str;
  864. maybe_loadesi;
  865. end;
  866. in_include_x_y,
  867. in_exclude_x_y:
  868. begin
  869. secondpass(p^.left^.left);
  870. if p^.left^.right^.left^.treetype=ordconstn then
  871. begin
  872. { calculate bit position }
  873. l:=1 shl (p^.left^.right^.left^.value mod 32);
  874. { determine operator }
  875. if p^.inlinenumber=in_include_x_y then
  876. asmop:=A_OR
  877. else
  878. begin
  879. asmop:=A_AND;
  880. l:=not(l);
  881. end;
  882. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  883. begin
  884. inc(p^.left^.left^.location.reference.offset,(p^.left^.right^.left^.value div 32)*4);
  885. exprasmlist^.concat(new(pai386,op_const_ref(asmop,S_L,
  886. l,newreference(p^.left^.left^.location.reference))));
  887. del_reference(p^.left^.left^.location.reference);
  888. end
  889. else
  890. { LOC_CREGISTER }
  891. exprasmlist^.concat(new(pai386,op_const_reg(asmop,S_L,
  892. l,p^.left^.left^.location.register)));
  893. end
  894. else
  895. begin
  896. { generate code for the element to set }
  897. ispushed:=maybe_push(p^.left^.right^.left^.registers32,p^.left^.left);
  898. secondpass(p^.left^.right^.left);
  899. if ispushed then
  900. restore(p^.left^.left);
  901. { determine asm operator }
  902. if p^.inlinenumber=in_include_x_y then
  903. asmop:=A_BTS
  904. else
  905. asmop:=A_BTR;
  906. if psetdef(p^.left^.resulttype)^.settype=smallset then
  907. begin
  908. if p^.left^.right^.left^.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  909. hregister:=p^.left^.right^.left^.location.register
  910. else
  911. begin
  912. hregister:=R_EDI;
  913. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  914. newreference(p^.left^.right^.left^.location.reference),R_EDI)));
  915. end;
  916. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  917. exprasmlist^.concat(new(pai386,op_reg_ref(asmop,S_L,hregister,
  918. newreference(p^.left^.right^.left^.location.reference))))
  919. else
  920. exprasmlist^.concat(new(pai386,op_reg_reg(asmop,S_L,hregister,
  921. p^.left^.right^.left^.location.register)));
  922. end
  923. else
  924. begin
  925. pushsetelement(p^.left^.right^.left);
  926. { normset is allways a ref }
  927. emitpushreferenceaddr(exprasmlist,
  928. p^.left^.left^.location.reference);
  929. if p^.inlinenumber=in_include_x_y then
  930. emitcall('FPC_SET_SET_BYTE',true)
  931. else
  932. emitcall('FPC_SET_UNSET_BYTE',true);
  933. {CGMessage(cg_e_include_not_implemented);}
  934. end;
  935. end;
  936. end;
  937. else internalerror(9);
  938. end;
  939. { reset pushedparasize }
  940. pushedparasize:=oldpushedparasize;
  941. end;
  942. end.
  943. {
  944. $Log$
  945. Revision 1.22 1998-12-11 23:36:07 florian
  946. + again more stuff for int64/qword:
  947. - comparision operators
  948. - code generation for: str, read(ln), write(ln)
  949. Revision 1.21 1998/12/11 00:02:50 peter
  950. + globtype,tokens,version unit splitted from globals
  951. Revision 1.20 1998/11/27 14:50:32 peter
  952. + open strings, $P switch support
  953. Revision 1.19 1998/11/26 13:10:40 peter
  954. * new int - int conversion -dNEWCNV
  955. * some function renamings
  956. Revision 1.18 1998/11/24 17:04:27 peter
  957. * fixed length(char) when char is a variable
  958. Revision 1.17 1998/11/05 12:02:33 peter
  959. * released useansistring
  960. * removed -Sv, its now available in fpc modes
  961. Revision 1.16 1998/10/22 17:11:13 pierre
  962. + terminated the include exclude implementation for i386
  963. * enums inside records fixed
  964. Revision 1.15 1998/10/21 15:12:50 pierre
  965. * bug fix for IOCHECK inside a procedure with iocheck modifier
  966. * removed the GPF for unexistant overloading
  967. (firstcall was called with procedinition=nil !)
  968. * changed typen to what Florian proposed
  969. gentypenode(p : pdef) sets the typenodetype field
  970. and resulttype is only set if inside bt_type block !
  971. Revision 1.14 1998/10/20 08:06:40 pierre
  972. * several memory corruptions due to double freemem solved
  973. => never use p^.loc.location:=p^.left^.loc.location;
  974. + finally I added now by default
  975. that ra386dir translates global and unit symbols
  976. + added a first field in tsymtable and
  977. a nextsym field in tsym
  978. (this allows to obtain ordered type info for
  979. records and objects in gdb !)
  980. Revision 1.13 1998/10/13 16:50:02 pierre
  981. * undid some changes of Peter that made the compiler wrong
  982. for m68k (I had to reinsert some ifdefs)
  983. * removed several memory leaks under m68k
  984. * removed the meory leaks for assembler readers
  985. * cross compiling shoud work again better
  986. ( crosscompiling sysamiga works
  987. but as68k still complain about some code !)
  988. Revision 1.12 1998/10/08 17:17:12 pierre
  989. * current_module old scanner tagged as invalid if unit is recompiled
  990. + added ppheap for better info on tracegetmem of heaptrc
  991. (adds line column and file index)
  992. * several memory leaks removed ith help of heaptrc !!
  993. Revision 1.11 1998/10/05 21:33:15 peter
  994. * fixed 161,165,166,167,168
  995. Revision 1.10 1998/10/05 12:32:44 peter
  996. + assert() support
  997. Revision 1.8 1998/10/02 10:35:09 peter
  998. * support for inc(pointer,value) which now increases with value instead
  999. of 0*value :)
  1000. Revision 1.7 1998/09/21 08:45:07 pierre
  1001. + added vmt_offset in tobjectdef.write for fututre use
  1002. (first steps to have objects without vmt if no virtual !!)
  1003. + added fpu_used field for tabstractprocdef :
  1004. sets this level to 2 if the functions return with value in FPU
  1005. (is then set to correct value at parsing of implementation)
  1006. THIS MIGHT refuse some code with FPU expression too complex
  1007. that were accepted before and even in some cases
  1008. that don't overflow in fact
  1009. ( like if f : float; is a forward that finally in implementation
  1010. only uses one fpu register !!)
  1011. Nevertheless I think that it will improve security on
  1012. FPU operations !!
  1013. * most other changes only for UseBrowser code
  1014. (added symtable references for record and objects)
  1015. local switch for refs to args and local of each function
  1016. (static symtable still missing)
  1017. UseBrowser still not stable and probably broken by
  1018. the definition hash array !!
  1019. Revision 1.6 1998/09/20 12:26:37 peter
  1020. * merged fixes
  1021. Revision 1.5 1998/09/17 09:42:15 peter
  1022. + pass_2 for cg386
  1023. * Message() -> CGMessage() for pass_1/pass_2
  1024. Revision 1.4 1998/09/14 10:43:49 peter
  1025. * all internal RTL functions start with FPC_
  1026. Revision 1.3.2.1 1998/09/20 12:20:07 peter
  1027. * Fixed stack not on 4 byte boundary when doing a call
  1028. Revision 1.3 1998/09/05 23:03:57 florian
  1029. * some fixes to get -Or work:
  1030. - inc/dec didn't take care of CREGISTER
  1031. - register calculcation of inc/dec was wrong
  1032. - var/const parameters get now assigned 32 bit register, but
  1033. const parameters only if they are passed by reference !
  1034. Revision 1.2 1998/09/04 08:41:40 peter
  1035. * updated some error CGMessages
  1036. Revision 1.1 1998/08/31 12:22:14 peter
  1037. * secondinline moved to cg386inl
  1038. Revision 1.19 1998/08/31 08:52:03 peter
  1039. * fixed error 10 with succ() and pref()
  1040. Revision 1.18 1998/08/20 21:36:38 peter
  1041. * fixed 'with object do' bug
  1042. Revision 1.17 1998/08/19 16:07:36 jonas
  1043. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1044. Revision 1.16 1998/08/18 09:24:36 pierre
  1045. * small warning position bug fixed
  1046. * support_mmx switches splitting was missing
  1047. * rhide error and warning output corrected
  1048. Revision 1.15 1998/08/13 11:00:09 peter
  1049. * fixed procedure<>procedure construct
  1050. Revision 1.14 1998/08/11 14:05:33 peter
  1051. * fixed sizeof(array of char)
  1052. Revision 1.13 1998/08/10 14:49:45 peter
  1053. + localswitches, moduleswitches, globalswitches splitting
  1054. Revision 1.12 1998/07/30 13:30:31 florian
  1055. * final implemenation of exception support, maybe it needs
  1056. some fixes :)
  1057. Revision 1.11 1998/07/24 22:16:52 florian
  1058. * internal error 10 together with array access fixed. I hope
  1059. that's the final fix.
  1060. Revision 1.10 1998/07/18 22:54:23 florian
  1061. * some ansi/wide/longstring support fixed:
  1062. o parameter passing
  1063. o returning as result from functions
  1064. Revision 1.9 1998/07/07 17:40:37 peter
  1065. * packrecords 4 works
  1066. * word aligning of parameters
  1067. Revision 1.8 1998/07/06 15:51:15 michael
  1068. Added length checking for string reading
  1069. Revision 1.7 1998/07/06 14:19:51 michael
  1070. + Added calls for reading/writing ansistrings
  1071. Revision 1.6 1998/07/01 15:28:48 peter
  1072. + better writeln/readln handling, now 100% like tp7
  1073. Revision 1.5 1998/06/25 14:04:17 peter
  1074. + internal inc/dec
  1075. Revision 1.4 1998/06/25 08:48:06 florian
  1076. * first version of rtti support
  1077. Revision 1.3 1998/06/09 16:01:33 pierre
  1078. + added procedure directive parsing for procvars
  1079. (accepted are popstack cdecl and pascal)
  1080. + added C vars with the following syntax
  1081. var C calias 'true_c_name';(can be followed by external)
  1082. reason is that you must add the Cprefix
  1083. which is target dependent
  1084. Revision 1.2 1998/06/08 13:13:29 pierre
  1085. + temporary variables now in temp_gen.pas unit
  1086. because it is processor independent
  1087. * mppc68k.bat modified to undefine i386 and support_mmx
  1088. (which are defaults for i386)
  1089. Revision 1.1 1998/06/05 17:44:10 peter
  1090. * splitted cgi386
  1091. }