cga68k.pas 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl, Carl Eric Codere
  4. This unit generates 68000 (or better) assembler from the parse tree
  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 cga68k;
  19. interface
  20. uses
  21. objects,cobjects,verbose,systems,globals,tree,symtable,types,strings,
  22. pass_1,hcodegen,aasm,m68k,tgen68k,files,gdb;
  23. procedure emitl(op : tasmop;var l : plabel);
  24. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  25. procedure emitcall(const routine:string;add_to_externals : boolean);
  26. procedure emitloadord2reg(location:Tlocation;orddef:Porddef;
  27. destreg:Tregister;delloc:boolean);
  28. { produces jumps to true respectively false labels using boolean expressions }
  29. procedure maketojumpbool(p : ptree);
  30. procedure emitoverflowcheck(p: ptree);
  31. procedure push_int(l : longint);
  32. function maybe_push(needed : byte;p : ptree) : boolean;
  33. procedure restore(p : ptree);
  34. procedure emit_push_mem(const ref : treference);
  35. procedure emitpushreferenceaddr(const ref : treference);
  36. procedure swaptree(p: ptree);
  37. procedure copystring(const dref,sref : treference;len : byte);
  38. procedure concatcopy(source,dest : treference;size : longint;delsource : boolean);
  39. { see implementation }
  40. procedure maybe_loada5;
  41. procedure emit_bounds_check(hp: treference; index: tregister);
  42. procedure loadstring(p:ptree);
  43. procedure floatload(t : tfloattype;const ref : treference; var location:tlocation);
  44. { return a float op_size from a floatb type }
  45. { also does some error checking for problems }
  46. function getfloatsize(t: tfloattype): topsize;
  47. procedure floatstore(t : tfloattype;var location:tlocation; const ref:treference);
  48. { procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  49. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize); }
  50. procedure firstcomplex(p : ptree);
  51. procedure secondfuncret(var p : ptree);
  52. { initialize respectively terminates the code generator }
  53. { for a new module or procedure }
  54. procedure codegen_doneprocedure;
  55. procedure codegen_donemodule;
  56. procedure codegen_newmodule;
  57. procedure codegen_newprocedure;
  58. { generate entry code for a procedure.}
  59. procedure genentrycode(const proc_names:Tstringcontainer;make_global:boolean;
  60. stackframe:longint;
  61. var parasize:longint;var nostackframe:boolean);
  62. { generate the exit code for a procedure. }
  63. procedure genexitcode(parasize:longint;nostackframe:boolean);
  64. implementation
  65. {
  66. procedure genconstadd(size : topsize;l : longint;const str : string);
  67. begin
  68. if l=0 then
  69. else if l=1 then
  70. exprasmlist^.concat(new(pai68k,op_A_INC,size,str)
  71. else if l=-1 then
  72. exprasmlist^.concat(new(pai68k,op_A_INC,size,str)
  73. else
  74. exprasmlist^.concat(new(pai68k,op_ADD,size,'$'+tostr(l)+','+str);
  75. end;
  76. }
  77. procedure copystring(const dref,sref : treference;len : byte);
  78. var
  79. pushed : tpushed;
  80. begin
  81. pushusedregisters(pushed,$ffff);
  82. emitpushreferenceaddr(dref);
  83. emitpushreferenceaddr(sref);
  84. push_int(len);
  85. emitcall('STRCOPY',true);
  86. maybe_loada5;
  87. popusedregisters(pushed);
  88. end;
  89. procedure loadstring(p:ptree);
  90. begin
  91. case p^.right^.resulttype^.deftype of
  92. stringdef : begin
  93. { load a string ... }
  94. { here two possible choices: }
  95. { if it is a char, then simply }
  96. { load 0 length string }
  97. if (p^.right^.treetype=stringconstn) and
  98. (p^.right^.values^='') then
  99. exprasmlist^.concat(new(pai68k,op_const_ref(
  100. A_MOVE,S_B,0,newreference(p^.left^.location.reference))))
  101. else
  102. copystring(p^.left^.location.reference,p^.right^.location.reference,
  103. min(pstringdef(p^.right^.resulttype)^.len,pstringdef(p^.left^.resulttype)^.len));
  104. end;
  105. orddef : begin
  106. if p^.right^.treetype=ordconstn then
  107. begin
  108. exprasmlist^.concat(new(pai68k,op_const_ref(A_MOVE,S_W,p^.right^.value*256+1,
  109. newreference(p^.left^.location.reference))))
  110. end
  111. else
  112. begin
  113. { not so elegant (goes better with extra register }
  114. if (p^.right^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  115. begin
  116. exprasmlist^.concat(new(pai68k,op_reg_reg(
  117. A_MOVE,S_L,p^.right^.location.register,R_D0)));
  118. ungetregister32(p^.right^.location.register);
  119. end
  120. else
  121. begin
  122. exprasmlist^.concat(new(pai68k,op_ref_reg(
  123. A_MOVE,S_L,newreference(p^.right^.location.reference),R_D0)));
  124. del_reference(p^.right^.location.reference);
  125. end;
  126. if (opt_processors = MC68020) then
  127. { alignment is not a problem on the 68020 and higher processors }
  128. Begin
  129. { add length of string to word }
  130. exprasmlist^.concat(new(pai68k,op_const_reg(A_OR,S_W,$0100,R_D0)));
  131. { put back into mem ... }
  132. exprasmlist^.concat(new(pai68k,op_reg_ref(A_MOVE,S_W,R_D0,
  133. newreference(p^.left^.location.reference))));
  134. end
  135. else
  136. Begin
  137. { alignment can cause problems }
  138. { add length of string to ref }
  139. exprasmlist^.concat(new(pai68k,op_const_ref(A_MOVE,S_B,1,
  140. newreference(p^.left^.location.reference))));
  141. if abs(p^.left^.location.reference.offset) >= 1 then
  142. Begin
  143. { temporarily decrease offset }
  144. Inc(p^.left^.location.reference.offset);
  145. exprasmlist^.concat(new(pai68k,op_reg_ref(A_MOVE,S_B,R_D0,
  146. newreference(p^.left^.location.reference))));
  147. Dec(p^.left^.location.reference.offset);
  148. { restore offset }
  149. end
  150. else
  151. Begin
  152. Comment(V_Debug,'SecondChar2String() internal error.');
  153. internalerror(34);
  154. end;
  155. end;
  156. end;
  157. end;
  158. else
  159. Message(sym_e_type_mismatch);
  160. end;
  161. end;
  162. procedure restore(p : ptree);
  163. var
  164. hregister : tregister;
  165. begin
  166. hregister:=getregister32;
  167. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,R_SPPULL,hregister)));
  168. if (p^.location.loc=LOC_REGISTER) or (p^.location.loc=LOC_CREGISTER) then
  169. begin
  170. p^.location.register:=hregister;
  171. end
  172. else
  173. begin
  174. reset_reference(p^.location.reference);
  175. p^.location.reference.index:=hregister;
  176. set_location(p^.left^.location,p^.location);
  177. end;
  178. end;
  179. function maybe_push(needed : byte;p : ptree) : boolean;
  180. var
  181. pushed : boolean;
  182. {hregister : tregister; }
  183. reg: tregister;
  184. begin
  185. if needed>usablereg32 then
  186. begin
  187. if (p^.location.loc=LOC_REGISTER) or
  188. (p^.location.loc=LOC_CREGISTER) then
  189. begin
  190. pushed:=true;
  191. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,p^.location.register,R_SPPUSH)));
  192. ungetregister32(p^.location.register);
  193. end
  194. else if ((p^.location.loc=LOC_MEM) or
  195. (p^.location.loc=LOC_REFERENCE)
  196. ) and
  197. ((p^.location.reference.base<>R_NO) or
  198. (p^.location.reference.index<>R_NO)
  199. ) then
  200. begin
  201. del_reference(p^.location.reference);
  202. exprasmlist^.concat(new(pai68k,op_ref_reg(A_LEA,S_L,newreference(p^.location.reference),
  203. R_A0)));
  204. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,R_A0,R_SPPUSH)));
  205. pushed:=true;
  206. end
  207. else pushed:=false;
  208. end
  209. else pushed:=false;
  210. maybe_push:=pushed;
  211. end;
  212. { emit out of range check for arrays and sets}
  213. procedure emit_bounds_check(hp: treference; index: tregister);
  214. { index = index of array to check }
  215. { memory of range check information for array }
  216. var
  217. hl : plabel;
  218. begin
  219. if (opt_processors = MC68020) then
  220. begin
  221. exprasmlist^.concat(new(pai68k, op_ref_reg(A_CMP2,S_L,newreference(hp),index)));
  222. getlabel(hl);
  223. emitl(A_BCC, hl);
  224. exprasmlist^.concat(new(pai68k, op_const_reg(A_MOVE,S_L,201,R_D0)));
  225. emitcall('HALT_ERROR',true);
  226. emitl(A_LABEL, hl);
  227. end
  228. else
  229. begin
  230. exprasmlist^.concat(new(pai68k, op_ref_reg(A_LEA,S_L,newreference(hp), R_A1)));
  231. exprasmlist^.concat(new(pai68k, op_reg_reg(A_MOVE, S_L, index, R_D0)));
  232. emitcall('RE_BOUNDS_CHECK',true);
  233. end;
  234. end;
  235. function getfloatsize(t: tfloattype): topsize;
  236. begin
  237. case t of
  238. s32real: getfloatsize := S_S;
  239. s64real: getfloatsize := S_Q;
  240. s80real: getfloatsize := S_X;
  241. {$ifdef extdebug}
  242. else {else case }
  243. begin
  244. Comment(V_Debug,' getfloatsize() trying to get unknown size.');
  245. internalerror(12);
  246. end;
  247. {$endif}
  248. end;
  249. end;
  250. procedure emitl(op : tasmop;var l : plabel);
  251. begin
  252. if op=A_LABEL then
  253. exprasmlist^.concat(new(pai_label,init(l)))
  254. else
  255. exprasmlist^.concat(new(pai_labeled,init(op,l)))
  256. end;
  257. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  258. begin
  259. if (reg1 <> reg2) or (i <> A_MOVE) then
  260. exprasmlist^.concat(new(pai68k,op_reg_reg(i,s,reg1,reg2)));
  261. end;
  262. procedure emitcall(const routine:string;add_to_externals : boolean);
  263. begin
  264. exprasmlist^.concat(new(pai68k,op_csymbol(A_JSR,S_NO,newcsymbol(routine,0))));
  265. if assem_need_external_list and add_to_externals and
  266. not (cs_compilesystem in aktswitches) then
  267. concat_external(routine,EXT_NEAR);
  268. end;
  269. procedure maketojumpbool(p : ptree);
  270. begin
  271. if p^.error then
  272. exit;
  273. if (p^.resulttype^.deftype=orddef) and
  274. (porddef(p^.resulttype)^.typ=bool8bit) then
  275. begin
  276. if is_constboolnode(p) then
  277. begin
  278. if p^.value<>0 then
  279. emitl(A_JMP,truelabel)
  280. else emitl(A_JMP,falselabel);
  281. end
  282. else
  283. begin
  284. case p^.location.loc of
  285. LOC_CREGISTER,LOC_REGISTER : begin
  286. exprasmlist^.concat(new(pai68k,op_reg(A_TST,S_B,p^.location.register)));
  287. ungetregister32(p^.location.register);
  288. emitl(A_BNE,truelabel);
  289. emitl(A_JMP,falselabel);
  290. end;
  291. LOC_MEM,LOC_REFERENCE : begin
  292. exprasmlist^.concat(new(pai68k,op_ref(
  293. A_TST,S_B,newreference(p^.location.reference))));
  294. del_reference(p^.location.reference);
  295. emitl(A_BNE,truelabel);
  296. emitl(A_JMP,falselabel);
  297. end;
  298. LOC_FLAGS : begin
  299. emitl(flag_2_jmp[p^.location.resflags],truelabel);
  300. emitl(A_JMP,falselabel);
  301. end;
  302. end;
  303. end;
  304. end
  305. else
  306. Message(sym_e_type_mismatch);
  307. end;
  308. procedure emitoverflowcheck(p: ptree);
  309. var
  310. hl : plabel;
  311. begin
  312. if cs_check_overflow in aktswitches then
  313. begin
  314. getlabel(hl);
  315. if not ((p^.resulttype^.deftype=pointerdef) or
  316. ((p^.resulttype^.deftype=orddef) and
  317. (porddef(p^.resulttype)^.typ in [u16bit,u32bit,u8bit,uchar,bool8bit]))) then
  318. emitl(A_BVC,hl)
  319. else
  320. emitl(A_BCC,hl);
  321. emitcall('RE_OVERFLOW',true);
  322. emitl(A_LABEL,hl);
  323. end;
  324. end;
  325. procedure push_int(l : longint);
  326. begin
  327. if (l = 0) and (opt_processors = MC68020) then
  328. begin
  329. exprasmlist^.concat(new(pai68k,op_reg(A_CLR,S_L,R_D6)));
  330. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,
  331. R_D6, R_SPPUSH)));
  332. end
  333. else
  334. if not(cs_littlesize in aktswitches) and (l >= -128) and (l <= 127) then
  335. begin
  336. exprasmlist^.concat(new(pai68k,op_const_reg(A_MOVEQ,S_L,l,R_D6)));
  337. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,R_D6,R_SPPUSH)));
  338. end
  339. else
  340. exprasmlist^.concat(new(pai68k,op_const_reg(A_MOVE,S_L,l,R_SPPUSH)));
  341. end;
  342. procedure emit_push_mem(const ref : treference);
  343. begin
  344. if ref.isintvalue then
  345. push_int(ref.offset)
  346. else
  347. exprasmlist^.concat(new(pai68k,op_ref(A_PEA,S_L,newreference(ref))));
  348. end;
  349. { USES REGISTER R_A1 }
  350. procedure emitpushreferenceaddr(const ref : treference);
  351. begin
  352. if ref.isintvalue then
  353. push_int(ref.offset)
  354. else
  355. begin
  356. if (ref.base=R_NO) and (ref.index=R_NO) then
  357. exprasmlist^.concat(new(pai68k,op_ref(A_PEA,S_L,
  358. newreference(ref))))
  359. else if (ref.base=R_NO) and (ref.index<>R_NO) and
  360. (ref.offset=0) and (ref.scalefactor=0) and (ref.symbol=nil) then
  361. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,
  362. ref.index,R_SPPUSH)))
  363. else if (ref.base<>R_NO) and (ref.index=R_NO) and
  364. (ref.offset=0) and (ref.symbol=nil) then
  365. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,ref.base,R_SPPUSH)))
  366. else
  367. begin
  368. exprasmlist^.concat(new(pai68k,op_ref_reg(A_LEA,S_L,newreference(ref),R_A1)));
  369. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,R_A1,R_SPPUSH)));
  370. end;
  371. end;
  372. end;
  373. procedure swaptree(p:Ptree);
  374. var swapp:Ptree;
  375. begin
  376. swapp:=p^.right;
  377. p^.right:=p^.left;
  378. p^.left:=swapp;
  379. p^.swaped:=not(p^.swaped);
  380. end;
  381. procedure genentrycode(const proc_names:Tstringcontainer;make_global:boolean;
  382. stackframe:longint;
  383. var parasize:longint;var nostackframe:boolean);
  384. {Generates the entry code for a procedure.}
  385. var hs:string;
  386. hp:Pused_unit;
  387. unitinits:taasmoutput;
  388. {$ifdef GDB}
  389. oldaktprocname : string;
  390. stab_function_name:Pai_stab_function_name;
  391. {$endif GDB}
  392. begin
  393. if (aktprocsym^.definition^.options and poproginit<>0) then
  394. begin
  395. {Init the stack checking.}
  396. if (cs_check_stack in aktswitches) and
  397. (target_info.target=target_linux) then
  398. begin
  399. procinfo.aktentrycode^.insert(new(pai68k,
  400. op_csymbol(A_JSR,S_NO,newcsymbol('INIT_STACK_CHECK',0))));
  401. end;
  402. unitinits.init;
  403. {Call the unit init procedures.}
  404. hp:=pused_unit(usedunits.first);
  405. while assigned(hp) do
  406. begin
  407. { call the unit init code and make it external }
  408. if (hp^.u^.flags and uf_init)<>0 then
  409. begin
  410. unitinits.concat(new(pai68k,op_csymbol(A_JSR,S_NO,newcsymbol('INIT$$'+hp^.u^.unitname^,0))));
  411. externals^.concat(new(pai_external,init('INIT$$'+hp^.u^.unitname^,EXT_NEAR)));
  412. end;
  413. hp:=pused_unit(hp^.next);
  414. end;
  415. procinfo.aktentrycode^.insertlist(@unitinits);
  416. unitinits.done;
  417. end;
  418. { a constructor needs a help procedure }
  419. if (aktprocsym^.definition^.options and poconstructor)<>0 then
  420. begin
  421. if procinfo._class^.isclass then
  422. begin
  423. procinfo.aktentrycode^.insert(new(pai_labeled,init(A_BEQ,quickexitlabel)));
  424. procinfo.aktentrycode^.insert(new(pai68k,op_csymbol(A_JSR,S_NO,
  425. newcsymbol('NEW_CLASS',0))));
  426. concat_external('NEW_CLASS',EXT_NEAR);
  427. end
  428. else
  429. begin
  430. procinfo.aktentrycode^.insert(new(pai_labeled,init(A_BEQ,quickexitlabel)));
  431. procinfo.aktentrycode^.insert(new(pai68k,op_csymbol(A_JSR,S_NO,
  432. newcsymbol('HELP_CONSTRUCTOR',0))));
  433. concat_external('HELP_CONSTRUCTOR',EXT_NEAR);
  434. end;
  435. end;
  436. { don't load ESI, does the caller }
  437. { omit stack frame ? }
  438. if procinfo.framepointer=stack_pointer then
  439. begin
  440. Message(cg_d_stackframe_omited);
  441. nostackframe:=true;
  442. if (aktprocsym^.definition^.options and (pounitinit or poproginit)<>0) then
  443. parasize:=0
  444. else
  445. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo.call_offset;
  446. end
  447. else
  448. begin
  449. if (aktprocsym^.definition^.options and (pounitinit or poproginit)<>0) then
  450. parasize:=0
  451. else
  452. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo.call_offset-8;
  453. nostackframe:=false;
  454. if stackframe<>0 then
  455. begin
  456. if cs_littlesize in aktswitches then
  457. begin
  458. if (cs_check_stack in aktswitches) and
  459. (target_info.target<>target_linux) then
  460. begin
  461. procinfo.aktentrycode^.insert(new(pai68k,
  462. op_csymbol(A_JSR,S_NO,newcsymbol('STACKCHECK',0))));
  463. procinfo.aktentrycode^.insert(new(pai68k,op_const_reg(A_MOVE,S_L,stackframe,R_SPPUSH)));
  464. end;
  465. { to allocate stack space }
  466. { here we allocate space using link signed 16-bit version }
  467. { -ve offset to allocate stack space! }
  468. if (stackframe > -32767) and (stackframe < 32769) then
  469. procinfo.aktentrycode^.insert(new(pai68k,op_reg_const(A_LINK,S_W,R_A6,-stackframe)))
  470. else
  471. Message(cg_e_stacklimit_in_local_routine);
  472. end
  473. else
  474. begin
  475. { Not to complicate the code generator too much, and since some }
  476. { of the systems only support this format, the stackframe cannot }
  477. { exceed 32K in size. }
  478. if (stackframe > -32767) and (stackframe < 32769) then
  479. begin
  480. procinfo.aktentrycode^.insert(new(pai68k,op_const_reg(A_SUB,S_L,stackframe,R_SP)));
  481. if (cs_check_stack in aktswitches) then
  482. begin
  483. procinfo.aktentrycode^.insert(new(pai68k,
  484. op_csymbol(A_JSR,S_NO,newcsymbol('STACKCHECK',0))));
  485. procinfo.aktentrycode^.insert(new(pai68k,op_const_reg(A_MOVE,S_L,
  486. stackframe,R_SPPUSH)));
  487. concat_external('STACKCHECK',EXT_NEAR);
  488. end;
  489. procinfo.aktentrycode^.insert(new(pai68k,op_reg_reg(A_MOVE,S_L,R_SP,R_A6)));
  490. procinfo.aktentrycode^.insert(new(pai68k,op_reg_reg(A_MOVE,S_L,R_A6,R_SPPUSH)));
  491. end
  492. else
  493. Message(cg_e_stacklimit_in_local_routine);
  494. end;
  495. end {endif stackframe<>0 }
  496. else
  497. begin
  498. procinfo.aktentrycode^.insert(new(pai68k,op_reg_reg(A_MOVE,S_L,R_SP,R_A6)));
  499. procinfo.aktentrycode^.insert(new(pai68k,op_reg_reg(A_MOVE,S_L,R_A6,R_SPPUSH)));
  500. end;
  501. end;
  502. if (aktprocsym^.definition^.options and pointerrupt)<>0 then
  503. generate_interrupt_stackframe_entry;
  504. {proc_names.insert(aktprocsym^.definition^.mangledname);}
  505. if (aktprocsym^.definition^.owner^.symtabletype=globalsymtable) or
  506. ((procinfo._class<>nil) and (procinfo._class^.owner^.
  507. symtabletype=globalsymtable)) then
  508. make_global:=true;
  509. hs:=proc_names.get;
  510. {$IfDef GDB}
  511. if (cs_debuginfo in aktswitches) and
  512. target_info.use_function_relative_addresses then
  513. stab_function_name := new(pai_stab_function_name,init(strpnew(hs)));
  514. oldaktprocname:=aktprocsym^.name;
  515. {$EndIf GDB}
  516. while hs<>'' do
  517. begin
  518. if make_global then
  519. procinfo.aktentrycode^.insert(new(pai_symbol,init_global(hs)))
  520. else
  521. procinfo.aktentrycode^.insert(new(pai_symbol,init(hs)));
  522. {$ifdef GDB}
  523. if (cs_debuginfo in aktswitches) and
  524. target_info.use_function_relative_addresses then
  525. begin
  526. procinfo.aktentrycode^.insert(new(pai_stab_function_name,init(strpnew(hs))));
  527. { This is not a nice solution to save the name, change it and restore when done }
  528. aktprocsym^.setname(hs);
  529. procinfo.aktentrycode^.insert(new(pai_stabs,init(aktprocsym^.stabstring)));
  530. end;
  531. {$endif GDB}
  532. hs:=proc_names.get;
  533. end;
  534. {$ifdef GDB}
  535. aktprocsym^.setname(oldaktprocname);
  536. if (cs_debuginfo in aktswitches) then
  537. begin
  538. if target_info.use_function_relative_addresses then
  539. procinfo.aktentrycode^.insert(stab_function_name);
  540. if make_global or ((procinfo.flags and pi_is_global) <> 0) then
  541. aktprocsym^.is_global := True;
  542. {This is dead code! Because lexlevel is increased at the
  543. start of compile_proc_body it can never be zero.}
  544. { if (lexlevel > 0) and (oldprocsym^.definition^.localst^.name = nil) then
  545. if oldprocsym^.owner^.symtabletype = objectsymtable then
  546. oldprocsym^.definition^.localst^.name := stringdup(oldprocsym^.owner^.name^+'_'+oldprocsym^.name)
  547. else
  548. oldprocsym^.definition^.localst^.name := stringdup(oldprocsym^.name);}
  549. aktprocsym^.isstabwritten:=true;
  550. end;
  551. {$endif GDB}
  552. { Alignment required for Motorola }
  553. procinfo.aktentrycode^.insert(new(pai_align,init(2)));
  554. {$ifdef extdebug}
  555. procinfo.aktentrycode^.insert(new(pai_direct,init(strpnew(target_info.newline))));
  556. {$endif extdebug}
  557. end;
  558. {Generate the exit code for a procedure.}
  559. procedure genexitcode(parasize:longint;nostackframe:boolean);
  560. var hr:Preference; {This is for function results.}
  561. op:Tasmop;
  562. s:Topsize;
  563. begin
  564. { !!!! insert there automatic destructors }
  565. procinfo.aktexitcode^.insert(new(pai_label,init(aktexitlabel)));
  566. { call the destructor help procedure }
  567. if (aktprocsym^.definition^.options and podestructor)<>0 then
  568. begin
  569. if procinfo._class^.isclass then
  570. begin
  571. procinfo.aktexitcode^.insert(new(pai68k,op_csymbol(A_JSR,S_NO,
  572. newcsymbol('DISPOSE_CLASS',0))));
  573. concat_external('DISPOSE_CLASS',EXT_NEAR);
  574. end
  575. else
  576. begin
  577. procinfo.aktexitcode^.insert(new(pai68k,op_csymbol(A_JSR,S_NO,
  578. newcsymbol('HELP_DESTRUCTOR',0))));
  579. concat_external('HELP_DESTRUCTOR',EXT_NEAR);
  580. end;
  581. end;
  582. { call __EXIT for main program }
  583. { ????????? }
  584. if (aktprocsym^.definition^.options and poproginit)<>0 then
  585. begin
  586. procinfo.aktexitcode^.concat(new(pai68k,op_csymbol(A_JSR,S_NO,newcsymbol('__EXIT',0))));
  587. externals^.concat(new(pai_external,init('__EXIT',EXT_NEAR)));
  588. end;
  589. { handle return value }
  590. if (aktprocsym^.definition^.options and poassembler)=0 then
  591. if (aktprocsym^.definition^.options and poconstructor)=0 then
  592. begin
  593. if procinfo.retdef<>pdef(voiddef) then
  594. begin
  595. if not procinfo.funcret_is_valid then
  596. Message(sym_w_function_result_not_set);
  597. new(hr);
  598. reset_reference(hr^);
  599. hr^.offset:=procinfo.retoffset;
  600. hr^.base:=procinfo.framepointer;
  601. if (procinfo.retdef^.deftype=orddef) then
  602. begin
  603. case porddef(procinfo.retdef)^.typ of
  604. s32bit,u32bit :
  605. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hr,R_D0)));
  606. u8bit,s8bit,uchar,bool8bit :
  607. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,S_B,hr,R_D0)));
  608. s16bit,u16bit :
  609. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,S_W,hr,R_D0)));
  610. end;
  611. end
  612. else
  613. if (procinfo.retdef^.deftype in [pointerdef,enumdef,procvardef]) or
  614. ((procinfo.retdef^.deftype=setdef) and
  615. (psetdef(procinfo.retdef)^.settype=smallset)) then
  616. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hr,R_D0)))
  617. else
  618. if (procinfo.retdef^.deftype=floatdef) then
  619. begin
  620. if pfloatdef(procinfo.retdef)^.typ=f32bit then
  621. begin
  622. { Isnt this missing ? }
  623. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hr,R_D0)));
  624. end
  625. else
  626. begin
  627. { how the return value is handled }
  628. { if in FPU mode, return in FP0 }
  629. if (pfloatdef(procinfo.retdef)^.typ = s32real)
  630. and (cs_fp_emulation in aktswitches) then
  631. begin
  632. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,
  633. S_L,hr,R_D0)))
  634. end
  635. else
  636. begin
  637. if cs_fp_emulation in aktswitches then
  638. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,
  639. S_L,hr,R_D0)))
  640. else
  641. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_FMOVE,
  642. getfloatsize(pfloatdef(procinfo.retdef)^.typ),hr,R_FP0)));
  643. end;
  644. end;
  645. end
  646. else
  647. dispose(hr);
  648. end
  649. end
  650. else
  651. begin
  652. { successful constructor deletes the zero flag }
  653. { and returns self in accumulator }
  654. procinfo.aktexitcode^.concat(new(pai_label,init(quickexitlabel)));
  655. { eax must be set to zero if the allocation failed !!! }
  656. procinfo.aktexitcode^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,R_A5,R_D0)));
  657. { faster then OR on mc68000/mc68020 }
  658. procinfo.aktexitcode^.concat(new(pai68k,op_reg(A_TST,S_L,R_D0)));
  659. end;
  660. procinfo.aktexitcode^.concat(new(pai_label,init(aktexit2label)));
  661. if not(nostackframe) then
  662. procinfo.aktexitcode^.concat(new(pai68k,op_reg(A_UNLK,S_NO,R_A6)));
  663. { at last, the return is generated }
  664. if (aktprocsym^.definition^.options and pointerrupt)<>0 then
  665. generate_interrupt_stackframe_exit
  666. else
  667. if (parasize=0) or ((aktprocsym^.definition^.options and poclearstack)<>0)
  668. then
  669. {Routines with the poclearstack flag set use only a ret.}
  670. { also routines with parasize=0 }
  671. procinfo.aktexitcode^.concat(new(pai68k,op_none(A_RTS,S_NO)))
  672. else
  673. { return with immediate size possible here }
  674. { signed! }
  675. if (opt_processors = MC68020) and (parasize < $7FFF) then
  676. procinfo.aktexitcode^.concat(new(pai68k,op_const(
  677. A_RTD,S_NO,parasize)))
  678. { manually restore the stack }
  679. else
  680. begin
  681. { We must pull the PC Counter from the stack, before }
  682. { restoring the stack pointer, otherwise the PC would }
  683. { point to nowhere! }
  684. { save the PC counter (pop it from the stack) }
  685. procinfo.aktexitcode^.concat(new(pai68k,op_reg_reg(
  686. A_MOVE,S_L,R_SPPULL,R_A0)));
  687. { can we do a quick addition ... }
  688. if (parasize > 0) and (parasize < 9) then
  689. procinfo.aktexitcode^.concat(new(pai68k,op_const_reg(
  690. A_ADD,S_L,parasize,R_SP)))
  691. else { nope ... }
  692. procinfo.aktexitcode^.concat(new(pai68k,op_const_reg(
  693. A_ADD,S_L,parasize,R_SP)));
  694. { endif }
  695. { restore the PC counter (push it on the stack) }
  696. procinfo.aktexitcode^.concat(new(pai68k,op_reg_reg(
  697. A_MOVE,S_L,R_A0,R_SPPUSH)));
  698. procinfo.aktexitcode^.concat(new(pai68k,op_none(
  699. A_RTS,S_NO)))
  700. end;
  701. {$ifdef GDB}
  702. if cs_debuginfo in aktswitches then
  703. begin
  704. aktprocsym^.concatstabto(procinfo.aktexitcode);
  705. if assigned(procinfo._class) then
  706. procinfo.aktexitcode^.concat(new(pai_stabs,init(strpnew(
  707. '"$t:v'+procinfo._class^.numberstring+'",'+
  708. tostr(N_PSYM)+',0,0,'+tostr(procinfo.esi_offset)))));
  709. if (porddef(aktprocsym^.definition^.retdef) <> voiddef) then
  710. procinfo.aktexitcode^.concat(new(pai_stabs,init(strpnew(
  711. '"'+aktprocsym^.name+':X'+aktprocsym^.definition^.retdef^.numberstring+'",'+
  712. tostr(N_PSYM)+',0,0,'+tostr(procinfo.retoffset)))));
  713. procinfo.aktexitcode^.concat(new(pai_stabn,init(strpnew('192,0,0,'
  714. +aktprocsym^.definition^.mangledname))));
  715. procinfo.aktexitcode^.concat(new(pai_stabn,init(strpnew('224,0,0,'
  716. +lab2str(aktexit2label)))));
  717. end;
  718. {$endif * GDB *}
  719. end;
  720. { USES REGISTERS R_A0 AND R_A1 }
  721. procedure concatcopy(source,dest : treference;size : longint;delsource : boolean);
  722. var
  723. ecxpushed : boolean;
  724. helpsize : longint;
  725. i : byte;
  726. reg8,reg32 : tregister;
  727. swap : boolean;
  728. hregister : tregister;
  729. iregister : tregister;
  730. jregister : tregister;
  731. hp1 : treference;
  732. hp2 : treference;
  733. hl : plabel;
  734. begin
  735. hregister := getregister32;
  736. if delsource then
  737. del_reference(source);
  738. { from 12 bytes movs is being used }
  739. if (size<=8) or (not(cs_littlesize in aktswitches) and (size<=12)) then
  740. begin
  741. helpsize:=size div 4;
  742. { move a dword x times }
  743. for i:=1 to helpsize do
  744. begin
  745. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,newreference(source),hregister)));
  746. exprasmlist^.concat(new(pai68k,op_reg_ref(A_MOVE,S_L,hregister,newreference(dest))));
  747. inc(source.offset,4);
  748. inc(dest.offset,4);
  749. dec(size,4);
  750. end;
  751. { move a word }
  752. if size>1 then
  753. begin
  754. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_W,newreference(source),hregister)));
  755. exprasmlist^.concat(new(pai68k,op_reg_ref(A_MOVE,S_W,hregister,newreference(dest))));
  756. inc(source.offset,2);
  757. inc(dest.offset,2);
  758. dec(size,2);
  759. end;
  760. { move a single byte }
  761. if size>0 then
  762. begin
  763. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_B,newreference(source),hregister)));
  764. exprasmlist^.concat(new(pai68k,op_reg_ref(A_MOVE,S_B,hregister,newreference(dest))));
  765. end
  766. end
  767. else
  768. begin
  769. if (usableaddress > 1) then
  770. begin
  771. iregister := getaddressreg;
  772. jregister := getaddressreg;
  773. end
  774. else
  775. if (usableaddress = 1) then
  776. begin
  777. iregister := getaddressreg;
  778. jregister := R_A1;
  779. end
  780. else
  781. begin
  782. iregister := R_A0;
  783. jregister := R_A1;
  784. end;
  785. { reference for move (An)+,(An)+ }
  786. reset_reference(hp1);
  787. hp1.base := iregister; { source register }
  788. hp1.direction := dir_inc;
  789. reset_reference(hp2);
  790. hp2.base := jregister;
  791. hp1.direction := dir_inc;
  792. { iregister = source }
  793. { jregister = destination }
  794. exprasmlist^.concat(new(pai68k,op_ref_reg(A_LEA,S_L,newreference(source),iregister)));
  795. exprasmlist^.concat(new(pai68k,op_ref_reg(A_LEA,S_L,newreference(dest),jregister)));
  796. { double word move }
  797. helpsize := size - size mod 4;
  798. size := size mod 4;
  799. exprasmlist^.concat(new(pai68k,op_const_reg(A_MOVE,S_L,helpsize div 4,hregister)));
  800. getlabel(hl);
  801. emitl(A_LABEL,hl);
  802. exprasmlist^.concat(new(pai68k,op_ref_ref(A_MOVE,S_L,newreference(hp1),newreference(hp2))));
  803. exprasmlist^.concat(new(pai68k,op_const_reg(A_SUBQ,S_L,4,hregister)));
  804. emitl(A_BNE,hl);
  805. if size > 1 then
  806. begin
  807. dec(size,2);
  808. exprasmlist^.concat(new(pai68k,op_ref_ref(A_MOVE,S_W,newreference(hp1), newreference(hp2))));
  809. end;
  810. if size = 1 then
  811. exprasmlist^.concat(new(pai68k,op_ref_ref(A_MOVE,S_B,newreference(hp1), newreference(hp2))));
  812. { restore the registers that we have just used olny if they are used! }
  813. if jregister = R_A1 then
  814. hp2.base := R_NO;
  815. if iregister = R_A0 then
  816. hp1.base := R_NO;
  817. del_reference(hp1);
  818. del_reference(hp2);
  819. end;
  820. { loading SELF-reference again }
  821. maybe_loada5;
  822. if delsource then
  823. ungetiftemp(source);
  824. ungetregister32(hregister);
  825. end;
  826. procedure emitloadord2reg(location:Tlocation;orddef:Porddef;
  827. destreg:Tregister;delloc:boolean);
  828. {A lot smaller and less bug sensitive than the original unfolded loads.}
  829. var tai:pai68k;
  830. r:Preference;
  831. begin
  832. case location.loc of
  833. LOC_REGISTER,LOC_CREGISTER:
  834. begin
  835. case orddef^.typ of
  836. u8bit: begin
  837. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,location.register,destreg)));
  838. exprasmlist^.concat(new(pai68k,op_const_reg(A_ANDI,S_L,$FF,destreg)));
  839. end;
  840. s8bit: begin
  841. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_B,location.register,destreg)));
  842. if (opt_processors <> MC68020) then
  843. begin
  844. { byte to word }
  845. exprasmlist^.concat(new(pai68k,op_reg(A_EXT,S_W,destreg)));
  846. { word to long }
  847. exprasmlist^.concat(new(pai68k,op_reg(A_EXT,S_L,destreg)));
  848. end
  849. else { 68020+ and later only }
  850. exprasmlist^.concat(new(pai68k,op_reg(A_EXTB,S_L,destreg)));
  851. end;
  852. u16bit: begin
  853. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_W,location.register,destreg)));
  854. exprasmlist^.concat(new(pai68k,op_const_reg(A_ANDI,S_L,$FFFF,destreg)));
  855. end;
  856. s16bit: begin
  857. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_W,location.register,destreg)));
  858. { word to long }
  859. exprasmlist^.concat(new(pai68k,op_reg(A_EXT,S_L,destreg)));
  860. end;
  861. u32bit:
  862. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,location.register,destreg)));
  863. s32bit:
  864. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,location.register,destreg)));
  865. end;
  866. if delloc then
  867. ungetregister(location.register);
  868. end;
  869. LOC_REFERENCE:
  870. begin
  871. r:=newreference(location.reference);
  872. case orddef^.typ of
  873. u8bit: begin
  874. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,r,destreg)));
  875. exprasmlist^.concat(new(pai68k,op_const_reg(A_ANDI,S_L,$FF,destreg)));
  876. end;
  877. s8bit: begin
  878. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_B,r,destreg)));
  879. if (opt_processors <> MC68020) then
  880. begin
  881. { byte to word }
  882. exprasmlist^.concat(new(pai68k,op_reg(A_EXT,S_W,destreg)));
  883. { word to long }
  884. exprasmlist^.concat(new(pai68k,op_reg(A_EXT,S_L,destreg)));
  885. end
  886. else { 68020+ and later only }
  887. exprasmlist^.concat(new(pai68k,op_reg(A_EXTB,S_L,destreg)));
  888. end;
  889. u16bit: begin
  890. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_W,r,destreg)));
  891. exprasmlist^.concat(new(pai68k,op_const_reg(A_ANDI,S_L,$ffff,destreg)));
  892. end;
  893. s16bit: begin
  894. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_W,r,destreg)));
  895. { word to long }
  896. exprasmlist^.concat(new(pai68k,op_reg(A_EXT,S_L,destreg)));
  897. end;
  898. u32bit:
  899. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,r,destreg)));
  900. s32bit:
  901. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,r,destreg)));
  902. end;
  903. if delloc then
  904. del_reference(location.reference);
  905. end
  906. else
  907. internalerror(6);
  908. end;
  909. end;
  910. { if necessary A5 is reloaded after a call}
  911. procedure maybe_loada5;
  912. var
  913. hp : preference;
  914. p : pprocinfo;
  915. i : longint;
  916. begin
  917. if assigned(procinfo._class) then
  918. begin
  919. if lexlevel>2 then
  920. begin
  921. new(hp);
  922. reset_reference(hp^);
  923. hp^.offset:=procinfo.framepointer_offset;
  924. hp^.base:=procinfo.framepointer;
  925. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hp,R_A5)));
  926. p:=procinfo.parent;
  927. for i:=3 to lexlevel-1 do
  928. begin
  929. new(hp);
  930. reset_reference(hp^);
  931. hp^.offset:=p^.framepointer_offset;
  932. hp^.base:=R_A5;
  933. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hp,R_A5)));
  934. p:=p^.parent;
  935. end;
  936. new(hp);
  937. reset_reference(hp^);
  938. hp^.offset:=p^.ESI_offset;
  939. hp^.base:=R_A5;
  940. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hp,R_A5)));
  941. end
  942. else
  943. begin
  944. new(hp);
  945. reset_reference(hp^);
  946. hp^.offset:=procinfo.ESI_offset;
  947. hp^.base:=procinfo.framepointer;
  948. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hp,R_A5)));
  949. end;
  950. end;
  951. end;
  952. (***********************************************************************)
  953. (* PROCEDURE FLOATLOAD *)
  954. (* Description: This routine is to be called each time a location *)
  955. (* must be set to LOC_FPU and a value loaded into a FPU register. *)
  956. (* *)
  957. (* Remark: The routine sets up the register field of LOC_FPU correctly*)
  958. (***********************************************************************)
  959. procedure floatload(t : tfloattype;const ref : treference; var location:tlocation);
  960. var
  961. op : tasmop;
  962. s : topsize;
  963. begin
  964. { no emulation }
  965. case t of
  966. s32real : s := S_S;
  967. s64real : s := S_Q;
  968. s80real : s := S_X;
  969. else
  970. begin
  971. Message(cg_f_unknown_float_type);
  972. end;
  973. end; { end case }
  974. location.loc := LOC_FPU;
  975. if not ((cs_fp_emulation) in aktswitches) then
  976. begin
  977. location.fpureg := getfloatreg;
  978. exprasmlist^.concat(new(pai68k,op_ref_reg(A_FMOVE,s,newreference(ref),location.fpureg)))
  979. end
  980. else
  981. { handle emulation }
  982. begin
  983. if t = s32real then
  984. begin
  985. location.fpureg := getregister32;
  986. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,newreference(ref),location.fpureg)))
  987. end
  988. else
  989. { other floating types are not supported in emulation mode }
  990. Message(sym_e_type_id_not_defined);
  991. end;
  992. end;
  993. { procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  994. begin
  995. case t of
  996. s32real : begin
  997. op:=A_FSTP;
  998. s:=S_S;
  999. end;
  1000. s64real : begin
  1001. op:=A_FSTP;
  1002. s:=S_L;
  1003. end;
  1004. s80real : begin
  1005. op:=A_FSTP;
  1006. s:=S_Q;
  1007. end;
  1008. s64bit : begin
  1009. op:=A_FISTP;
  1010. s:=S_Q;
  1011. end;
  1012. else internalerror(17);
  1013. end;
  1014. end; }
  1015. { stores an FPU value to memory }
  1016. { location:tlocation used to free up FPU register }
  1017. { ref: destination of storage }
  1018. procedure floatstore(t : tfloattype;var location:tlocation; const ref:treference);
  1019. var
  1020. op : tasmop;
  1021. s : topsize;
  1022. begin
  1023. if location.loc <> LOC_FPU then
  1024. InternalError(34);
  1025. { no emulation }
  1026. case t of
  1027. s32real : s := S_S;
  1028. s64real : s := S_Q;
  1029. s80real : s := S_X;
  1030. else
  1031. begin
  1032. Message(cg_f_unknown_float_type);
  1033. end;
  1034. end; { end case }
  1035. if not ((cs_fp_emulation) in aktswitches) then
  1036. begin
  1037. exprasmlist^.concat(new(pai68k,op_reg_ref(A_FMOVE,s,location.fpureg,newreference(ref))));
  1038. ungetregister(location.fpureg);
  1039. end
  1040. else
  1041. { handle emulation }
  1042. begin
  1043. if t = s32real then
  1044. begin
  1045. exprasmlist^.concat(new(pai68k,op_reg_ref(A_MOVE,S_L,location.fpureg,newreference(ref))));
  1046. ungetregister32(location.fpureg);
  1047. end
  1048. else
  1049. { other floating types are not supported in emulation mode }
  1050. Message(sym_e_type_id_not_defined);
  1051. end;
  1052. location.fpureg:=R_NO; { no register in LOC_FPU now }
  1053. end;
  1054. procedure firstcomplex(p : ptree);
  1055. var
  1056. hp : ptree;
  1057. begin
  1058. { always calculate boolean AND and OR from left to right }
  1059. if ((p^.treetype=orn) or (p^.treetype=andn)) and
  1060. (p^.left^.resulttype^.deftype=orddef) and
  1061. (porddef(p^.left^.resulttype)^.typ=bool8bit) then
  1062. p^.swaped:=false
  1063. else if (p^.left^.registers32<p^.right^.registers32)
  1064. { the following check is appropriate, because all }
  1065. { 4 registers are rarely used and it is thereby }
  1066. { achieved that the extra code is being dropped }
  1067. { by exchanging not commutative operators }
  1068. and (p^.right^.registers32<=4) then
  1069. begin
  1070. hp:=p^.left;
  1071. p^.left:=p^.right;
  1072. p^.right:=hp;
  1073. p^.swaped:=true;
  1074. end
  1075. else p^.swaped:=false;
  1076. end;
  1077. procedure secondfuncret(var p : ptree);
  1078. var
  1079. hregister : tregister;
  1080. begin
  1081. clear_reference(p^.location.reference);
  1082. p^.location.reference.base:=procinfo.framepointer;
  1083. p^.location.reference.offset:=procinfo.retoffset;
  1084. if ret_in_param(procinfo.retdef) then
  1085. begin
  1086. hregister:=getaddressreg;
  1087. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVEA,S_L,newreference(p^.location.reference),hregister)));
  1088. p^.location.reference.base:=hregister;
  1089. p^.location.reference.offset:=0;
  1090. end;
  1091. end;
  1092. procedure codegen_newprocedure;
  1093. begin
  1094. aktbreaklabel:=nil;
  1095. aktcontinuelabel:=nil;
  1096. { aktexitlabel:=0; is store in oldaktexitlabel
  1097. so it must not be reset to zero before this storage !}
  1098. { the type of this lists isn't important }
  1099. { because the code of this lists is }
  1100. { copied to the code segment }
  1101. procinfo.aktentrycode:=new(paasmoutput,init);
  1102. procinfo.aktexitcode:=new(paasmoutput,init);
  1103. procinfo.aktproccode:=new(paasmoutput,init);
  1104. end;
  1105. procedure codegen_doneprocedure;
  1106. begin
  1107. dispose(procinfo.aktentrycode,done);
  1108. dispose(procinfo.aktexitcode,done);
  1109. dispose(procinfo.aktproccode,done);
  1110. end;
  1111. procedure codegen_newmodule;
  1112. begin
  1113. exprasmlist:=new(paasmoutput,init);
  1114. end;
  1115. procedure codegen_donemodule;
  1116. begin
  1117. dispose(exprasmlist,done);
  1118. dispose(codesegment,done);
  1119. dispose(bsssegment,done);
  1120. dispose(datasegment,done);
  1121. dispose(debuglist,done);
  1122. dispose(externals,done);
  1123. dispose(consts,done);
  1124. end;
  1125. end.
  1126. {
  1127. $Log$
  1128. Revision 1.2 1998-03-28 23:09:54 florian
  1129. * secondin bugfix (m68k and i386)
  1130. * overflow checking bugfix (m68k and i386) -- pretty useless in
  1131. secondadd, since everything is done using 32-bit
  1132. * loading pointer to routines hopefully fixed (m68k)
  1133. * flags problem with calls to RTL internal routines fixed (still strcmp
  1134. to fix) (m68k)
  1135. * #ELSE was still incorrect (didn't take care of the previous level)
  1136. * problem with filenames in the command line solved
  1137. * problem with mangledname solved
  1138. * linking name problem solved (was case insensitive)
  1139. * double id problem and potential crash solved
  1140. * stop after first error
  1141. * and=>test problem removed
  1142. * correct read for all float types
  1143. * 2 sigsegv fixes and a cosmetic fix for Internal Error
  1144. * push/pop is now correct optimized (=> mov (%esp),reg)
  1145. Revision 1.1.1.1 1998/03/25 11:18:13 root
  1146. * Restored version
  1147. Revision 1.15 1998/03/22 12:45:38 florian
  1148. * changes of Carl-Eric to m68k target commit:
  1149. - wrong nodes because of the new string cg in intel, I had to create
  1150. this under m68k also ... had to work it out to fix potential alignment
  1151. problems --> this removes the crash of the m68k compiler.
  1152. - added absolute addressing in m68k assembler (required for Amiga startup)
  1153. - fixed alignment problems (because of byte return values, alignment
  1154. would not be always valid) -- is this ok if i change the offset if odd in
  1155. setfirsttemp ?? -- it seems ok...
  1156. Revision 1.14 1998/03/10 04:20:37 carl
  1157. * extdebug problems
  1158. - removed loadstring as it is not required for the m68k
  1159. Revision 1.13 1998/03/10 01:17:16 peter
  1160. * all files have the same header
  1161. * messages are fully implemented, EXTDEBUG uses Comment()
  1162. + AG... files for the Assembler generation
  1163. Revision 1.12 1998/03/09 10:44:35 peter
  1164. + string='', string<>'', string:='', string:=char optimizes (the first 2
  1165. were already in cg68k2)
  1166. Revision 1.11 1998/03/06 00:52:03 peter
  1167. * replaced all old messages from errore.msg, only ExtDebug and some
  1168. Comment() calls are left
  1169. * fixed options.pas
  1170. Revision 1.10 1998/03/03 04:12:04 carl
  1171. * moved generate routines to this unit
  1172. Revision 1.9 1998/03/02 01:48:17 peter
  1173. * renamed target_DOS to target_GO32V1
  1174. + new verbose system, merged old errors and verbose units into one new
  1175. verbose.pas, so errors.pas is obsolete
  1176. Revision 1.8 1998/02/13 10:34:45 daniel
  1177. * Made Motorola version compilable.
  1178. * Fixed optimizer
  1179. Revision 1.7 1998/02/12 11:49:50 daniel
  1180. Yes! Finally! After three retries, my patch!
  1181. Changes:
  1182. Complete rewrite of psub.pas.
  1183. Added support for DLL's.
  1184. Compiler requires less memory.
  1185. Platform units for each platform.
  1186. Revision 1.6 1998/01/11 03:39:02 carl
  1187. * bugfix of concatcopy , was using wrong reference
  1188. * bugfix of MOVEQ
  1189. Revision 1.3 1997/12/09 13:30:05 carl
  1190. + renamed some stuff
  1191. Revision 1.2 1997/12/03 13:59:01 carl
  1192. + added emitcall as in i386 version.
  1193. Revision 1.1.1.1 1997/11/27 08:32:53 michael
  1194. FPC Compiler CVS start
  1195. Pre-CVS log:
  1196. CEC Carl-Eric Codere
  1197. FK Florian Klaempfl
  1198. PM Pierre Muller
  1199. + feature added
  1200. - removed
  1201. * bug fixed or changed
  1202. History:
  1203. 27th september 1997:
  1204. + first version for MC68000 (using v093 template) (CEC)
  1205. 9th october 1997:
  1206. * fixed a bug in push_int as well as other routines which used
  1207. getregister32 while they are not supposed to (because of how
  1208. the allocation of registers work in parser.pas) (CEC)
  1209. * Fixed some bugs in the concatcopy routine, was allocating
  1210. registers which were not supposed to be allocated. (CEC)
  1211. }