cg68kinl.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate m68k 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 cg68kinl;
  19. interface
  20. uses
  21. tree;
  22. procedure secondinline(var p : ptree);
  23. implementation
  24. uses
  25. cobjects,verbose,globals,systems,
  26. aasm,types,symtable,
  27. hcodegen,temp_gen,pass_2,
  28. m68k,cga68k,tgen68k,cg68kld,cg68kcal;
  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. addsubop:array[in_inc_x..in_dec_x] of tasmop=(A_ADDQ,A_SUBQ);
  61. var
  62. aktfile : treference;
  63. ft : tfiletype;
  64. opsize : topsize;
  65. asmop : tasmop;
  66. pushed : tpushed;
  67. {inc/dec}
  68. addconstant : boolean;
  69. addvalue : longint;
  70. procedure handlereadwrite(doread,doln : boolean);
  71. { produces code for READ(LN) and WRITE(LN) }
  72. procedure loadstream;
  73. const
  74. io:array[0..1] of string[7]=('_OUTPUT','_INPUT');
  75. var
  76. r : preference;
  77. begin
  78. new(r);
  79. reset_reference(r^);
  80. r^.symbol:=stringdup('U_'+upper(target_info.system_unit)+io[byte(doread)]);
  81. concat_external(r^.symbol^,EXT_NEAR);
  82. exprasmlist^.concat(new(pai68k,op_ref_reg(A_LEA,S_L,r,R_A0)))
  83. end;
  84. var
  85. node,hp : ptree;
  86. typedtyp,
  87. pararesult : pdef;
  88. has_length : boolean;
  89. dummycoll : tdefcoll;
  90. iolabel : plabel;
  91. npara : longint;
  92. begin
  93. { I/O check }
  94. if cs_check_io in aktlocalswitches then
  95. begin
  96. getlabel(iolabel);
  97. emitl(A_LABEL,iolabel);
  98. end
  99. else
  100. iolabel:=nil;
  101. { for write of real with the length specified }
  102. has_length:=false;
  103. hp:=nil;
  104. { reserve temporary pointer to data variable }
  105. aktfile.symbol:=nil;
  106. gettempofsizereference(4,aktfile);
  107. { first state text data }
  108. ft:=ft_text;
  109. { and state a parameter ? }
  110. if p^.left=nil then
  111. begin
  112. { the following instructions are for "writeln;" }
  113. loadstream;
  114. { save @aktfile in temporary variable }
  115. exprasmlist^.concat(new(pai68k,op_reg_ref(A_MOVE,S_L,R_A0,newreference(aktfile))));
  116. end
  117. else
  118. begin
  119. { revers paramters }
  120. node:=reversparameter(p^.left);
  121. p^.left := node;
  122. npara := nb_para;
  123. { calculate data variable }
  124. { is first parameter a file type ? }
  125. if node^.left^.resulttype^.deftype=filedef then
  126. begin
  127. ft:=pfiledef(node^.left^.resulttype)^.filetype;
  128. if ft=ft_typed then
  129. typedtyp:=pfiledef(node^.left^.resulttype)^.typed_as;
  130. secondpass(node^.left);
  131. if codegenerror then
  132. exit;
  133. { save reference in temporary variables }
  134. if node^.left^.location.loc<>LOC_REFERENCE then
  135. begin
  136. CGMessage(cg_e_illegal_expression);
  137. exit;
  138. end;
  139. exprasmlist^.concat(new(pai68k,op_ref_reg(A_LEA,S_L,newreference(node^.left^.location.reference),R_A0)));
  140. { skip to the next parameter }
  141. node:=node^.right;
  142. end
  143. else
  144. begin
  145. { load stdin/stdout stream }
  146. loadstream;
  147. end;
  148. { save @aktfile in temporary variable }
  149. exprasmlist^.concat(new(pai68k,op_reg_ref(A_MOVE,S_L,R_A0,newreference(aktfile))));
  150. if doread then
  151. { parameter by READ gives call by reference }
  152. dummycoll.paratyp:=vs_var
  153. { an WRITE Call by "Const" }
  154. else
  155. dummycoll.paratyp:=vs_const;
  156. { because of secondcallparan, which otherwise attaches }
  157. if ft=ft_typed then
  158. { this is to avoid copy of simple const parameters }
  159. dummycoll.data:=new(pformaldef,init)
  160. else
  161. { I think, this isn't a good solution (FK) }
  162. dummycoll.data:=nil;
  163. while assigned(node) do
  164. begin
  165. pushusedregisters(pushed,$ff);
  166. hp:=node;
  167. node:=node^.right;
  168. hp^.right:=nil;
  169. if hp^.is_colon_para then
  170. CGMessage(parser_e_illegal_colon_qualifier);
  171. if ft=ft_typed then
  172. never_copy_const_param:=true;
  173. secondcallparan(hp,@dummycoll,false);
  174. if ft=ft_typed then
  175. never_copy_const_param:=false;
  176. hp^.right:=node;
  177. if codegenerror then
  178. exit;
  179. emit_push_mem(aktfile);
  180. if (ft=ft_typed) then
  181. begin
  182. { OK let's try this }
  183. { first we must only allow the right type }
  184. { we have to call blockread or blockwrite }
  185. { but the real problem is that }
  186. { reset and rewrite should have set }
  187. { the type size }
  188. { as recordsize for that file !!!! }
  189. { how can we make that }
  190. { I think that is only possible by adding }
  191. { reset and rewrite to the inline list a call }
  192. { allways read only one record by element }
  193. push_int(typedtyp^.size);
  194. if doread then
  195. emitcall('FPC_TYPED_READ',true)
  196. else
  197. emitcall('FPC_TYPED_WRITE',true);
  198. end
  199. else
  200. begin
  201. { save current position }
  202. pararesult:=hp^.left^.resulttype;
  203. { handle possible field width }
  204. { of course only for write(ln) }
  205. if not doread then
  206. begin
  207. { handle total width parameter }
  208. if assigned(node) and node^.is_colon_para then
  209. begin
  210. hp:=node;
  211. node:=node^.right;
  212. hp^.right:=nil;
  213. secondcallparan(hp,@dummycoll,false);
  214. hp^.right:=node;
  215. if codegenerror then
  216. exit;
  217. has_length:=true;
  218. end
  219. else
  220. if pararesult^.deftype<>floatdef then
  221. push_int(0)
  222. else
  223. push_int(-32767);
  224. { a second colon para for a float ? }
  225. if assigned(node) and node^.is_colon_para then
  226. begin
  227. hp:=node;
  228. node:=node^.right;
  229. hp^.right:=nil;
  230. secondcallparan(hp,@dummycoll,false);
  231. hp^.right:=node;
  232. if pararesult^.deftype<>floatdef then
  233. CGMessage(parser_e_illegal_colon_qualifier);
  234. if codegenerror then
  235. exit;
  236. end
  237. else
  238. begin
  239. if pararesult^.deftype=floatdef then
  240. push_int(-1);
  241. end
  242. end;
  243. case pararesult^.deftype of
  244. stringdef : begin
  245. if doread then
  246. begin
  247. { push maximum string length }
  248. push_int(pstringdef(pararesult)^.len);
  249. case pstringdef(pararesult)^.string_typ of
  250. st_shortstring:
  251. emitcall ('FPC_READ_TEXT_STRING',true);
  252. st_ansistring:
  253. emitcall ('FPC_READ_TEXT_ANSISTRING',true);
  254. st_longstring:
  255. emitcall ('FPC_READ_TEXT_LONGSTRING',true);
  256. st_widestring:
  257. emitcall ('FPC_READ_TEXT_ANSISTRING',true);
  258. end
  259. end
  260. else
  261. Case pstringdef(Pararesult)^.string_typ of
  262. st_shortstring:
  263. emitcall ('FPC_WRITE_TEXT_STRING',true);
  264. st_ansistring:
  265. emitcall ('FPC_WRITE_TEXT_ANSISTRING',true);
  266. st_longstring:
  267. emitcall ('FPC_WRITE_TEXT_LONGSTRING',true);
  268. st_widestring:
  269. emitcall ('FPC_WRITE_TEXT_ANSISTRING',true);
  270. end;
  271. end;
  272. pointerdef : begin
  273. if is_equal(ppointerdef(pararesult)^.definition,cchardef) then
  274. begin
  275. if doread then
  276. emitcall('FPC_READ_TEXT_PCHAR_AS_POINTER',true)
  277. else
  278. emitcall('FPC_WRITE_TEXT_PCHAR_AS_POINTER',true);
  279. end;
  280. end;
  281. arraydef : begin
  282. if (parraydef(pararesult)^.lowrange=0) and
  283. is_equal(parraydef(pararesult)^.definition,cchardef) 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_loada5;
  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_loada5;
  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(pai68k,op_csymbol(A_PEA,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. if codegenerror then
  406. exit;
  407. dummycoll.paratyp:=vs_const;
  408. disposetree(hp);
  409. p^.left:=nil;
  410. { second arg }
  411. hp:=node;
  412. node:=node^.right;
  413. hp^.right:=nil;
  414. { frac para }
  415. if hp^.is_colon_para and assigned(node) and
  416. node^.is_colon_para then
  417. begin
  418. dummycoll.data:=hp^.resulttype;
  419. secondcallparan(hp,@dummycoll,false);
  420. if codegenerror then
  421. exit;
  422. disposetree(hp);
  423. hp:=node;
  424. node:=node^.right;
  425. hp^.right:=nil;
  426. has_length:=true;
  427. end
  428. else
  429. if is_real then
  430. push_int(-1);
  431. { third arg, length only if is_real }
  432. if hp^.is_colon_para then
  433. begin
  434. dummycoll.data:=hp^.resulttype;
  435. secondcallparan(hp,@dummycoll,false);
  436. if codegenerror then
  437. exit;
  438. disposetree(hp);
  439. hp:=node;
  440. node:=node^.right;
  441. hp^.right:=nil;
  442. end
  443. else
  444. if is_real then
  445. push_int(-32767)
  446. else
  447. push_int(-1);
  448. { last arg longint or real }
  449. secondcallparan(hp,@dummycoll,false);
  450. if codegenerror then
  451. exit;
  452. disposetree(hp);
  453. if is_real then
  454. emitcall('FPC_STR_'+float_name[pfloatdef(hp^.resulttype)^.typ],true)
  455. else if porddef(hp^.resulttype)^.typ=u32bit then
  456. emitcall('FPC_STR_CARDINAL',true)
  457. else
  458. emitcall('FPC_STR_LONGINT',true);
  459. popusedregisters(pushed);
  460. end;
  461. var
  462. r : preference;
  463. l : longint;
  464. ispushed : boolean;
  465. hregister : tregister;
  466. otlabel,oflabel,filenamestring : plabel;
  467. oldpushedparasize : longint;
  468. begin
  469. { save & reset pushedparasize }
  470. oldpushedparasize:=pushedparasize;
  471. pushedparasize:=0;
  472. case p^.inlinenumber of
  473. in_assert_x_y:
  474. begin
  475. { !!!!!!!!! }
  476. end;
  477. in_lo_word,
  478. in_hi_word :
  479. begin
  480. secondpass(p^.left);
  481. p^.location.loc:=LOC_REGISTER;
  482. if p^.left^.location.loc<>LOC_REGISTER then
  483. begin
  484. if p^.left^.location.loc=LOC_CREGISTER then
  485. begin
  486. p^.location.register:=getregister32;
  487. emit_reg_reg(A_MOVE,S_W,p^.left^.location.register,
  488. p^.location.register);
  489. end
  490. else
  491. begin
  492. del_reference(p^.left^.location.reference);
  493. p^.location.register:=getregister32;
  494. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_W,
  495. newreference(p^.left^.location.reference),
  496. p^.location.register)));
  497. end;
  498. end
  499. else p^.location.register:=p^.left^.location.register;
  500. if p^.inlinenumber=in_hi_word then
  501. exprasmlist^.concat(new(pai68k,op_const_reg(A_LSR,S_W,8,p^.location.register)));
  502. p^.location.register:=p^.location.register;
  503. end;
  504. in_high_x :
  505. begin
  506. if is_open_array(p^.left^.resulttype) then
  507. begin
  508. secondpass(p^.left);
  509. del_reference(p^.left^.location.reference);
  510. p^.location.register:=getregister32;
  511. new(r);
  512. reset_reference(r^);
  513. r^.base:=highframepointer;
  514. r^.offset:=highoffset+4;
  515. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,
  516. r,p^.location.register)));
  517. end
  518. end;
  519. in_sizeof_x,
  520. in_typeof_x :
  521. begin
  522. { sizeof(openarray) handling }
  523. if (p^.inlinenumber=in_sizeof_x) and
  524. is_open_array(p^.left^.resulttype) then
  525. begin
  526. { sizeof(openarray)=high(openarray)+1 }
  527. secondpass(p^.left);
  528. del_reference(p^.left^.location.reference);
  529. p^.location.register:=getregister32;
  530. new(r);
  531. reset_reference(r^);
  532. r^.base:=highframepointer;
  533. r^.offset:=highoffset+4;
  534. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,
  535. r,p^.location.register)));
  536. exprasmlist^.concat(new(pai68k,op_const_reg(A_ADD,S_L,
  537. 1,p^.location.register)));
  538. if parraydef(p^.left^.resulttype)^.elesize<>1 then
  539. exprasmlist^.concat(new(pai68k,op_const_reg(A_MULS,S_L,
  540. parraydef(p^.left^.resulttype)^.elesize,p^.location.register)));
  541. end
  542. else
  543. begin
  544. { for both cases load vmt }
  545. if p^.left^.treetype=typen then
  546. begin
  547. exprasmlist^.concat(new(pai68k,op_csymbol_reg(A_LEA,
  548. S_L,newcsymbol(pobjectdef(p^.left^.resulttype)^.vmt_mangledname,0),
  549. R_A0)));
  550. p^.location.register:=getregister32;
  551. emit_reg_reg(A_MOVE,S_L,R_A0,p^.location.register);
  552. end
  553. else
  554. begin
  555. secondpass(p^.left);
  556. del_reference(p^.left^.location.reference);
  557. p^.location.loc:=LOC_REGISTER;
  558. p^.location.register:=getregister32;
  559. { load VMT pointer }
  560. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,
  561. newreference(p^.left^.location.reference),
  562. p^.location.register)));
  563. end;
  564. { in sizeof load size }
  565. if p^.inlinenumber=in_sizeof_x then
  566. begin
  567. new(r);
  568. reset_reference(r^);
  569. { load the address in A0 }
  570. { because now supposedly p^.location.register is an }
  571. { address. }
  572. emit_reg_reg(A_MOVE, S_L, p^.location.register, R_A0);
  573. r^.base:=R_A0;
  574. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,r,
  575. p^.location.register)));
  576. end;
  577. end;
  578. end;
  579. in_lo_long,
  580. in_hi_long : begin
  581. secondpass(p^.left);
  582. p^.location.loc:=LOC_REGISTER;
  583. if p^.left^.location.loc<>LOC_REGISTER then
  584. begin
  585. if p^.left^.location.loc=LOC_CREGISTER then
  586. begin
  587. p^.location.register:=getregister32;
  588. emit_reg_reg(A_MOVE,S_L,p^.left^.location.register,
  589. p^.location.register);
  590. end
  591. else
  592. begin
  593. del_reference(p^.left^.location.reference);
  594. p^.location.register:=getregister32;
  595. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,
  596. newreference(p^.left^.location.reference),
  597. p^.location.register)));
  598. end;
  599. end
  600. else p^.location.register:=p^.left^.location.register;
  601. if p^.inlinenumber=in_hi_long then
  602. begin
  603. exprasmlist^.concat(new(pai68k,op_const_reg(A_MOVEQ, S_L, 16, R_D1)));
  604. exprasmlist^.concat(new(pai68k,op_reg_reg(A_LSR,S_L,R_D1,p^.location.register)));
  605. end;
  606. p^.location.register:=p^.location.register;
  607. end;
  608. in_length_string :
  609. begin
  610. secondpass(p^.left);
  611. set_location(p^.location,p^.left^.location);
  612. { length in ansi strings is at offset -8 }
  613. {$ifdef UseAnsiString}
  614. if is_ansistring(p^.left^.resulttype) then
  615. dec(p^.location.reference.offset,8);
  616. {$endif UseAnsiString}
  617. end;
  618. in_pred_x,
  619. in_succ_x:
  620. begin
  621. secondpass(p^.left);
  622. if p^.inlinenumber=in_pred_x then
  623. asmop:=A_SUB
  624. else
  625. asmop:=A_ADD;
  626. case p^.resulttype^.size of
  627. 4 : opsize:=S_L;
  628. 2 : opsize:=S_W;
  629. 1 : opsize:=S_B;
  630. else
  631. internalerror(10080);
  632. end;
  633. p^.location.loc:=LOC_REGISTER;
  634. if p^.left^.location.loc<>LOC_REGISTER then
  635. begin
  636. p^.location.register:=getregister32;
  637. if p^.left^.location.loc=LOC_CREGISTER then
  638. emit_reg_reg(A_MOVE,opsize,p^.left^.location.register,
  639. p^.location.register)
  640. else
  641. if p^.left^.location.loc=LOC_FLAGS then
  642. exprasmlist^.concat(new(pai68k,op_reg(flag_2_set[p^.left^.location.resflags],S_NO,
  643. p^.location.register)))
  644. else
  645. begin
  646. del_reference(p^.left^.location.reference);
  647. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,opsize,newreference(p^.left^.location.reference),
  648. p^.location.register)));
  649. end;
  650. end
  651. else p^.location.register:=p^.left^.location.register;
  652. exprasmlist^.concat(new(pai68k,op_const_reg(asmop,opsize,1,
  653. p^.location.register)))
  654. { here we should insert bounds check ? }
  655. { and direct call to bounds will crash the program }
  656. { if we are at the limit }
  657. { we could also simply say that pred(first)=first and succ(last)=last }
  658. { could this be usefull I don't think so (PM)
  659. emitoverflowcheck;}
  660. end;
  661. in_dec_x,
  662. in_inc_x :
  663. begin
  664. { set defaults }
  665. addvalue:=1;
  666. addconstant:=true;
  667. { load first parameter, must be a reference }
  668. secondpass(p^.left^.left);
  669. case p^.left^.left^.resulttype^.deftype of
  670. orddef,
  671. enumdef : begin
  672. case p^.left^.left^.resulttype^.size of
  673. 1 : opsize:=S_B;
  674. 2 : opsize:=S_W;
  675. 4 : opsize:=S_L;
  676. end;
  677. end;
  678. pointerdef : begin
  679. opsize:=S_L;
  680. addvalue:=ppointerdef(p^.left^.left^.resulttype)^.definition^.savesize;
  681. end;
  682. else
  683. internalerror(10081);
  684. end;
  685. { second argument specified?, must be a s32bit in register }
  686. if assigned(p^.left^.right) then
  687. begin
  688. secondpass(p^.left^.right^.left);
  689. { when constant, just multiply the addvalue }
  690. if is_constintnode(p^.left^.right^.left) then
  691. addvalue:=addvalue*get_ordinal_value(p^.left^.right^.left)
  692. else
  693. begin
  694. case p^.left^.right^.left^.location.loc of
  695. LOC_REGISTER,
  696. LOC_CREGISTER : hregister:=p^.left^.right^.left^.location.register;
  697. LOC_MEM,
  698. LOC_REFERENCE : begin
  699. hregister:=getregister32;
  700. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,
  701. newreference(p^.left^.right^.left^.location.reference),hregister)));
  702. end;
  703. else
  704. internalerror(10082);
  705. end;
  706. { insert multiply with addvalue if its >1 }
  707. if addvalue>1 then
  708. exprasmlist^.concat(new(pai68k,op_const_reg(A_MULS,opsize,
  709. addvalue,hregister)));
  710. addconstant:=false;
  711. end;
  712. end;
  713. { write the add instruction }
  714. if addconstant then
  715. begin
  716. exprasmlist^.concat(new(pai68k,op_const_ref(addsubop[p^.inlinenumber],opsize,
  717. addvalue,newreference(p^.left^.left^.location.reference))));
  718. end
  719. else
  720. begin
  721. exprasmlist^.concat(new(pai68k,op_reg_ref(addsubop[p^.inlinenumber],opsize,
  722. hregister,newreference(p^.left^.left^.location.reference))));
  723. ungetregister32(hregister);
  724. end;
  725. emitoverflowcheck(p^.left^.left);
  726. end;
  727. in_assigned_x :
  728. begin
  729. secondpass(p^.left^.left);
  730. p^.location.loc:=LOC_FLAGS;
  731. if (p^.left^.left^.location.loc=LOC_REGISTER) or
  732. (p^.left^.left^.location.loc=LOC_CREGISTER) then
  733. begin
  734. exprasmlist^.concat(new(pai68k,op_reg(A_TST,S_L,
  735. p^.left^.left^.location.register)));
  736. ungetregister32(p^.left^.left^.location.register);
  737. end
  738. else
  739. begin
  740. exprasmlist^.concat(new(pai68k,op_ref(A_TST,S_L,
  741. newreference(p^.left^.left^.location.reference))));
  742. del_reference(p^.left^.left^.location.reference);
  743. end;
  744. p^.location.resflags:=F_NE;
  745. end;
  746. in_reset_typedfile,in_rewrite_typedfile :
  747. begin
  748. pushusedregisters(pushed,$ffff);
  749. exprasmlist^.concat(new(pai68k,op_const_reg(A_MOVE,S_L,
  750. pfiledef(p^.left^.resulttype)^.typed_as^.size,R_SPPUSH)));
  751. secondload(p^.left);
  752. emitpushreferenceaddr(p^.left^.location.reference);
  753. if p^.inlinenumber=in_reset_typedfile then
  754. emitcall('FPC_RESET_TYPED',true)
  755. else
  756. emitcall('FPC_REWRITE_TYPED',true);
  757. popusedregisters(pushed);
  758. end;
  759. in_write_x :
  760. handlereadwrite(false,false);
  761. in_writeln_x :
  762. handlereadwrite(false,true);
  763. in_read_x :
  764. handlereadwrite(true,false);
  765. in_readln_x :
  766. handlereadwrite(true,true);
  767. in_str_x_string :
  768. begin
  769. handle_str;
  770. maybe_loada5;
  771. end;
  772. in_include_x_y,
  773. in_exclude_x_y:
  774. begin
  775. { !!!!!!! }
  776. (* secondpass(p^.left^.left);
  777. if p^.left^.right^.left^.treetype=ordconstn then
  778. begin
  779. { calculate bit position }
  780. l:=1 shl (p^.left^.right^.left^.value mod 32);
  781. { determine operator }
  782. if p^.inlinenumber=in_include_x_y then
  783. asmop:=A_OR
  784. else
  785. begin
  786. asmop:=A_AND;
  787. l:=not(l);
  788. end;
  789. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  790. begin
  791. inc(p^.left^.left^.location.reference.offset,(p^.left^.right^.left^.value div 32)*4);
  792. exprasmlist^.concat(new(pai68k,op_const_ref(asmop,S_L,
  793. l,newreference(p^.left^.left^.location.reference))));
  794. del_reference(p^.left^.left^.location.reference);
  795. end
  796. else
  797. { LOC_CREGISTER }
  798. exprasmlist^.concat(new(pai68k,op_const_reg(asmop,S_L,
  799. l,p^.left^.left^.location.register)));
  800. end
  801. else
  802. begin
  803. { generate code for the element to set }
  804. ispushed:=maybe_push(p^.left^.right^.left^.registers32,p^.left^.left);
  805. secondpass(p^.left^.right^.left);
  806. if ispushed then
  807. restore(p^.left^.left);
  808. { determine asm operator }
  809. if p^.inlinenumber=in_include_x_y then
  810. asmop:=A_BTS
  811. else
  812. asmop:=A_BTR;
  813. if psetdef(p^.left^.resulttype)^.settype=smallset then
  814. begin
  815. if p^.left^.right^.left^.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  816. hregister:=p^.left^.right^.left^.location.register
  817. else
  818. begin
  819. hregister:=R_EDI;
  820. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  821. newreference(p^.left^.right^.left^.location.reference),R_EDI)));
  822. end;
  823. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  824. exprasmlist^.concat(new(pai386,op_reg_ref(asmop,S_L,R_EDI,
  825. newreference(p^.left^.right^.left^.location.reference))))
  826. else
  827. exprasmlist^.concat(new(pai386,op_reg_reg(asmop,S_L,R_EDI,
  828. p^.left^.right^.left^.location.register)));
  829. end
  830. else
  831. begin
  832. end;
  833. end;
  834. *)
  835. end;
  836. else
  837. internalerror(9);
  838. end;
  839. pushedparasize:=oldpushedparasize;
  840. end;
  841. end.
  842. {
  843. $Log$
  844. Revision 1.7 1998-10-13 16:50:08 pierre
  845. * undid some changes of Peter that made the compiler wrong
  846. for m68k (I had to reinsert some ifdefs)
  847. * removed several memory leaks under m68k
  848. * removed the meory leaks for assembler readers
  849. * cross compiling shoud work again better
  850. ( crosscompiling sysamiga works
  851. but as68k still complain about some code !)
  852. Revision 1.6 1998/10/06 20:48:58 peter
  853. * m68k compiler compiles again
  854. Revision 1.5 1998/09/20 12:26:39 peter
  855. * merged fixes
  856. Revision 1.4 1998/09/17 09:42:26 peter
  857. + pass_2 for cg386
  858. * Message() -> CGMessage() for pass_1/pass_2
  859. Revision 1.3 1998/09/14 10:43:59 peter
  860. * all internal RTL functions start with FPC_
  861. Revision 1.2.2.1 1998/09/20 12:20:10 peter
  862. * Fixed stack not on 4 byte boundary when doing a call
  863. Revision 1.2 1998/09/04 08:41:48 peter
  864. * updated some error CGMessages
  865. Revision 1.1 1998/09/01 09:07:09 peter
  866. * m68k fixes, splitted cg68k like cgi386
  867. }