cg68kcal.pas 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Generate m68k assembler for in call 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. {$ifdef FPC}
  19. {$goto on}
  20. {$endif FPC}
  21. unit cg68kcal;
  22. interface
  23. uses
  24. symtable,tree;
  25. { save the size of pushed parameter }
  26. var
  27. pushedparasize : longint;
  28. procedure secondcallparan(var p : ptree;defcoll : pdefcoll;
  29. push_from_left_to_right : boolean);
  30. procedure secondcalln(var p : ptree);
  31. procedure secondprocinline(var p : ptree);
  32. implementation
  33. uses
  34. globtype,systems,symconst,
  35. cobjects,verbose,globals,
  36. aasm,types,
  37. hcodegen,temp_gen,pass_2,
  38. cpubase,cga68k,tgen68k,cg68kld;
  39. {*****************************************************************************
  40. SecondCallParaN
  41. *****************************************************************************}
  42. procedure secondcallparan(var p : ptree;defcoll : pdefcoll;
  43. push_from_left_to_right : boolean);
  44. procedure maybe_push_open_array_high;
  45. var
  46. r : preference;
  47. begin
  48. { open array ? }
  49. { defcoll^.data can be nil for read/write }
  50. if assigned(defcoll^.data) and
  51. is_open_array(defcoll^.data) then
  52. begin
  53. inc(pushedparasize,4);
  54. { push high }
  55. if is_open_array(p^.left^.resulttype) then
  56. begin
  57. new(r);
  58. reset_reference(r^);
  59. r^.base:=highframepointer;
  60. r^.offset:=highoffset+4;
  61. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_L,r,R_SPPUSH)));
  62. end
  63. else
  64. push_int(parraydef(p^.left^.resulttype)^.highrange-parraydef(p^.left^.resulttype)^.lowrange);
  65. end;
  66. end;
  67. var
  68. size : longint;
  69. stackref : treference;
  70. otlabel,hlabel,oflabel : pasmlabel;
  71. { temporary variables: }
  72. reg : tregister;
  73. tempdeftype : tdeftype;
  74. tempreference : treference;
  75. r : preference;
  76. s : topsize;
  77. op : tasmop;
  78. begin
  79. { push from left to right if specified }
  80. if push_from_left_to_right and assigned(p^.right) then
  81. secondcallparan(p^.right,defcoll^.next,push_from_left_to_right);
  82. otlabel:=truelabel;
  83. oflabel:=falselabel;
  84. getlabel(truelabel);
  85. getlabel(falselabel);
  86. secondpass(p^.left);
  87. { in codegen.handleread.. defcoll^.data is set to nil }
  88. if assigned(defcoll^.data) and
  89. (defcoll^.data^.deftype=formaldef) then
  90. begin
  91. { allow @var }
  92. if p^.left^.treetype=addrn then
  93. begin
  94. { allways a register }
  95. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_L,p^.left^.location.register,R_SPPUSH)));
  96. ungetregister32(p^.left^.location.register);
  97. end
  98. else
  99. begin
  100. if (p^.left^.location.loc<>LOC_REFERENCE) and
  101. (p^.left^.location.loc<>LOC_MEM) then
  102. CGMessage(type_e_mismatch)
  103. else
  104. begin
  105. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  106. del_reference(p^.left^.location.reference);
  107. end;
  108. end;
  109. inc(pushedparasize,4);
  110. end
  111. { handle call by reference parameter }
  112. else if (defcoll^.paratyp=vs_var) then
  113. begin
  114. if (p^.left^.location.loc<>LOC_REFERENCE) then
  115. CGMessage(cg_e_var_must_be_reference);
  116. maybe_push_open_array_high;
  117. inc(pushedparasize,4);
  118. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  119. del_reference(p^.left^.location.reference);
  120. end
  121. else
  122. begin
  123. tempdeftype:=p^.resulttype^.deftype;
  124. if tempdeftype=filedef then
  125. CGMessage(cg_e_file_must_call_by_reference);
  126. if (assigned(defcoll^.data) and
  127. is_open_array(defcoll^.data)) or
  128. push_addr_param(p^.resulttype) then
  129. begin
  130. maybe_push_open_array_high;
  131. inc(pushedparasize,4);
  132. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  133. del_reference(p^.left^.location.reference);
  134. end
  135. else
  136. case p^.left^.location.loc of
  137. LOC_REGISTER,
  138. LOC_CREGISTER : begin
  139. { HERE IS A BIG PROBLEM }
  140. { --> We *MUST* know the data size to push }
  141. { for the moment, we can say that the savesize }
  142. { indicates the parameter size to push, but }
  143. { that is CERTAINLY NOT TRUE! }
  144. { CAN WE USE LIKE LOC_MEM OR LOC_REFERENCE?? }
  145. case integer(p^.left^.resulttype^.size) of
  146. 1 : Begin
  147. { A byte sized value normally increments }
  148. { the SP by 2, BUT because how memory has }
  149. { been setup OR because of GAS, a byte sized }
  150. { push CRASHES the Amiga, therefore, we do it }
  151. { by hand instead. }
  152. { PUSH A WORD SHIFTED LEFT 8 }
  153. reg := getregister32;
  154. emit_reg_reg(A_MOVE, S_B, p^.left^.location.register, reg);
  155. exprasmlist^.concat(new(paicpu,op_const_reg(A_LSL,S_W,
  156. 8, reg)));
  157. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_W,
  158. reg,R_SPPUSH)));
  159. { offset will be TWO greater }
  160. inc(pushedparasize,2);
  161. ungetregister32(reg);
  162. ungetregister32(p^.left^.location.register);
  163. end;
  164. 2 :
  165. Begin
  166. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_W,
  167. p^.left^.location.register,R_SPPUSH)));
  168. inc(pushedparasize,2);
  169. ungetregister32(p^.left^.location.register);
  170. end;
  171. 4 : Begin
  172. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_L,
  173. p^.left^.location.register,R_SPPUSH)));
  174. inc(pushedparasize,4);
  175. ungetregister32(p^.left^.location.register);
  176. end;
  177. else
  178. Begin
  179. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_L,
  180. p^.left^.location.register,R_SPPUSH)));
  181. inc(pushedparasize,4);
  182. ungetregister32(p^.left^.location.register);
  183. end;
  184. end; { end case }
  185. end;
  186. LOC_FPU : begin
  187. size:=pfloatdef(p^.left^.resulttype)^.size;
  188. inc(pushedparasize,size);
  189. { how now how long a FPU is !! }
  190. if (size > 0) and (size < 9) then
  191. exprasmlist^.concat(new(paicpu,op_const_reg(A_SUBQ,S_L,size,R_SP)))
  192. else
  193. exprasmlist^.concat(new(paicpu,op_const_reg(A_SUBA,
  194. S_L,size,R_SP)));
  195. new(r);
  196. reset_reference(r^);
  197. r^.base:=R_SP;
  198. s:=getfloatsize(pfloatdef(p^.left^.resulttype)^.typ);
  199. if (cs_fp_emulation in aktmoduleswitches) or (s=S_FS) then
  200. begin
  201. { when in emulation mode... }
  202. { only single supported!!! }
  203. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOVE,S_L,
  204. p^.left^.location.fpureg,r)));
  205. end
  206. else
  207. { convert back from extended to normal type }
  208. exprasmlist^.concat(new(paicpu,op_reg_ref(A_FMOVE,s,
  209. p^.left^.location.fpureg,r)));
  210. end;
  211. LOC_REFERENCE,LOC_MEM :
  212. begin
  213. tempreference:=p^.left^.location.reference;
  214. del_reference(p^.left^.location.reference);
  215. case p^.resulttype^.deftype of
  216. enumdef,
  217. orddef : begin
  218. case p^.resulttype^.size of
  219. 4 : begin
  220. emit_push_mem(tempreference);
  221. inc(pushedparasize,4);
  222. end;
  223. 1 : Begin
  224. { We push a BUT, the SP is incremented by 2 }
  225. { as specified in the Motorola Prog's Ref Manual }
  226. { Therefore offet increments BY 2!!! }
  227. { BUG??? ... }
  228. { SWAP OPERANDS: }
  229. if tempreference.isintvalue then
  230. Begin
  231. exprasmlist^.concat(new(paicpu,op_const_reg(A_MOVE,S_W,
  232. tempreference.offset shl 8,R_SPPUSH)));
  233. end
  234. else
  235. Begin
  236. { A byte sized value normally increments }
  237. { the SP by 2, BUT because how memory has }
  238. { been setup OR because of GAS, a byte sized }
  239. { push CRASHES the Amiga, therefore, we do it }
  240. { by hand instead. }
  241. { PUSH A WORD SHIFTED LEFT 8 }
  242. reg:=getregister32;
  243. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_B,
  244. newreference(tempreference),reg)));
  245. exprasmlist^.concat(new(paicpu,op_const_reg(A_LSL,S_W,
  246. 8, reg)));
  247. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_W,
  248. reg,R_SPPUSH)));
  249. ungetregister32(reg);
  250. { exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_W,
  251. newreference(tempreference),R_SPPUSH))); }
  252. end;
  253. inc(pushedparasize,2);
  254. end;
  255. 2 : begin
  256. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_W,
  257. newreference(tempreference),R_SPPUSH)));
  258. inc(pushedparasize,2);
  259. end;
  260. end;
  261. end;
  262. floatdef : begin
  263. case pfloatdef(p^.resulttype)^.typ of
  264. f32bit,
  265. s32real :
  266. begin
  267. emit_push_mem(tempreference);
  268. inc(pushedparasize,4);
  269. end;
  270. s64real:
  271. {s64bit }
  272. begin
  273. inc(tempreference.offset,4);
  274. emit_push_mem(tempreference);
  275. dec(tempreference.offset,4);
  276. emit_push_mem(tempreference);
  277. inc(pushedparasize,8);
  278. end;
  279. {$ifdef use48}
  280. s48real : begin
  281. end;
  282. {$endif}
  283. s80real : begin
  284. CGMessage(cg_f_extended_cg68k_not_supported);
  285. { inc(tempreference.offset,6);
  286. emit_push_mem(tempreference);
  287. dec(tempreference.offset,4);
  288. emit_push_mem(tempreference);
  289. dec(tempreference.offset,2);
  290. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_W,
  291. newreference(tempreference),R_SPPUSH)));
  292. inc(pushedparasize,extended_size);}
  293. end;
  294. end;
  295. end;
  296. pointerdef,procvardef,
  297. classrefdef: begin
  298. emit_push_mem(tempreference);
  299. inc(pushedparasize,4);
  300. end;
  301. arraydef,recorddef,stringdef,setdef,objectdef :
  302. begin
  303. if ((p^.resulttype^.deftype=setdef) and
  304. (psetdef(p^.resulttype)^.settype=smallset)) then
  305. begin
  306. emit_push_mem(tempreference);
  307. inc(pushedparasize,4);
  308. end
  309. else
  310. begin
  311. size:=p^.resulttype^.size;
  312. { Alignment }
  313. {
  314. if (size>=4) and ((size and 3)<>0) then
  315. inc(size,4-(size and 3))
  316. else if (size>=2) and ((size and 1)<>0) then
  317. inc(size,2-(size and 1))
  318. else
  319. if size=1 then size:=2;
  320. }
  321. { create stack space }
  322. if (size > 0) and (size < 9) then
  323. exprasmlist^.concat(new(paicpu,op_const_reg(A_SUBQ,S_L,size,R_SP)))
  324. else
  325. exprasmlist^.concat(new(paicpu,op_const_reg(A_SUBA,
  326. S_L,size,R_SP)));
  327. inc(pushedparasize,size);
  328. { create stack reference }
  329. stackref.symbol := nil;
  330. clear_reference(stackref);
  331. stackref.base:=R_SP;
  332. { produce copy }
  333. if p^.resulttype^.deftype=stringdef then
  334. begin
  335. copystring(stackref,p^.left^.location.reference,
  336. pstringdef(p^.resulttype)^.len);
  337. end
  338. else
  339. begin
  340. concatcopy(p^.left^.location.reference,
  341. stackref,p^.resulttype^.size,true);
  342. end;
  343. end;
  344. end;
  345. else CGMessage(cg_e_illegal_expression);
  346. end;
  347. end;
  348. LOC_JUMP : begin
  349. getlabel(hlabel);
  350. inc(pushedparasize,2);
  351. emitl(A_LABEL,truelabel);
  352. exprasmlist^.concat(new(paicpu,op_const_reg(A_MOVE,S_W,1 shl 8,R_SPPUSH)));
  353. emitl(A_JMP,hlabel);
  354. emitl(A_LABEL,falselabel);
  355. exprasmlist^.concat(new(paicpu,op_const_reg(A_MOVE,S_W,0,R_SPPUSH)));
  356. emitl(A_LABEL,hlabel);
  357. end;
  358. LOC_FLAGS : begin
  359. exprasmlist^.concat(new(paicpu,op_reg(flag_2_set[p^.left^.location.resflags],S_B,
  360. R_D0)));
  361. exprasmlist^.concat(new(paicpu,op_reg(A_NEG, S_B, R_D0)));
  362. exprasmlist^.concat(new(paicpu,op_const_reg(A_AND,S_W,$ff, R_D0)));
  363. inc(pushedparasize,2);
  364. { ----------------- HACK ----------------------- }
  365. { HERE IS THE BYTE SIZED PUSH HACK ONCE AGAIN }
  366. { SHIFT LEFT THE BYTE TO MAKE IT WORK! }
  367. exprasmlist^.concat(new(paicpu,op_const_reg(A_LSL,S_W,8, R_D0)));
  368. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_W,R_D0,R_SPPUSH)));
  369. end;
  370. end;
  371. end;
  372. truelabel:=otlabel;
  373. falselabel:=oflabel;
  374. { push from right to left }
  375. if not push_from_left_to_right and assigned(p^.right) then
  376. secondcallparan(p^.right,defcoll^.next,push_from_left_to_right);
  377. end;
  378. {*****************************************************************************
  379. SecondCallN
  380. *****************************************************************************}
  381. procedure secondcalln(var p : ptree);
  382. var
  383. unusedregisters : tregisterset;
  384. pushed : tpushed;
  385. funcretref : treference;
  386. hregister : tregister;
  387. oldpushedparasize : longint;
  388. { true if a5 must be loaded again after the subroutine }
  389. loada5 : boolean;
  390. { true if a virtual method must be called directly }
  391. no_virtual_call : boolean;
  392. { true if we produce a con- or destrutor in a call }
  393. is_con_or_destructor : boolean;
  394. { true if a constructor is called again }
  395. extended_new : boolean;
  396. { adress returned from an I/O-error }
  397. iolabel : pasmlabel;
  398. { lexlevel count }
  399. i : longint;
  400. { help reference pointer }
  401. r : preference;
  402. pp,params : ptree;
  403. { temp register allocation }
  404. reg: tregister;
  405. { help reference pointer }
  406. ref: preference;
  407. label
  408. dont_call;
  409. begin
  410. extended_new:=false;
  411. iolabel:=nil;
  412. loada5:=true;
  413. no_virtual_call:=false;
  414. unusedregisters:=unused;
  415. if not assigned(p^.procdefinition) then
  416. exit;
  417. { only if no proc var }
  418. if not(assigned(p^.right)) then
  419. is_con_or_destructor:=(potype_constructor=p^.procdefinition^.proctypeoption)
  420. or (potype_destructor=p^.procdefinition^.proctypeoption);
  421. { proc variables destroy all registers }
  422. if (p^.right=nil) and
  423. { virtual methods too }
  424. (po_virtualmethod in p^.procdefinition^.procoptions) then
  425. begin
  426. if (po_iocheck in p^.procdefinition^.procoptions) and
  427. not(po_iocheck in aktprocsym^.definition^.procoptions) and
  428. (cs_check_io in aktlocalswitches) then
  429. begin
  430. getlabel(iolabel);
  431. emitl(A_LABEL,iolabel);
  432. end
  433. else iolabel:=nil;
  434. { save all used registers }
  435. pushusedregisters(pushed,pprocdef(p^.procdefinition)^.usedregisters);
  436. { give used registers through }
  437. usedinproc:=usedinproc or pprocdef(p^.procdefinition)^.usedregisters;
  438. end
  439. else
  440. begin
  441. pushusedregisters(pushed,$ffff);
  442. usedinproc:=$ffff;
  443. { no IO check for methods and procedure variables }
  444. iolabel:=nil;
  445. end;
  446. { generate the code for the parameter and push them }
  447. oldpushedparasize:=pushedparasize;
  448. pushedparasize:=0;
  449. if (p^.resulttype<>pdef(voiddef)) and
  450. ret_in_param(p^.resulttype) then
  451. begin
  452. funcretref.symbol:=nil;
  453. {$ifdef test_dest_loc}
  454. if dest_loc_known and (dest_loc_tree=p) and
  455. (dest_loc.loc in [LOC_REFERENCE,LOC_MEM]) then
  456. begin
  457. funcretref:=dest_loc.reference;
  458. if assigned(dest_loc.reference.symbol) then
  459. funcretref.symbol:=stringdup(dest_loc.reference.symbol^);
  460. in_dest_loc:=true;
  461. end
  462. else
  463. {$endif test_dest_loc}
  464. gettempofsizereference(p^.procdefinition^.retdef^.size,funcretref);
  465. end;
  466. if assigned(p^.left) then
  467. begin
  468. pushedparasize:=0;
  469. { be found elsewhere }
  470. if assigned(p^.right) then
  471. secondcallparan(p^.left,pprocvardef(p^.right^.resulttype)^.para1,
  472. (pocall_leftright in p^.procdefinition^.proccalloptions))
  473. else
  474. secondcallparan(p^.left,p^.procdefinition^.para1,
  475. (pocall_leftright in p^.procdefinition^.proccalloptions));
  476. end;
  477. params:=p^.left;
  478. p^.left:=nil;
  479. if ret_in_param(p^.resulttype) then
  480. begin
  481. emitpushreferenceaddr(exprasmlist,funcretref);
  482. inc(pushedparasize,4);
  483. end;
  484. { overloaded operator have no symtable }
  485. if (p^.right=nil) then
  486. begin
  487. { push self }
  488. if assigned(p^.symtable) and
  489. (p^.symtable^.symtabletype=withsymtable) then
  490. begin
  491. { dirty trick to avoid the secondcall below }
  492. p^.methodpointer:=genzeronode(callparan);
  493. p^.methodpointer^.location.loc:=LOC_REGISTER;
  494. p^.methodpointer^.location.register:=R_A5;
  495. { change dispose type !! }
  496. p^.disposetyp:=dt_mbleft_and_method;
  497. { make a reference }
  498. new(r);
  499. reset_reference(r^);
  500. r^.offset:=p^.symtable^.datasize;
  501. r^.base:=procinfo^.framepointer;
  502. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_L,r,R_A5)));
  503. end;
  504. { push self }
  505. if assigned(p^.symtable) and
  506. ((p^.symtable^.symtabletype=objectsymtable) or
  507. (p^.symtable^.symtabletype=withsymtable)) then
  508. begin
  509. if assigned(p^.methodpointer) then
  510. begin
  511. case p^.methodpointer^.treetype of
  512. typen :
  513. begin
  514. { direct call to inherited method }
  515. if po_abstractmethod in p^.procdefinition^.procoptions then
  516. begin
  517. CGMessage(cg_e_cant_call_abstract_method);
  518. goto dont_call;
  519. end;
  520. { generate no virtual call }
  521. no_virtual_call:=true;
  522. if (sp_static in p^.symtableprocentry^.symoptions) then
  523. begin
  524. { well lets put the VMT address directly into a5 }
  525. { it is kind of dirty but that is the simplest }
  526. { way to accept virtual static functions (PM) }
  527. loada5:=true;
  528. exprasmlist^.concat(new(paicpu,op_csymbol_reg(A_MOVE,S_L,
  529. newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,0),R_A5)));
  530. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_L,R_A5,R_SPPUSH)));
  531. end
  532. else
  533. { this is a member call, so A5 isn't modfied }
  534. loada5:=false;
  535. { a class destructor needs a flag }
  536. if pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  537. assigned(aktprocsym) and
  538. (aktprocsym^.definition^.proctypeoption=potype_destructor) then
  539. begin
  540. push_int(0);
  541. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_L,R_A5,R_SPPUSH)));
  542. end;
  543. if not(is_con_or_destructor and
  544. pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  545. assigned(aktprocsym) and
  546. (aktprocsym^.definition^.proctypeoption in [potype_constructor,potype_destructor])
  547. ) then
  548. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_L,R_A5,R_SPPUSH)));
  549. { if an inherited con- or destructor should be }
  550. { called in a con- or destructor then a warning }
  551. { will be made }
  552. { con- and destructors need a pointer to the vmt }
  553. if is_con_or_destructor and
  554. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class) and
  555. assigned(aktprocsym) then
  556. begin
  557. if not(aktprocsym^.definition^.proctypeoption in
  558. [potype_constructor,potype_destructor]) then
  559. CGMessage(cg_w_member_cd_call_from_method);
  560. end;
  561. { class destructors get there flag below }
  562. if is_con_or_destructor and
  563. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  564. assigned(aktprocsym) and
  565. (aktprocsym^.definition^.proctypeoption=potype_destructor)) then
  566. push_int(0);
  567. end;
  568. hnewn : begin
  569. { extended syntax of new }
  570. { A5 must be zero }
  571. exprasmlist^.concat(new(paicpu,op_const_reg(A_MOVE,S_L,0,R_A5)));
  572. emit_reg_reg(A_MOVE,S_L,R_A5, R_SPPUSH);
  573. { insert the vmt }
  574. exprasmlist^.concat(new(paicpu,op_csymbol(A_PEA,S_L,
  575. newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,0))));
  576. extended_new:=true;
  577. end;
  578. hdisposen : begin
  579. secondpass(p^.methodpointer);
  580. { destructor with extended syntax called from dispose }
  581. { hdisposen always deliver LOC_REFRENZ }
  582. exprasmlist^.concat(new(paicpu,op_ref_reg(A_LEA,S_L,
  583. newreference(p^.methodpointer^.location.reference),R_A5)));
  584. del_reference(p^.methodpointer^.location.reference);
  585. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_L,R_A5,R_SPPUSH)));
  586. exprasmlist^.concat(new(paicpu,op_csymbol(A_PEA,S_L,
  587. newcsymbol(pobjectdef
  588. (p^.methodpointer^.resulttype)^.vmt_mangledname,0))));
  589. end;
  590. else
  591. begin
  592. { call to a instance member }
  593. if (p^.symtable^.symtabletype<>withsymtable) then
  594. begin
  595. secondpass(p^.methodpointer);
  596. case p^.methodpointer^.location.loc of
  597. LOC_REGISTER :
  598. begin
  599. ungetregister32(p^.methodpointer^.location.register);
  600. emit_reg_reg(A_MOVE,S_L,p^.methodpointer^.location.register,R_A5);
  601. end;
  602. else
  603. begin
  604. if (p^.methodpointer^.resulttype^.deftype=objectdef) and
  605. pobjectdef(p^.methodpointer^.resulttype)^.is_class then
  606. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_L,
  607. newreference(p^.methodpointer^.location.reference),R_A5)))
  608. else
  609. Begin
  610. exprasmlist^.concat(new(paicpu,op_ref_reg(A_LEA,S_L,
  611. newreference(p^.methodpointer^.location.reference),R_A5)));
  612. end;
  613. del_reference(p^.methodpointer^.location.reference);
  614. end;
  615. end;
  616. end;
  617. { when calling a class method, we have to load ESI with the VMT !
  618. But, not for a class method via self }
  619. if not(po_containsself in p^.procdefinition^.procoptions) then
  620. begin
  621. if (po_classmethod in p^.procdefinition^.procoptions) and
  622. not(p^.methodpointer^.resulttype^.deftype=classrefdef) then
  623. begin
  624. { class method needs current VMT }
  625. new(r);
  626. reset_reference(r^);
  627. r^.base:=R_A5;
  628. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  629. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_L,r,R_A5)));
  630. end;
  631. { direct call to destructor: don't remove data! }
  632. if (p^.procdefinition^.proctypeoption=potype_destructor) and
  633. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  634. (pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  635. push_int(1);
  636. { direct call to class constructor, don't allocate memory }
  637. if (p^.procdefinition^.proctypeoption=potype_constructor) and
  638. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  639. (pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  640. push_int(0)
  641. else
  642. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_L,R_A5,R_SPPUSH)));
  643. if is_con_or_destructor then
  644. begin
  645. { classes don't get a VMT pointer pushed }
  646. if (p^.methodpointer^.resulttype^.deftype=objectdef) and
  647. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  648. begin
  649. if (p^.procdefinition^.proctypeoption=potype_constructor) then
  650. begin
  651. { it's no bad idea, to insert the VMT }
  652. exprasmlist^.concat(new(paicpu,op_csymbol(A_PEA,S_L,
  653. newcsymbol(pobjectdef(
  654. p^.methodpointer^.resulttype)^.vmt_mangledname,0))));
  655. end
  656. { destructors haven't to dispose the instance, if this is }
  657. { a direct call }
  658. else
  659. push_int(0);
  660. end;
  661. end;
  662. end;
  663. end;
  664. end;
  665. end
  666. else
  667. begin
  668. if (po_classmethod in p^.procdefinition^.procoptions) and
  669. not(
  670. assigned(aktprocsym) and
  671. (po_classmethod in aktprocsym^.definition^.procoptions)
  672. ) then
  673. begin
  674. { class method needs current VMT }
  675. new(r);
  676. reset_reference(r^);
  677. r^.base:=R_A5;
  678. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  679. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_L,r,R_A5)));
  680. end
  681. else
  682. begin
  683. { member call, A5 isn't modified }
  684. loada5:=false;
  685. end;
  686. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_L,R_A5,R_SPPUSH)));
  687. { but a con- or destructor here would probably almost }
  688. { always be placed wrong }
  689. if is_con_or_destructor then
  690. begin
  691. CGMessage(cg_w_member_cd_call_from_method);
  692. { not insert VMT pointer } { VMT-Zeiger nicht eintragen }
  693. push_int(0);
  694. end;
  695. end;
  696. end;
  697. { push base pointer ?}
  698. if (lexlevel>=normal_function_level) and assigned(pprocdef(p^.procdefinition)^.parast) and
  699. ((pprocdef(p^.procdefinition)^.parast^.symtablelevel)>normal_function_level) then
  700. begin
  701. { if we call a nested function in a method, we must }
  702. { push also SELF! }
  703. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  704. { access }
  705. {
  706. begin
  707. loadesi:=false;
  708. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_ESI)));
  709. end;
  710. }
  711. if lexlevel=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  712. begin
  713. new(r);
  714. reset_reference(r^);
  715. r^.offset:=procinfo^.framepointer_offset;
  716. r^.base:=procinfo^.framepointer;
  717. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_L,r,R_SPPUSH)))
  718. end
  719. { this is only true if the difference is one !!
  720. but it cannot be more !! }
  721. else if lexlevel=(pprocdef(p^.procdefinition)^.parast^.symtablelevel)-1 then
  722. begin
  723. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_L,procinfo^.framepointer,R_SPPUSH)))
  724. end
  725. else if lexlevel>(pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  726. begin
  727. hregister:=getaddressreg;
  728. new(r);
  729. reset_reference(r^);
  730. r^.offset:=procinfo^.framepointer_offset;
  731. r^.base:=procinfo^.framepointer;
  732. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_L,r,hregister)));
  733. for i:=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) to lexlevel-1 do
  734. begin
  735. new(r);
  736. reset_reference(r^);
  737. {we should get the correct frame_pointer_offset at each level
  738. how can we do this !!! }
  739. r^.offset:=procinfo^.framepointer_offset;
  740. r^.base:=hregister;
  741. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_L,r,hregister)));
  742. end;
  743. exprasmlist^.concat(new(paicpu,op_reg_reg(A_MOVE,S_L,hregister,R_SPPUSH)));
  744. ungetregister32(hregister);
  745. end
  746. else
  747. internalerror(25000);
  748. end;
  749. if (po_virtualmethod in p^.procdefinition^.procoptions) and
  750. not(no_virtual_call) then
  751. begin
  752. { static functions contain the vmt_address in ESI }
  753. { also class methods }
  754. if assigned(aktprocsym) then
  755. begin
  756. if (((sp_static in aktprocsym^.symoptions) or
  757. (po_classmethod in aktprocsym^.definition^.procoptions)) and
  758. ((p^.methodpointer=nil) or (p^.methodpointer^.treetype=typen)))
  759. or
  760. (po_staticmethod in p^.procdefinition^.procoptions) or
  761. (p^.procdefinition^.proctypeoption=potype_constructor) or
  762. { A5 is loaded earlier }
  763. (po_classmethod in p^.procdefinition^.procoptions) then
  764. begin
  765. new(r);
  766. reset_reference(r^);
  767. r^.base:=R_a5;
  768. end
  769. else
  770. begin
  771. new(r);
  772. reset_reference(r^);
  773. r^.base:=R_a5;
  774. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  775. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_L,r,R_a0)));
  776. new(r);
  777. reset_reference(r^);
  778. r^.base:=R_a0;
  779. end;
  780. end
  781. else
  782. begin
  783. new(r);
  784. reset_reference(r^);
  785. r^.base:=R_a5;
  786. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_L,r,R_a0)));
  787. new(r);
  788. reset_reference(r^);
  789. r^.base:=R_a0;
  790. end;
  791. if pprocdef(p^.procdefinition)^.extnumber=-1 then
  792. internalerror(1609991);
  793. r^.offset:=pprocdef(p^.procdefinition)^.extnumber*4+12;
  794. if (cs_check_range in aktlocalswitches) then
  795. begin
  796. { If the base is already A0, the no instruction will }
  797. { be emitted! }
  798. emit_reg_reg(A_MOVE,S_L,r^.base,R_A0);
  799. emitcall('FPC_CHECK_OBJECT',true);
  800. end;
  801. { This was wrong we must then load the address into the }
  802. { register a0 and/or a5 }
  803. { Because doing an indirect call with offset is NOT }
  804. { allowed on the m68k! }
  805. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_L,newreference(r^),R_A0)));
  806. { clear the reference }
  807. reset_reference(r^);
  808. r^.base := R_A0;
  809. exprasmlist^.concat(new(paicpu,op_ref(A_JSR,S_NO,r)));
  810. end
  811. else if pocall_palmossyscall in p^.procdefinition^.proccalloptions then
  812. begin
  813. exprasmlist^.concat(new(paicpu,op_const(A_TRAP,S_NO,15)));
  814. exprasmlist^.concat(new(pai_const,init_16bit(pprocdef(p^.procdefinition)^.extnumber)));
  815. end
  816. else
  817. emitcall(pprocdef(p^.procdefinition)^.mangledname,
  818. (p^.symtableproc^.symtabletype=unitsymtable) or
  819. ((p^.symtableproc^.symtabletype=objectsymtable) and
  820. (pobjectdef(p^.symtableproc^.defowner)^.owner^.symtabletype=unitsymtable))or
  821. ((p^.symtableproc^.symtabletype=withsymtable) and
  822. (pobjectdef(p^.symtableproc^.defowner)^.owner^.symtabletype=unitsymtable)));
  823. if (pocall_clearstack in p^.procdefinition^.proccalloptions) then
  824. begin
  825. if (pushedparasize > 0) and (pushedparasize < 9) then
  826. { restore the stack, to its initial value }
  827. exprasmlist^.concat(new(paicpu,op_const_reg(A_ADDQ,S_L,pushedparasize,R_SP)))
  828. else
  829. { restore the stack, to its initial value }
  830. exprasmlist^.concat(new(paicpu,op_const_reg(A_ADDA,S_L,pushedparasize,R_SP)));
  831. end;
  832. end
  833. else
  834. begin
  835. secondpass(p^.right);
  836. case p^.right^.location.loc of
  837. LOC_REGISTER,
  838. LOC_CREGISTER : begin
  839. if p^.right^.location.register in [R_D0..R_D7] then
  840. begin
  841. reg := getaddressreg;
  842. emit_reg_reg(A_MOVE,S_L,p^.right^.location.register,reg);
  843. new(ref);
  844. reset_reference(ref^);
  845. ref^.base := reg;
  846. exprasmlist^.concat(new(paicpu,op_ref(A_JSR,S_NO,ref)));
  847. ungetregister(reg);
  848. end
  849. else
  850. begin
  851. new(ref);
  852. reset_reference(ref^);
  853. ref^.base := p^.right^.location.register;
  854. exprasmlist^.concat(new(paicpu,op_ref(A_JSR,S_NO,ref)));
  855. end;
  856. ungetregister32(p^.right^.location.register);
  857. end
  858. else
  859. begin
  860. if assigned(p^.right^.location.reference.symbol) then
  861. { Here we have a symbolic name to the routine, so solve }
  862. { problem by loading the address first, and then emitting }
  863. { the call. }
  864. begin
  865. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_L,
  866. newreference(p^.right^.location.reference),R_A1)));
  867. new(ref);
  868. reset_reference(ref^);
  869. ref^.base := R_A1;
  870. exprasmlist^.concat(new(paicpu,op_ref(A_JSR,S_NO,ref)));
  871. end
  872. else
  873. begin
  874. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVE,S_L,
  875. newreference(p^.right^.location.reference),R_A1)));
  876. new(ref);
  877. reset_reference(ref^);
  878. ref^.base := R_A1;
  879. exprasmlist^.concat(new(paicpu,op_ref(A_JSR,S_NO,ref)));
  880. end;
  881. del_reference(p^.right^.location.reference);
  882. end;
  883. end;
  884. end;
  885. dont_call:
  886. pushedparasize:=oldpushedparasize;
  887. unused:=unusedregisters;
  888. { handle function results }
  889. if p^.resulttype<>pdef(voiddef) then
  890. begin
  891. { a contructor could be a function with boolean result }
  892. if (p^.right=nil) and
  893. (p^.procdefinition^.proctypeoption=potype_constructor) and
  894. { quick'n'dirty check if it is a class or an object }
  895. (p^.resulttype^.deftype=orddef) then
  896. begin
  897. p^.location.loc:=LOC_FLAGS;
  898. p^.location.resflags:=F_NE;
  899. if extended_new then
  900. begin
  901. {$ifdef test_dest_loc}
  902. if dest_loc_known and (dest_loc_tree=p) then
  903. mov_reg_to_dest(p,S_L,R_EAX)
  904. else
  905. {$endif test_dest_loc}
  906. hregister:=getregister32;
  907. emit_reg_reg(A_MOVE,S_L,R_D0,hregister);
  908. p^.location.register:=hregister;
  909. end;
  910. end
  911. { structed results are easy to handle.... }
  912. else if ret_in_param(p^.resulttype) then
  913. begin
  914. p^.location.loc:=LOC_MEM;
  915. stringdispose(p^.location.reference.symbol);
  916. p^.location.reference:=funcretref;
  917. end
  918. else
  919. begin
  920. if (p^.resulttype^.deftype in [orddef,enumdef]) then
  921. begin
  922. p^.location.loc:=LOC_REGISTER;
  923. case p^.resulttype^.size of
  924. 4 :
  925. begin
  926. hregister:=getregister32;
  927. emit_reg_reg(A_MOVE,S_L,R_D0,hregister);
  928. p^.location.register:=hregister;
  929. end;
  930. 1 :
  931. begin
  932. hregister:=getregister32;
  933. emit_reg_reg(A_MOVE,S_B,R_D0,hregister);
  934. p^.location.register:=hregister;
  935. end;
  936. 2:
  937. begin
  938. hregister:=getregister32;
  939. emit_reg_reg(A_MOVE,S_L,R_D0,hregister);
  940. p^.location.register:=hregister;
  941. end;
  942. else internalerror(7);
  943. end
  944. end
  945. else if (p^.resulttype^.deftype=floatdef) then
  946. case pfloatdef(p^.resulttype)^.typ of
  947. f32bit :
  948. begin
  949. p^.location.loc:=LOC_REGISTER;
  950. hregister:=getregister32;
  951. emit_reg_reg(A_MOVE,S_L,R_D0,hregister);
  952. p^.location.register:=hregister;
  953. end;
  954. s32real : Begin
  955. p^.location.loc:=LOC_FPU;
  956. hregister:=getregister32;
  957. emit_reg_reg(A_MOVE,S_L,R_D0,hregister);
  958. p^.location.fpureg:=hregister;
  959. end;
  960. s64comp,s64real,s80real: begin
  961. if cs_fp_emulation in aktmoduleswitches then
  962. begin
  963. p^.location.loc:=LOC_FPU;
  964. hregister:=getregister32;
  965. emit_reg_reg(A_MOVE,S_L,R_D0,hregister);
  966. p^.location.fpureg:=hregister;
  967. end
  968. else
  969. begin
  970. { TRUE FPU mode }
  971. p^.location.loc:=LOC_FPU;
  972. { on exit of function result in R_FP0 }
  973. p^.location.fpureg:=R_FP0;
  974. end;
  975. end;
  976. else
  977. begin
  978. p^.location.loc:=LOC_FPU;
  979. p^.location.fpureg:=R_FP0;
  980. end;
  981. end {end case }
  982. else
  983. begin
  984. p^.location.loc:=LOC_REGISTER;
  985. hregister:=getregister32;
  986. emit_reg_reg(A_MOVE,S_L,R_D0,hregister);
  987. p^.location.register:=hregister;
  988. end;
  989. end;
  990. end;
  991. { perhaps i/o check ? }
  992. if iolabel<>nil then
  993. begin
  994. exprasmlist^.concat(new(paicpu,op_csymbol(A_PEA,S_L,newcsymbol(iolabel^.name,0))));
  995. emitcall('FPC_IOCHECK',true);
  996. end;
  997. { restore registers }
  998. popusedregisters(pushed);
  999. { at last, restore instance pointer (SELF) }
  1000. if loada5 then
  1001. maybe_loada5;
  1002. pp:=params;
  1003. while assigned(pp) do
  1004. begin
  1005. if assigned(pp^.left) then
  1006. if (pp^.left^.location.loc=LOC_REFERENCE) or
  1007. (pp^.left^.location.loc=LOC_MEM) then
  1008. ungetiftemp(pp^.left^.location.reference);
  1009. pp:=pp^.right;
  1010. end;
  1011. disposetree(params);
  1012. end;
  1013. {*****************************************************************************
  1014. SecondProcInlineN
  1015. *****************************************************************************}
  1016. procedure secondprocinline(var p : ptree);
  1017. begin
  1018. InternalError(132421);
  1019. end;
  1020. end.
  1021. {
  1022. $Log$
  1023. Revision 1.2 2000-07-13 11:32:36 michael
  1024. + removed logs
  1025. }