cga68k.pas 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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_FS;
  239. s64real: getfloatsize := S_FL;
  240. s80real: getfloatsize := S_FX;
  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. end;
  555. {Generate the exit code for a procedure.}
  556. procedure genexitcode(parasize:longint;nostackframe:boolean);
  557. var hr:Preference; {This is for function results.}
  558. op:Tasmop;
  559. s:Topsize;
  560. begin
  561. { !!!! insert there automatic destructors }
  562. procinfo.aktexitcode^.insert(new(pai_label,init(aktexitlabel)));
  563. { call the destructor help procedure }
  564. if (aktprocsym^.definition^.options and podestructor)<>0 then
  565. begin
  566. if procinfo._class^.isclass then
  567. begin
  568. procinfo.aktexitcode^.insert(new(pai68k,op_csymbol(A_JSR,S_NO,
  569. newcsymbol('DISPOSE_CLASS',0))));
  570. concat_external('DISPOSE_CLASS',EXT_NEAR);
  571. end
  572. else
  573. begin
  574. procinfo.aktexitcode^.insert(new(pai68k,op_csymbol(A_JSR,S_NO,
  575. newcsymbol('HELP_DESTRUCTOR',0))));
  576. concat_external('HELP_DESTRUCTOR',EXT_NEAR);
  577. end;
  578. end;
  579. { call __EXIT for main program }
  580. { ????????? }
  581. if (aktprocsym^.definition^.options and poproginit)<>0 then
  582. begin
  583. procinfo.aktexitcode^.concat(new(pai68k,op_csymbol(A_JSR,S_NO,newcsymbol('__EXIT',0))));
  584. externals^.concat(new(pai_external,init('__EXIT',EXT_NEAR)));
  585. end;
  586. { handle return value }
  587. if (aktprocsym^.definition^.options and poassembler)=0 then
  588. if (aktprocsym^.definition^.options and poconstructor)=0 then
  589. begin
  590. if procinfo.retdef<>pdef(voiddef) then
  591. begin
  592. if not procinfo.funcret_is_valid then
  593. Message(sym_w_function_result_not_set);
  594. new(hr);
  595. reset_reference(hr^);
  596. hr^.offset:=procinfo.retoffset;
  597. hr^.base:=procinfo.framepointer;
  598. if (procinfo.retdef^.deftype=orddef) then
  599. begin
  600. case porddef(procinfo.retdef)^.typ of
  601. s32bit,u32bit :
  602. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hr,R_D0)));
  603. u8bit,s8bit,uchar,bool8bit :
  604. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,S_B,hr,R_D0)));
  605. s16bit,u16bit :
  606. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,S_W,hr,R_D0)));
  607. end;
  608. end
  609. else
  610. if (procinfo.retdef^.deftype in [pointerdef,enumdef,procvardef]) or
  611. ((procinfo.retdef^.deftype=setdef) and
  612. (psetdef(procinfo.retdef)^.settype=smallset)) then
  613. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hr,R_D0)))
  614. else
  615. if (procinfo.retdef^.deftype=floatdef) then
  616. begin
  617. if pfloatdef(procinfo.retdef)^.typ=f32bit then
  618. begin
  619. { Isnt this missing ? }
  620. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hr,R_D0)));
  621. end
  622. else
  623. begin
  624. { how the return value is handled }
  625. { if in FPU mode, return in FP0 }
  626. if (pfloatdef(procinfo.retdef)^.typ = s32real)
  627. and (cs_fp_emulation in aktswitches) then
  628. begin
  629. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,
  630. S_L,hr,R_D0)))
  631. end
  632. else
  633. begin
  634. if cs_fp_emulation in aktswitches then
  635. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_MOVE,
  636. S_L,hr,R_D0)))
  637. else
  638. procinfo.aktexitcode^.concat(new(pai68k,op_ref_reg(A_FMOVE,
  639. getfloatsize(pfloatdef(procinfo.retdef)^.typ),hr,R_FP0)));
  640. end;
  641. end;
  642. end
  643. else
  644. dispose(hr);
  645. end
  646. end
  647. else
  648. begin
  649. { successful constructor deletes the zero flag }
  650. { and returns self in accumulator }
  651. procinfo.aktexitcode^.concat(new(pai_label,init(quickexitlabel)));
  652. { eax must be set to zero if the allocation failed !!! }
  653. procinfo.aktexitcode^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,R_A5,R_D0)));
  654. { faster then OR on mc68000/mc68020 }
  655. procinfo.aktexitcode^.concat(new(pai68k,op_reg(A_TST,S_L,R_D0)));
  656. end;
  657. procinfo.aktexitcode^.concat(new(pai_label,init(aktexit2label)));
  658. if not(nostackframe) then
  659. procinfo.aktexitcode^.concat(new(pai68k,op_reg(A_UNLK,S_NO,R_A6)));
  660. { at last, the return is generated }
  661. if (aktprocsym^.definition^.options and pointerrupt)<>0 then
  662. generate_interrupt_stackframe_exit
  663. else
  664. if (parasize=0) or ((aktprocsym^.definition^.options and poclearstack)<>0)
  665. then
  666. {Routines with the poclearstack flag set use only a ret.}
  667. { also routines with parasize=0 }
  668. procinfo.aktexitcode^.concat(new(pai68k,op_none(A_RTS,S_NO)))
  669. else
  670. { return with immediate size possible here }
  671. { signed! }
  672. if (opt_processors = MC68020) and (parasize < $7FFF) then
  673. procinfo.aktexitcode^.concat(new(pai68k,op_const(
  674. A_RTD,S_NO,parasize)))
  675. { manually restore the stack }
  676. else
  677. begin
  678. { We must pull the PC Counter from the stack, before }
  679. { restoring the stack pointer, otherwise the PC would }
  680. { point to nowhere! }
  681. { save the PC counter (pop it from the stack) }
  682. procinfo.aktexitcode^.concat(new(pai68k,op_reg_reg(
  683. A_MOVE,S_L,R_SPPULL,R_A0)));
  684. { can we do a quick addition ... }
  685. if (parasize > 0) and (parasize < 9) then
  686. procinfo.aktexitcode^.concat(new(pai68k,op_const_reg(
  687. A_ADD,S_L,parasize,R_SP)))
  688. else { nope ... }
  689. procinfo.aktexitcode^.concat(new(pai68k,op_const_reg(
  690. A_ADD,S_L,parasize,R_SP)));
  691. { endif }
  692. { restore the PC counter (push it on the stack) }
  693. procinfo.aktexitcode^.concat(new(pai68k,op_reg_reg(
  694. A_MOVE,S_L,R_A0,R_SPPUSH)));
  695. procinfo.aktexitcode^.concat(new(pai68k,op_none(
  696. A_RTS,S_NO)))
  697. end;
  698. {$ifdef GDB}
  699. if cs_debuginfo in aktswitches then
  700. begin
  701. aktprocsym^.concatstabto(procinfo.aktexitcode);
  702. if assigned(procinfo._class) then
  703. procinfo.aktexitcode^.concat(new(pai_stabs,init(strpnew(
  704. '"$t:v'+procinfo._class^.numberstring+'",'+
  705. tostr(N_PSYM)+',0,0,'+tostr(procinfo.esi_offset)))));
  706. if (porddef(aktprocsym^.definition^.retdef) <> voiddef) then
  707. procinfo.aktexitcode^.concat(new(pai_stabs,init(strpnew(
  708. '"'+aktprocsym^.name+':X'+aktprocsym^.definition^.retdef^.numberstring+'",'+
  709. tostr(N_PSYM)+',0,0,'+tostr(procinfo.retoffset)))));
  710. procinfo.aktexitcode^.concat(new(pai_stabn,init(strpnew('192,0,0,'
  711. +aktprocsym^.definition^.mangledname))));
  712. procinfo.aktexitcode^.concat(new(pai_stabn,init(strpnew('224,0,0,'
  713. +lab2str(aktexit2label)))));
  714. end;
  715. {$endif * GDB *}
  716. end;
  717. { USES REGISTERS R_A0 AND R_A1 }
  718. procedure concatcopy(source,dest : treference;size : longint;delsource : boolean);
  719. var
  720. ecxpushed : boolean;
  721. helpsize : longint;
  722. i : byte;
  723. reg8,reg32 : tregister;
  724. swap : boolean;
  725. hregister : tregister;
  726. iregister : tregister;
  727. jregister : tregister;
  728. hp1 : treference;
  729. hp2 : treference;
  730. hl : plabel;
  731. begin
  732. hregister := getregister32;
  733. if delsource then
  734. del_reference(source);
  735. { from 12 bytes movs is being used }
  736. if (size<=8) or (not(cs_littlesize in aktswitches) and (size<=12)) then
  737. begin
  738. helpsize:=size div 4;
  739. { move a dword x times }
  740. for i:=1 to helpsize do
  741. begin
  742. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,newreference(source),hregister)));
  743. exprasmlist^.concat(new(pai68k,op_reg_ref(A_MOVE,S_L,hregister,newreference(dest))));
  744. inc(source.offset,4);
  745. inc(dest.offset,4);
  746. dec(size,4);
  747. end;
  748. { move a word }
  749. if size>1 then
  750. begin
  751. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_W,newreference(source),hregister)));
  752. exprasmlist^.concat(new(pai68k,op_reg_ref(A_MOVE,S_W,hregister,newreference(dest))));
  753. inc(source.offset,2);
  754. inc(dest.offset,2);
  755. dec(size,2);
  756. end;
  757. { move a single byte }
  758. if size>0 then
  759. begin
  760. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_B,newreference(source),hregister)));
  761. exprasmlist^.concat(new(pai68k,op_reg_ref(A_MOVE,S_B,hregister,newreference(dest))));
  762. end
  763. end
  764. else
  765. begin
  766. if (usableaddress > 1) then
  767. begin
  768. iregister := getaddressreg;
  769. jregister := getaddressreg;
  770. end
  771. else
  772. if (usableaddress = 1) then
  773. begin
  774. iregister := getaddressreg;
  775. jregister := R_A1;
  776. end
  777. else
  778. begin
  779. iregister := R_A0;
  780. jregister := R_A1;
  781. end;
  782. { reference for move (An)+,(An)+ }
  783. reset_reference(hp1);
  784. hp1.base := iregister; { source register }
  785. hp1.direction := dir_inc;
  786. reset_reference(hp2);
  787. hp2.base := jregister;
  788. hp1.direction := dir_inc;
  789. { iregister = source }
  790. { jregister = destination }
  791. exprasmlist^.concat(new(pai68k,op_ref_reg(A_LEA,S_L,newreference(source),iregister)));
  792. exprasmlist^.concat(new(pai68k,op_ref_reg(A_LEA,S_L,newreference(dest),jregister)));
  793. { double word move }
  794. helpsize := size - size mod 4;
  795. size := size mod 4;
  796. exprasmlist^.concat(new(pai68k,op_const_reg(A_MOVE,S_L,helpsize div 4,hregister)));
  797. getlabel(hl);
  798. emitl(A_LABEL,hl);
  799. exprasmlist^.concat(new(pai68k,op_ref_ref(A_MOVE,S_L,newreference(hp1),newreference(hp2))));
  800. exprasmlist^.concat(new(pai68k,op_const_reg(A_SUBQ,S_L,4,hregister)));
  801. emitl(A_BNE,hl);
  802. if size > 1 then
  803. begin
  804. dec(size,2);
  805. exprasmlist^.concat(new(pai68k,op_ref_ref(A_MOVE,S_W,newreference(hp1), newreference(hp2))));
  806. end;
  807. if size = 1 then
  808. exprasmlist^.concat(new(pai68k,op_ref_ref(A_MOVE,S_B,newreference(hp1), newreference(hp2))));
  809. { restore the registers that we have just used olny if they are used! }
  810. if jregister = R_A1 then
  811. hp2.base := R_NO;
  812. if iregister = R_A0 then
  813. hp1.base := R_NO;
  814. del_reference(hp1);
  815. del_reference(hp2);
  816. end;
  817. { loading SELF-reference again }
  818. maybe_loada5;
  819. if delsource then
  820. ungetiftemp(source);
  821. ungetregister32(hregister);
  822. end;
  823. procedure emitloadord2reg(location:Tlocation;orddef:Porddef;
  824. destreg:Tregister;delloc:boolean);
  825. {A lot smaller and less bug sensitive than the original unfolded loads.}
  826. var tai:pai68k;
  827. r:Preference;
  828. begin
  829. case location.loc of
  830. LOC_REGISTER,LOC_CREGISTER:
  831. begin
  832. case orddef^.typ of
  833. u8bit: begin
  834. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,location.register,destreg)));
  835. exprasmlist^.concat(new(pai68k,op_const_reg(A_ANDI,S_L,$FF,destreg)));
  836. end;
  837. s8bit: begin
  838. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_B,location.register,destreg)));
  839. if (opt_processors <> MC68020) then
  840. begin
  841. { byte to word }
  842. exprasmlist^.concat(new(pai68k,op_reg(A_EXT,S_W,destreg)));
  843. { word to long }
  844. exprasmlist^.concat(new(pai68k,op_reg(A_EXT,S_L,destreg)));
  845. end
  846. else { 68020+ and later only }
  847. exprasmlist^.concat(new(pai68k,op_reg(A_EXTB,S_L,destreg)));
  848. end;
  849. u16bit: begin
  850. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_W,location.register,destreg)));
  851. exprasmlist^.concat(new(pai68k,op_const_reg(A_ANDI,S_L,$FFFF,destreg)));
  852. end;
  853. s16bit: begin
  854. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_W,location.register,destreg)));
  855. { word to long }
  856. exprasmlist^.concat(new(pai68k,op_reg(A_EXT,S_L,destreg)));
  857. end;
  858. u32bit:
  859. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,location.register,destreg)));
  860. s32bit:
  861. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,location.register,destreg)));
  862. end;
  863. if delloc then
  864. ungetregister(location.register);
  865. end;
  866. LOC_REFERENCE:
  867. begin
  868. r:=newreference(location.reference);
  869. case orddef^.typ of
  870. u8bit: begin
  871. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,r,destreg)));
  872. exprasmlist^.concat(new(pai68k,op_const_reg(A_ANDI,S_L,$FF,destreg)));
  873. end;
  874. s8bit: begin
  875. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_B,r,destreg)));
  876. if (opt_processors <> MC68020) then
  877. begin
  878. { byte to word }
  879. exprasmlist^.concat(new(pai68k,op_reg(A_EXT,S_W,destreg)));
  880. { word to long }
  881. exprasmlist^.concat(new(pai68k,op_reg(A_EXT,S_L,destreg)));
  882. end
  883. else { 68020+ and later only }
  884. exprasmlist^.concat(new(pai68k,op_reg(A_EXTB,S_L,destreg)));
  885. end;
  886. u16bit: begin
  887. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_W,r,destreg)));
  888. exprasmlist^.concat(new(pai68k,op_const_reg(A_ANDI,S_L,$ffff,destreg)));
  889. end;
  890. s16bit: begin
  891. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_W,r,destreg)));
  892. { word to long }
  893. exprasmlist^.concat(new(pai68k,op_reg(A_EXT,S_L,destreg)));
  894. end;
  895. u32bit:
  896. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,r,destreg)));
  897. s32bit:
  898. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,r,destreg)));
  899. end;
  900. if delloc then
  901. del_reference(location.reference);
  902. end
  903. else
  904. internalerror(6);
  905. end;
  906. end;
  907. { if necessary A5 is reloaded after a call}
  908. procedure maybe_loada5;
  909. var
  910. hp : preference;
  911. p : pprocinfo;
  912. i : longint;
  913. begin
  914. if assigned(procinfo._class) then
  915. begin
  916. if lexlevel>2 then
  917. begin
  918. new(hp);
  919. reset_reference(hp^);
  920. hp^.offset:=procinfo.framepointer_offset;
  921. hp^.base:=procinfo.framepointer;
  922. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hp,R_A5)));
  923. p:=procinfo.parent;
  924. for i:=3 to lexlevel-1 do
  925. begin
  926. new(hp);
  927. reset_reference(hp^);
  928. hp^.offset:=p^.framepointer_offset;
  929. hp^.base:=R_A5;
  930. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hp,R_A5)));
  931. p:=p^.parent;
  932. end;
  933. new(hp);
  934. reset_reference(hp^);
  935. hp^.offset:=p^.ESI_offset;
  936. hp^.base:=R_A5;
  937. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hp,R_A5)));
  938. end
  939. else
  940. begin
  941. new(hp);
  942. reset_reference(hp^);
  943. hp^.offset:=procinfo.ESI_offset;
  944. hp^.base:=procinfo.framepointer;
  945. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,hp,R_A5)));
  946. end;
  947. end;
  948. end;
  949. (***********************************************************************)
  950. (* PROCEDURE FLOATLOAD *)
  951. (* Description: This routine is to be called each time a location *)
  952. (* must be set to LOC_FPU and a value loaded into a FPU register. *)
  953. (* *)
  954. (* Remark: The routine sets up the register field of LOC_FPU correctly*)
  955. (***********************************************************************)
  956. procedure floatload(t : tfloattype;const ref : treference; var location:tlocation);
  957. var
  958. op : tasmop;
  959. s : topsize;
  960. begin
  961. { no emulation }
  962. case t of
  963. s32real : s := S_FS;
  964. s64real : s := S_FL;
  965. s80real : s := S_FX;
  966. else
  967. begin
  968. Message(cg_f_unknown_float_type);
  969. end;
  970. end; { end case }
  971. location.loc := LOC_FPU;
  972. if not ((cs_fp_emulation) in aktswitches) then
  973. begin
  974. location.fpureg := getfloatreg;
  975. exprasmlist^.concat(new(pai68k,op_ref_reg(A_FMOVE,s,newreference(ref),location.fpureg)))
  976. end
  977. else
  978. { handle emulation }
  979. begin
  980. if t = s32real then
  981. begin
  982. location.fpureg := getregister32;
  983. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,newreference(ref),location.fpureg)))
  984. end
  985. else
  986. { other floating types are not supported in emulation mode }
  987. Message(sym_e_type_id_not_defined);
  988. end;
  989. end;
  990. { procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  991. begin
  992. case t of
  993. s32real : begin
  994. op:=A_FSTP;
  995. s:=S_FS;
  996. end;
  997. s64real : begin
  998. op:=A_FSTP;
  999. s:=S_FL;
  1000. end;
  1001. s80real : begin
  1002. op:=A_FSTP;
  1003. s:=S_FX;
  1004. end;
  1005. s64bit : begin
  1006. op:=A_FISTP;
  1007. s:=S_IQ;
  1008. end;
  1009. else internalerror(17);
  1010. end;
  1011. end; }
  1012. { stores an FPU value to memory }
  1013. { location:tlocation used to free up FPU register }
  1014. { ref: destination of storage }
  1015. procedure floatstore(t : tfloattype;var location:tlocation; const ref:treference);
  1016. var
  1017. op : tasmop;
  1018. s : topsize;
  1019. begin
  1020. if location.loc <> LOC_FPU then
  1021. InternalError(34);
  1022. { no emulation }
  1023. case t of
  1024. s32real : s := S_FS;
  1025. s64real : s := S_FL;
  1026. s80real : s := S_FX;
  1027. else
  1028. begin
  1029. Message(cg_f_unknown_float_type);
  1030. end;
  1031. end; { end case }
  1032. if not ((cs_fp_emulation) in aktswitches) then
  1033. begin
  1034. exprasmlist^.concat(new(pai68k,op_reg_ref(A_FMOVE,s,location.fpureg,newreference(ref))));
  1035. ungetregister(location.fpureg);
  1036. end
  1037. else
  1038. { handle emulation }
  1039. begin
  1040. if t = s32real then
  1041. begin
  1042. exprasmlist^.concat(new(pai68k,op_reg_ref(A_MOVE,S_L,location.fpureg,newreference(ref))));
  1043. ungetregister32(location.fpureg);
  1044. end
  1045. else
  1046. { other floating types are not supported in emulation mode }
  1047. Message(sym_e_type_id_not_defined);
  1048. end;
  1049. location.fpureg:=R_NO; { no register in LOC_FPU now }
  1050. end;
  1051. procedure firstcomplex(p : ptree);
  1052. var
  1053. hp : ptree;
  1054. begin
  1055. { always calculate boolean AND and OR from left to right }
  1056. if ((p^.treetype=orn) or (p^.treetype=andn)) and
  1057. (p^.left^.resulttype^.deftype=orddef) and
  1058. (porddef(p^.left^.resulttype)^.typ=bool8bit) then
  1059. p^.swaped:=false
  1060. else if (p^.left^.registers32<p^.right^.registers32)
  1061. { the following check is appropriate, because all }
  1062. { 4 registers are rarely used and it is thereby }
  1063. { achieved that the extra code is being dropped }
  1064. { by exchanging not commutative operators }
  1065. and (p^.right^.registers32<=4) then
  1066. begin
  1067. hp:=p^.left;
  1068. p^.left:=p^.right;
  1069. p^.right:=hp;
  1070. p^.swaped:=true;
  1071. end
  1072. else p^.swaped:=false;
  1073. end;
  1074. procedure secondfuncret(var p : ptree);
  1075. var
  1076. hregister : tregister;
  1077. begin
  1078. clear_reference(p^.location.reference);
  1079. p^.location.reference.base:=procinfo.framepointer;
  1080. p^.location.reference.offset:=procinfo.retoffset;
  1081. if ret_in_param(procinfo.retdef) then
  1082. begin
  1083. hregister:=getaddressreg;
  1084. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVEA,S_L,newreference(p^.location.reference),hregister)));
  1085. p^.location.reference.base:=hregister;
  1086. p^.location.reference.offset:=0;
  1087. end;
  1088. end;
  1089. procedure codegen_newprocedure;
  1090. begin
  1091. aktbreaklabel:=nil;
  1092. aktcontinuelabel:=nil;
  1093. { aktexitlabel:=0; is store in oldaktexitlabel
  1094. so it must not be reset to zero before this storage !}
  1095. { the type of this lists isn't important }
  1096. { because the code of this lists is }
  1097. { copied to the code segment }
  1098. procinfo.aktentrycode:=new(paasmoutput,init);
  1099. procinfo.aktexitcode:=new(paasmoutput,init);
  1100. procinfo.aktproccode:=new(paasmoutput,init);
  1101. end;
  1102. procedure codegen_doneprocedure;
  1103. begin
  1104. dispose(procinfo.aktentrycode,done);
  1105. dispose(procinfo.aktexitcode,done);
  1106. dispose(procinfo.aktproccode,done);
  1107. end;
  1108. procedure codegen_newmodule;
  1109. begin
  1110. exprasmlist:=new(paasmoutput,init);
  1111. end;
  1112. procedure codegen_donemodule;
  1113. begin
  1114. dispose(exprasmlist,done);
  1115. dispose(codesegment,done);
  1116. dispose(bsssegment,done);
  1117. dispose(datasegment,done);
  1118. dispose(debuglist,done);
  1119. dispose(externals,done);
  1120. dispose(consts,done);
  1121. end;
  1122. end.
  1123. {
  1124. $Log$
  1125. Revision 1.3 1998-04-29 10:33:46 pierre
  1126. + added some code for ansistring (not complete nor working yet)
  1127. * corrected operator overloading
  1128. * corrected nasm output
  1129. + started inline procedures
  1130. + added starstarn : use ** for exponentiation (^ gave problems)
  1131. + started UseTokenInfo cond to get accurate positions
  1132. Revision 1.2 1998/03/28 23:09:54 florian
  1133. * secondin bugfix (m68k and i386)
  1134. * overflow checking bugfix (m68k and i386) -- pretty useless in
  1135. secondadd, since everything is done using 32-bit
  1136. * loading pointer to routines hopefully fixed (m68k)
  1137. * flags problem with calls to RTL internal routines fixed (still strcmp
  1138. to fix) (m68k)
  1139. * #ELSE was still incorrect (didn't take care of the previous level)
  1140. * problem with filenames in the command line solved
  1141. * problem with mangledname solved
  1142. * linking name problem solved (was case insensitive)
  1143. * double id problem and potential crash solved
  1144. * stop after first error
  1145. * and=>test problem removed
  1146. * correct read for all float types
  1147. * 2 sigsegv fixes and a cosmetic fix for Internal Error
  1148. * push/pop is now correct optimized (=> mov (%esp),reg)
  1149. Revision 1.1.1.1 1998/03/25 11:18:13 root
  1150. * Restored version
  1151. Revision 1.15 1998/03/22 12:45:38 florian
  1152. * changes of Carl-Eric to m68k target commit:
  1153. - wrong nodes because of the new string cg in intel, I had to create
  1154. this under m68k also ... had to work it out to fix potential alignment
  1155. problems --> this removes the crash of the m68k compiler.
  1156. - added absolute addressing in m68k assembler (required for Amiga startup)
  1157. - fixed alignment problems (because of byte return values, alignment
  1158. would not be always valid) -- is this ok if i change the offset if odd in
  1159. setfirsttemp ?? -- it seems ok...
  1160. Revision 1.14 1998/03/10 04:20:37 carl
  1161. * extdebug problems
  1162. - removed loadstring as it is not required for the m68k
  1163. Revision 1.13 1998/03/10 01:17:16 peter
  1164. * all files have the same header
  1165. * messages are fully implemented, EXTDEBUG uses Comment()
  1166. + AG... files for the Assembler generation
  1167. Revision 1.12 1998/03/09 10:44:35 peter
  1168. + string='', string<>'', string:='', string:=char optimizes (the first 2
  1169. were already in cg68k2)
  1170. Revision 1.11 1998/03/06 00:52:03 peter
  1171. * replaced all old messages from errore.msg, only ExtDebug and some
  1172. Comment() calls are left
  1173. * fixed options.pas
  1174. Revision 1.10 1998/03/03 04:12:04 carl
  1175. * moved generate routines to this unit
  1176. Revision 1.9 1998/03/02 01:48:17 peter
  1177. * renamed target_DOS to target_GO32V1
  1178. + new verbose system, merged old errors and verbose units into one new
  1179. verbose.pas, so errors.pas is obsolete
  1180. Revision 1.8 1998/02/13 10:34:45 daniel
  1181. * Made Motorola version compilable.
  1182. * Fixed optimizer
  1183. Revision 1.7 1998/02/12 11:49:50 daniel
  1184. Yes! Finally! After three retries, my patch!
  1185. Changes:
  1186. Complete rewrite of psub.pas.
  1187. Added support for DLL's.
  1188. Compiler requires less memory.
  1189. Platform units for each platform.
  1190. Revision 1.6 1998/01/11 03:39:02 carl
  1191. * bugfix of concatcopy , was using wrong reference
  1192. * bugfix of MOVEQ
  1193. Revision 1.3 1997/12/09 13:30:05 carl
  1194. + renamed some stuff
  1195. Revision 1.2 1997/12/03 13:59:01 carl
  1196. + added emitcall as in i386 version.
  1197. Revision 1.1.1.1 1997/11/27 08:32:53 michael
  1198. FPC Compiler CVS start
  1199. Pre-CVS log:
  1200. CEC Carl-Eric Codere
  1201. FK Florian Klaempfl
  1202. PM Pierre Muller
  1203. + feature added
  1204. - removed
  1205. * bug fixed or changed
  1206. History:
  1207. 27th september 1997:
  1208. + first version for MC68000 (using v093 template) (CEC)
  1209. 9th october 1997:
  1210. * fixed a bug in push_int as well as other routines which used
  1211. getregister32 while they are not supposed to (because of how
  1212. the allocation of registers work in parser.pas) (CEC)
  1213. * Fixed some bugs in the concatcopy routine, was allocating
  1214. registers which were not supposed to be allocated. (CEC)
  1215. }