cgcpu.pas 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158
  1. {
  2. Copyright (c) 1998-2002 by the FPC team
  3. This unit implements the code generator for the 680x0
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cgbase,cgobj,globtype,
  22. aasmbase,aasmtai,aasmdata,aasmcpu,
  23. cpubase,cpuinfo,
  24. parabase,cpupara,
  25. node,symconst,symtype,symdef,
  26. cgutils,cg64f32;
  27. type
  28. tcg68k = class(tcg)
  29. procedure init_register_allocators;override;
  30. procedure done_register_allocators;override;
  31. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);override;
  32. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);override;
  33. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);override;
  34. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);override;
  35. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  36. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  37. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);override;
  38. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  39. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);override;
  40. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);override;
  41. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);override;
  42. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);override;
  43. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  44. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  45. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  46. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  47. procedure a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);override;
  48. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister); override;
  49. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  50. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  51. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); override;
  52. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister; l : tasmlabel);override;
  53. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel); override;
  54. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  55. procedure a_jmp_name(list : TAsmList;const s : string); override;
  56. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  57. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  58. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  59. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  60. { generates overflow checking code for a node }
  61. procedure g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef); override;
  62. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  63. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  64. procedure g_save_registers(list:TAsmList);override;
  65. procedure g_restore_registers(list:TAsmList);override;
  66. procedure g_adjust_self_value(list:TAsmList;procdef:tprocdef;ioffset:tcgint);override;
  67. { # Sign or zero extend the register to a full 32-bit value.
  68. The new value is left in the same register.
  69. }
  70. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  71. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  72. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  73. function fixref(list: TAsmList; var ref: treference): boolean;
  74. protected
  75. procedure call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  76. procedure call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  77. private
  78. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  79. function force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  80. procedure move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  81. end;
  82. tcg64f68k = class(tcg64f32)
  83. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG; size: tcgsize; regsrc,regdst : tregister64);override;
  84. procedure a_op64_const_reg(list : TAsmList;op:TOpCG; size: tcgsize; value : int64;regdst : tregister64);override;
  85. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);override;
  86. end;
  87. { This function returns true if the reference+offset is valid.
  88. Otherwise extra code must be generated to solve the reference.
  89. On the m68k, this verifies that the reference is valid
  90. (e.g : if index register is used, then the max displacement
  91. is 256 bytes, if only base is used, then max displacement
  92. is 32K
  93. }
  94. function isvalidrefoffset(const ref: treference): boolean;
  95. function isvalidreference(const ref: treference): boolean;
  96. procedure create_codegen;
  97. implementation
  98. uses
  99. globals,verbose,systems,cutils,
  100. symsym,symtable,defutil,paramgr,procinfo,
  101. rgobj,tgobj,rgcpu,fmodule;
  102. const
  103. { opcode table lookup }
  104. topcg2tasmop: Array[topcg] of tasmop =
  105. (
  106. A_NONE,
  107. A_MOVE,
  108. A_ADD,
  109. A_AND,
  110. A_DIVU,
  111. A_DIVS,
  112. A_MULS,
  113. A_MULU,
  114. A_NEG,
  115. A_NOT,
  116. A_OR,
  117. A_ASR,
  118. A_LSL,
  119. A_LSR,
  120. A_SUB,
  121. A_EOR,
  122. A_ROL,
  123. A_ROR
  124. );
  125. { opcode with extend bits table lookup, used by 64bit cg }
  126. topcg2tasmopx: Array[topcg] of tasmop =
  127. (
  128. A_NONE,
  129. A_NONE,
  130. A_ADDX,
  131. A_NONE,
  132. A_NONE,
  133. A_NONE,
  134. A_NONE,
  135. A_NONE,
  136. A_NEGX,
  137. A_NONE,
  138. A_NONE,
  139. A_NONE,
  140. A_NONE,
  141. A_NONE,
  142. A_SUBX,
  143. A_NONE,
  144. A_NONE,
  145. A_NONE
  146. );
  147. TOpCmp2AsmCond: Array[topcmp] of TAsmCond =
  148. (
  149. C_NONE,
  150. C_EQ,
  151. C_GT,
  152. C_LT,
  153. C_GE,
  154. C_LE,
  155. C_NE,
  156. C_LS,
  157. C_CS,
  158. C_CC,
  159. C_HI
  160. );
  161. function isvalidreference(const ref: treference): boolean;
  162. begin
  163. isvalidreference:=isvalidrefoffset(ref) and
  164. { don't try to generate addressing with symbol and base reg and offset
  165. it might fail in linking stage if the symbol is more than 32k away (KB) }
  166. not (assigned(ref.symbol) and (ref.base <> NR_NO) and (ref.offset <> 0)) and
  167. { coldfire and 68000 cannot handle non-addressregs as bases }
  168. not ((current_settings.cputype in cpu_coldfire+[cpu_mc68000]) and
  169. not isaddressregister(ref.base));
  170. end;
  171. function isvalidrefoffset(const ref: treference): boolean;
  172. begin
  173. isvalidrefoffset := true;
  174. if ref.index <> NR_NO then
  175. begin
  176. // if ref.base <> NR_NO then
  177. // internalerror(2002081401);
  178. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  179. isvalidrefoffset := false
  180. end
  181. else
  182. begin
  183. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  184. isvalidrefoffset := false;
  185. end;
  186. end;
  187. {****************************************************************************}
  188. { TCG68K }
  189. {****************************************************************************}
  190. function use_push(const cgpara:tcgpara):boolean;
  191. begin
  192. result:=(not paramanager.use_fixed_stack) and
  193. assigned(cgpara.location) and
  194. (cgpara.location^.loc=LOC_REFERENCE) and
  195. (cgpara.location^.reference.index=NR_STACK_POINTER_REG);
  196. end;
  197. procedure tcg68k.init_register_allocators;
  198. var
  199. reg: TSuperRegister;
  200. address_regs: array of TSuperRegister;
  201. begin
  202. inherited init_register_allocators;
  203. address_regs:=nil;
  204. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  205. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7],
  206. first_int_imreg,[]);
  207. { set up the array of address registers to use }
  208. for reg:=RS_A0 to RS_A6 do
  209. begin
  210. { don't hardwire the frame pointer register, because it can vary between target OS }
  211. if assigned(current_procinfo) and (current_procinfo.framepointer = NR_FRAME_POINTER_REG)
  212. and (reg = RS_FRAME_POINTER_REG) then
  213. continue;
  214. setlength(address_regs,length(address_regs)+1);
  215. address_regs[length(address_regs)-1]:=reg;
  216. end;
  217. rg[R_ADDRESSREGISTER]:=trgcpu.create(R_ADDRESSREGISTER,R_SUBWHOLE,
  218. address_regs, first_addr_imreg, []);
  219. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  220. [RS_FP0,RS_FP1,RS_FP2,RS_FP3,RS_FP4,RS_FP5,RS_FP6,RS_FP7],
  221. first_fpu_imreg,[]);
  222. end;
  223. procedure tcg68k.done_register_allocators;
  224. begin
  225. rg[R_INTREGISTER].free;
  226. rg[R_FPUREGISTER].free;
  227. rg[R_ADDRESSREGISTER].free;
  228. inherited done_register_allocators;
  229. end;
  230. procedure tcg68k.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  231. var
  232. pushsize : tcgsize;
  233. ref : treference;
  234. begin
  235. { it's probably necessary to port this from x86 later, or provide an m68k solution (KB) }
  236. { TODO: FIX ME! check_register_size()}
  237. // check_register_size(size,r);
  238. if use_push(cgpara) then
  239. begin
  240. cgpara.check_simple_location;
  241. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  242. pushsize:=cgpara.location^.size
  243. else
  244. pushsize:=int_cgsize(cgpara.alignment);
  245. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  246. ref.direction := dir_dec;
  247. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],makeregsize(list,r,pushsize),ref));
  248. end
  249. else
  250. inherited a_load_reg_cgpara(list,size,r,cgpara);
  251. end;
  252. procedure tcg68k.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);
  253. var
  254. pushsize : tcgsize;
  255. ref : treference;
  256. begin
  257. if use_push(cgpara) then
  258. begin
  259. cgpara.check_simple_location;
  260. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  261. pushsize:=cgpara.location^.size
  262. else
  263. pushsize:=int_cgsize(cgpara.alignment);
  264. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  265. ref.direction := dir_dec;
  266. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[pushsize],a,ref));
  267. end
  268. else
  269. inherited a_load_const_cgpara(list,size,a,cgpara);
  270. end;
  271. procedure tcg68k.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  272. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  273. var
  274. pushsize : tcgsize;
  275. tmpreg : tregister;
  276. href : treference;
  277. ref : treference;
  278. begin
  279. if not assigned(paraloc) then
  280. exit;
  281. { TODO: FIX ME!!! this also triggers location bug }
  282. {if (paraloc^.loc<>LOC_REFERENCE) or
  283. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  284. (tcgsize2size[paraloc^.size]>sizeof(tcgint)) then
  285. internalerror(200501162);}
  286. { Pushes are needed in reverse order, add the size of the
  287. current location to the offset where to load from. This
  288. prevents wrong calculations for the last location when
  289. the size is not a power of 2 }
  290. if assigned(paraloc^.next) then
  291. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  292. { Push the data starting at ofs }
  293. href:=r;
  294. inc(href.offset,ofs);
  295. fixref(list,href);
  296. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  297. pushsize:=paraloc^.size
  298. else
  299. pushsize:=int_cgsize(cgpara.alignment);
  300. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, tcgsize2size[pushsize]);
  301. ref.direction := dir_dec;
  302. if tcgsize2size[paraloc^.size]<cgpara.alignment then
  303. begin
  304. tmpreg:=getintregister(list,pushsize);
  305. a_load_ref_reg(list,paraloc^.size,pushsize,href,tmpreg);
  306. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],tmpreg,ref));
  307. end
  308. else
  309. list.concat(taicpu.op_ref_ref(A_MOVE,tcgsize2opsize[pushsize],href,ref));
  310. end;
  311. var
  312. len : tcgint;
  313. href : treference;
  314. begin
  315. { cgpara.size=OS_NO requires a copy on the stack }
  316. if use_push(cgpara) then
  317. begin
  318. { Record copy? }
  319. if (cgpara.size in [OS_NO,OS_F64]) or (size=OS_NO) then
  320. begin
  321. cgpara.check_simple_location;
  322. len:=align(cgpara.intsize,cgpara.alignment);
  323. g_stackpointer_alloc(list,len);
  324. reference_reset_base(href,NR_STACK_POINTER_REG,0,cgpara.alignment);
  325. g_concatcopy(list,r,href,len);
  326. end
  327. else
  328. begin
  329. if tcgsize2size[cgpara.size]<>tcgsize2size[size] then
  330. internalerror(200501161);
  331. { We need to push the data in reverse order,
  332. therefor we use a recursive algorithm }
  333. pushdata(cgpara.location,0);
  334. end
  335. end
  336. else
  337. inherited a_load_ref_cgpara(list,size,r,cgpara);
  338. end;
  339. procedure tcg68k.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);
  340. var
  341. tmpref : treference;
  342. begin
  343. { 68k always passes arguments on the stack }
  344. if use_push(cgpara) then
  345. begin
  346. //list.concat(tai_comment.create(strpnew('a_loadaddr_ref_cgpara: PEA')));
  347. cgpara.check_simple_location;
  348. tmpref:=r;
  349. fixref(list,tmpref);
  350. list.concat(taicpu.op_ref(A_PEA,S_NO,tmpref));
  351. end
  352. else
  353. inherited a_loadaddr_ref_cgpara(list,r,cgpara);
  354. end;
  355. function tcg68k.fixref(list: TAsmList; var ref: treference): boolean;
  356. var
  357. hreg,idxreg : tregister;
  358. href : treference;
  359. instr : taicpu;
  360. scale : aint;
  361. begin
  362. result:=false;
  363. { The MC68020+ has extended
  364. addressing capabilities with a 32-bit
  365. displacement.
  366. }
  367. { first ensure that base is an address register }
  368. if ((ref.base<>NR_NO) and (ref.index<>NR_NO)) and
  369. (not isaddressregister(ref.base) and isaddressregister(ref.index)) and
  370. (ref.scalefactor < 2) then
  371. begin
  372. { if we have both base and index registers, but base is data and index
  373. is address, we can just swap them, as FPC always uses long index.
  374. but we can only do this, if the index has no scalefactor }
  375. hreg:=ref.base;
  376. ref.base:=ref.index;
  377. ref.index:=hreg;
  378. //list.concat(tai_comment.create(strpnew('fixref: base and index swapped')));
  379. end;
  380. if (not assigned (ref.symbol) and (current_settings.cputype<>cpu_MC68000)) and
  381. (ref.base<>NR_NO) and not isaddressregister(ref.base) then
  382. begin
  383. hreg:=getaddressregister(list);
  384. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  385. add_move_instruction(instr);
  386. list.concat(instr);
  387. fixref:=true;
  388. ref.base:=hreg;
  389. end;
  390. if (current_settings.cputype=cpu_MC68020) then
  391. exit;
  392. { ToDo: check which constraints of Coldfire also apply to MC68000 }
  393. case current_settings.cputype of
  394. cpu_MC68000:
  395. begin
  396. if (ref.base<>NR_NO) then
  397. begin
  398. if (ref.index<>NR_NO) and assigned(ref.symbol) then
  399. begin
  400. hreg:=getaddressregister(list);
  401. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  402. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.index,hreg));
  403. ref.index:=NR_NO;
  404. ref.base:=hreg;
  405. end;
  406. { base + reg }
  407. if ref.index <> NR_NO then
  408. begin
  409. { base + reg + offset }
  410. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  411. begin
  412. hreg:=getaddressregister(list);
  413. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  414. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  415. fixref:=true;
  416. ref.offset:=0;
  417. ref.base:=hreg;
  418. exit;
  419. end;
  420. end
  421. else
  422. { base + offset }
  423. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  424. begin
  425. hreg:=getaddressregister(list);
  426. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  427. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  428. fixref:=true;
  429. ref.offset:=0;
  430. ref.base:=hreg;
  431. exit;
  432. end;
  433. if assigned(ref.symbol) then
  434. begin
  435. hreg:=getaddressregister(list);
  436. idxreg:=ref.base;
  437. ref.base:=NR_NO;
  438. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  439. reference_reset_base(ref,hreg,0,ref.alignment);
  440. fixref:=true;
  441. ref.index:=idxreg;
  442. end
  443. else if not isaddressregister(ref.base) then
  444. begin
  445. hreg:=getaddressregister(list);
  446. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  447. //add_move_instruction(instr);
  448. list.concat(instr);
  449. fixref:=true;
  450. ref.base:=hreg;
  451. end;
  452. end
  453. else
  454. { Note: symbol -> ref would be supported as long as ref does not
  455. contain a offset or index... (maybe something for the
  456. optimizer) }
  457. if Assigned(ref.symbol) and (ref.index<>NR_NO) then
  458. begin
  459. hreg:=cg.getaddressregister(list);
  460. idxreg:=ref.index;
  461. ref.index:=NR_NO;
  462. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  463. reference_reset_base(ref,hreg,0,ref.alignment);
  464. ref.index:=idxreg;
  465. fixref:=true;
  466. end;
  467. end;
  468. cpu_isa_a,
  469. cpu_isa_a_p,
  470. cpu_isa_b,
  471. cpu_isa_c:
  472. begin
  473. if (ref.base<>NR_NO) then
  474. begin
  475. if assigned(ref.symbol) then
  476. begin
  477. //list.concat(tai_comment.create(strpnew('fixref: symbol')));
  478. hreg:=cg.getaddressregister(list);
  479. reference_reset_symbol(href,ref.symbol,ref.offset,ref.alignment);
  480. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  481. if ref.index<>NR_NO then
  482. begin
  483. { fold the symbol + offset into the base, not the base into the index,
  484. because that might screw up the scalefactor of the reference }
  485. //list.concat(tai_comment.create(strpnew('fixref: symbol + offset (index + base)')));
  486. idxreg:=getaddressregister(list);
  487. reference_reset_base(href,ref.base,0,ref.alignment);
  488. href.index:=hreg;
  489. hreg:=getaddressregister(list);
  490. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  491. ref.base:=hreg;
  492. end
  493. else
  494. ref.index:=hreg;
  495. ref.offset:=0;
  496. ref.symbol:=nil;
  497. fixref:=true;
  498. end
  499. else
  500. { base + reg }
  501. if ref.index <> NR_NO then
  502. begin
  503. { base + reg + offset }
  504. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  505. begin
  506. hreg:=getaddressregister(list);
  507. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  508. begin
  509. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  510. //add_move_instruction(instr);
  511. list.concat(instr);
  512. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  513. end
  514. else
  515. begin
  516. //list.concat(tai_comment.create(strpnew('fixref: base + reg + offset lea')));
  517. reference_reset_base(href,ref.base,ref.offset,ref.alignment);
  518. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,href,hreg));
  519. end;
  520. fixref:=true;
  521. ref.base:=hreg;
  522. ref.offset:=0;
  523. exit;
  524. end;
  525. end
  526. else
  527. { base + offset }
  528. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  529. begin
  530. hreg:=getaddressregister(list);
  531. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  532. //add_move_instruction(instr);
  533. list.concat(instr);
  534. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  535. fixref:=true;
  536. ref.offset:=0;
  537. ref.base:=hreg;
  538. exit;
  539. end;
  540. end
  541. else
  542. { Note: symbol -> ref would be supported as long as ref does not
  543. contain a offset or index... (maybe something for the
  544. optimizer) }
  545. if Assigned(ref.symbol) {and (ref.index<>NR_NO)} then
  546. begin
  547. hreg:=cg.getaddressregister(list);
  548. idxreg:=ref.index;
  549. scale:=ref.scalefactor;
  550. ref.index:=NR_NO;
  551. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  552. reference_reset_base(ref,hreg,0,ref.alignment);
  553. ref.index:=idxreg;
  554. ref.scalefactor:=scale;
  555. fixref:=true;
  556. end;
  557. end;
  558. end;
  559. end;
  560. procedure tcg68k.call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  561. var
  562. paraloc1,paraloc2,paraloc3 : tcgpara;
  563. pd : tprocdef;
  564. begin
  565. pd:=search_system_proc(name);
  566. paraloc1.init;
  567. paraloc2.init;
  568. paraloc3.init;
  569. paramanager.getintparaloc(pd,1,paraloc1);
  570. paramanager.getintparaloc(pd,2,paraloc2);
  571. paramanager.getintparaloc(pd,3,paraloc3);
  572. a_load_const_cgpara(list,OS_8,0,paraloc3);
  573. a_load_const_cgpara(list,size,a,paraloc2);
  574. a_load_reg_cgpara(list,OS_32,reg,paraloc1);
  575. paramanager.freecgpara(list,paraloc3);
  576. paramanager.freecgpara(list,paraloc2);
  577. paramanager.freecgpara(list,paraloc1);
  578. alloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  579. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  580. a_call_name(list,name,false);
  581. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  582. dealloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  583. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  584. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg);
  585. paraloc3.done;
  586. paraloc2.done;
  587. paraloc1.done;
  588. end;
  589. procedure tcg68k.call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  590. var
  591. paraloc1,paraloc2,paraloc3 : tcgpara;
  592. pd : tprocdef;
  593. begin
  594. pd:=search_system_proc(name);
  595. paraloc1.init;
  596. paraloc2.init;
  597. paraloc3.init;
  598. paramanager.getintparaloc(pd,1,paraloc1);
  599. paramanager.getintparaloc(pd,2,paraloc2);
  600. paramanager.getintparaloc(pd,3,paraloc3);
  601. a_load_const_cgpara(list,OS_8,0,paraloc3);
  602. a_load_reg_cgpara(list,OS_32,reg1,paraloc2);
  603. a_load_reg_cgpara(list,OS_32,reg2,paraloc1);
  604. paramanager.freecgpara(list,paraloc3);
  605. paramanager.freecgpara(list,paraloc2);
  606. paramanager.freecgpara(list,paraloc1);
  607. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  608. alloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  609. a_call_name(list,name,false);
  610. dealloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  611. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  612. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  613. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg2);
  614. paraloc3.done;
  615. paraloc2.done;
  616. paraloc1.done;
  617. end;
  618. procedure tcg68k.a_call_name(list : TAsmList;const s : string; weak: boolean);
  619. var
  620. sym: tasmsymbol;
  621. begin
  622. if not(weak) then
  623. sym:=current_asmdata.RefAsmSymbol(s)
  624. else
  625. sym:=current_asmdata.WeakRefAsmSymbol(s);
  626. list.concat(taicpu.op_sym(A_JSR,S_NO,sym));
  627. end;
  628. procedure tcg68k.a_call_reg(list : TAsmList;reg: tregister);
  629. var
  630. tmpref : treference;
  631. tmpreg : tregister;
  632. instr : taicpu;
  633. begin
  634. if isaddressregister(reg) then
  635. begin
  636. { if we have an address register, we can jump to the address directly }
  637. reference_reset_base(tmpref,reg,0,4);
  638. end
  639. else
  640. begin
  641. { if we have a data register, we need to move it to an address register first }
  642. tmpreg:=getaddressregister(list);
  643. reference_reset_base(tmpref,tmpreg,0,4);
  644. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,tmpreg);
  645. add_move_instruction(instr);
  646. list.concat(instr);
  647. end;
  648. list.concat(taicpu.op_ref(A_JSR,S_NO,tmpref));
  649. end;
  650. procedure tcg68k.a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);
  651. var
  652. opsize: topsize;
  653. begin
  654. opsize:=tcgsize2opsize[size];
  655. if isaddressregister(register) then
  656. begin
  657. { an m68k manual I have recommends SUB Ax,Ax to be used instead of CLR for address regs }
  658. if a = 0 then
  659. list.concat(taicpu.op_reg_reg(A_SUB,S_L,register,register))
  660. else
  661. { ISA B/C Coldfire has MOV3Q which can move -1 or 1..7 to any reg }
  662. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c]) and
  663. ((longint(a) = -1) or ((longint(a) > 0) and (longint(a) < 8))) then
  664. list.concat(taicpu.op_const_reg(A_MOV3Q,S_L,longint(a),register))
  665. else
  666. { We don't have to specify the size here, the assembler will decide the size of
  667. the operand it needs. If this ends up as a MOVEA.W, that will sign extend the
  668. value in the dest. reg to full 32 bits (specific to Ax regs only) }
  669. list.concat(taicpu.op_const_reg(A_MOVEA,S_NO,longint(a),register));
  670. end
  671. else
  672. if a = 0 then
  673. list.concat(taicpu.op_reg(A_CLR,S_L,register))
  674. else
  675. begin
  676. { Prefer MOV3Q if applicable, it allows replacement spilling for register }
  677. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c]) and
  678. ((longint(a)=-1) or ((longint(a)>0) and (longint(a)<8))) then
  679. list.concat(taicpu.op_const_reg(A_MOV3Q,S_L,longint(a),register))
  680. else if (longint(a) >= low(shortint)) and (longint(a) <= high(shortint)) then
  681. list.concat(taicpu.op_const_reg(A_MOVEQ,S_L,longint(a),register))
  682. else
  683. begin
  684. { ISA B/C Coldfire has sign extend/zero extend moves }
  685. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c]) and
  686. (size in [OS_16, OS_8, OS_S16, OS_S8]) and
  687. ((longint(a) >= low(smallint)) and (longint(a) <= high(smallint))) then
  688. begin
  689. if size in [OS_16, OS_8] then
  690. list.concat(taicpu.op_const_reg(A_MVZ,opsize,longint(a),register))
  691. else
  692. list.concat(taicpu.op_const_reg(A_MVS,opsize,longint(a),register));
  693. end
  694. else
  695. begin
  696. { clear the register first, for unsigned and positive values, so
  697. we don't need to zero extend after }
  698. if (size in [OS_16,OS_8]) or
  699. ((size in [OS_S16,OS_S8]) and (a > 0)) then
  700. list.concat(taicpu.op_reg(A_CLR,S_L,register));
  701. list.concat(taicpu.op_const_reg(A_MOVE,opsize,longint(a),register));
  702. { only sign extend if we need to, zero extension is not necessary because the CLR.L above }
  703. if (size in [OS_S16,OS_S8]) and (a < 0) then
  704. sign_extend(list,size,register);
  705. end;
  706. end;
  707. end;
  708. end;
  709. procedure tcg68k.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  710. var
  711. hreg : tregister;
  712. href : treference;
  713. begin
  714. a:=longint(a);
  715. href:=ref;
  716. fixref(list,href);
  717. if (a=0) then
  718. list.concat(taicpu.op_ref(A_CLR,tcgsize2opsize[tosize],href))
  719. else if (tcgsize2opsize[tosize]=S_L) and
  720. (current_settings.cputype in [cpu_isa_b,cpu_isa_c]) and
  721. ((a=-1) or ((a>0) and (a<8))) then
  722. list.concat(taicpu.op_const_ref(A_MOV3Q,S_L,a,href))
  723. { for coldfire we need to go through a temporary register if we have a
  724. offset, index or symbol given }
  725. else if (current_settings.cputype in cpu_coldfire) and
  726. (
  727. (href.offset<>0) or
  728. { TODO : check whether we really need this second condition }
  729. (href.index<>NR_NO) or
  730. assigned(href.symbol)
  731. ) then
  732. begin
  733. hreg:=getintregister(list,tosize);
  734. a_load_const_reg(list,tosize,a,hreg);
  735. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[tosize],hreg,href));
  736. end
  737. else
  738. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[tosize],longint(a),href));
  739. end;
  740. procedure tcg68k.a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  741. var
  742. href : treference;
  743. begin
  744. href := ref;
  745. fixref(list,href);
  746. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  747. a_load_reg_reg(list,fromsize,tosize,register,register);
  748. { move to destination reference }
  749. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],register,href));
  750. end;
  751. procedure tcg68k.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  752. var
  753. aref: treference;
  754. bref: treference;
  755. tmpref : treference;
  756. dofix : boolean;
  757. hreg: TRegister;
  758. begin
  759. aref := sref;
  760. bref := dref;
  761. fixref(list,aref);
  762. fixref(list,bref);
  763. if TCGSize2OpSize[fromsize]<>TCGSize2OpSize[tosize] then
  764. begin
  765. { if we need to change the size then always use a temporary
  766. register }
  767. hreg:=getintregister(list,fromsize);
  768. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[fromsize],aref,hreg));
  769. sign_extend(list,fromsize,tosize,hreg);
  770. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],hreg,bref));
  771. exit;
  772. end;
  773. { Coldfire dislikes certain move combinations }
  774. if current_settings.cputype in cpu_coldfire then
  775. begin
  776. { TODO : move.b/w only allowed in newer coldfires... (ISA_B+) }
  777. dofix:=false;
  778. if { (d16,Ax) and (d8,Ax,Xi) }
  779. (
  780. (aref.base<>NR_NO) and
  781. (
  782. (aref.index<>NR_NO) or
  783. (aref.offset<>0)
  784. )
  785. ) or
  786. { (xxx) }
  787. assigned(aref.symbol) then
  788. begin
  789. if aref.index<>NR_NO then
  790. begin
  791. dofix:={ (d16,Ax) and (d8,Ax,Xi) }
  792. (
  793. (bref.base<>NR_NO) and
  794. (
  795. (bref.index<>NR_NO) or
  796. (bref.offset<>0)
  797. )
  798. ) or
  799. { (xxx) }
  800. assigned(bref.symbol);
  801. end
  802. else
  803. { offset <> 0, but no index }
  804. begin
  805. dofix:={ (d8,Ax,Xi) }
  806. (
  807. (bref.base<>NR_NO) and
  808. (bref.index<>NR_NO)
  809. ) or
  810. { (xxx) }
  811. assigned(bref.symbol);
  812. end;
  813. end;
  814. if dofix then
  815. begin
  816. hreg:=getaddressregister(list);
  817. reference_reset_base(tmpref,hreg,0,0);
  818. list.concat(taicpu.op_ref_reg(A_LEA,S_L,aref,hreg));
  819. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],tmpref,bref));
  820. exit;
  821. end;
  822. end;
  823. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],aref,bref));
  824. end;
  825. procedure tcg68k.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  826. var
  827. instr : taicpu;
  828. begin
  829. { move to destination register }
  830. instr:=taicpu.op_reg_reg(A_MOVE,TCGSize2OpSize[fromsize],reg1,reg2);
  831. add_move_instruction(instr);
  832. list.concat(instr);
  833. sign_extend(list, fromsize, reg2);
  834. end;
  835. procedure tcg68k.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  836. var
  837. href : treference;
  838. size : tcgsize;
  839. begin
  840. href:=ref;
  841. fixref(list,href);
  842. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  843. size:=fromsize
  844. else
  845. size:=tosize;
  846. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[size],href,register));
  847. { extend the value in the register }
  848. sign_extend(list, size, register);
  849. end;
  850. procedure tcg68k.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  851. var
  852. href : treference;
  853. begin
  854. href:=ref;
  855. fixref(list, href);
  856. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,r));
  857. end;
  858. procedure tcg68k.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  859. var
  860. instr : taicpu;
  861. begin
  862. { in emulation mode, only 32-bit single is supported }
  863. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  864. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg1,reg2)
  865. else
  866. instr:=taicpu.op_reg_reg(A_FMOVE,tcgsize2opsize[tosize],reg1,reg2);
  867. add_move_instruction(instr);
  868. list.concat(instr);
  869. end;
  870. procedure tcg68k.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  871. var
  872. opsize : topsize;
  873. href : treference;
  874. begin
  875. opsize := tcgsize2opsize[fromsize];
  876. { extended is not supported, since it is not available on Coldfire }
  877. if opsize = S_FX then
  878. internalerror(20020729);
  879. href := ref;
  880. fixref(list,href);
  881. { in emulation mode, only 32-bit single is supported }
  882. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  883. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,reg))
  884. else
  885. begin
  886. list.concat(taicpu.op_ref_reg(A_FMOVE,opsize,href,reg));
  887. if (tosize < fromsize) then
  888. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  889. end;
  890. end;
  891. procedure tcg68k.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  892. var
  893. opsize : topsize;
  894. begin
  895. opsize := tcgsize2opsize[tosize];
  896. { extended is not supported, since it is not available on Coldfire }
  897. if opsize = S_FX then
  898. internalerror(20020729);
  899. { in emulation mode, only 32-bit single is supported }
  900. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  901. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,reg, ref))
  902. else
  903. list.concat(taicpu.op_reg_ref(A_FMOVE,opsize,reg, ref));
  904. end;
  905. procedure tcg68k.a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);
  906. begin
  907. case cgpara.location^.loc of
  908. LOC_REFERENCE,LOC_CREFERENCE:
  909. begin
  910. case size of
  911. OS_F64:
  912. cg64.a_load64_ref_cgpara(list,ref,cgpara);
  913. OS_F32:
  914. a_load_ref_cgpara(list,size,ref,cgpara);
  915. else
  916. internalerror(2013021201);
  917. end;
  918. end;
  919. else
  920. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  921. end;
  922. end;
  923. procedure tcg68k.a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  924. var
  925. scratch_reg : tregister;
  926. scratch_reg2: tregister;
  927. opcode : tasmop;
  928. begin
  929. optimize_op_const(size, op, a);
  930. opcode := topcg2tasmop[op];
  931. case op of
  932. OP_NONE :
  933. begin
  934. { Opcode is optimized away }
  935. end;
  936. OP_MOVE :
  937. begin
  938. { Optimized, replaced with a simple load }
  939. a_load_const_reg(list,size,a,reg);
  940. end;
  941. OP_ADD,
  942. OP_SUB:
  943. begin
  944. { add/sub works the same way, so have it unified here }
  945. if (a >= 1) and (a <= 8) then
  946. if (op = OP_ADD) then
  947. opcode:=A_ADDQ
  948. else
  949. opcode:=A_SUBQ;
  950. list.concat(taicpu.op_const_reg(opcode, S_L, a, reg));
  951. end;
  952. OP_AND,
  953. OP_OR,
  954. OP_XOR:
  955. begin
  956. scratch_reg := force_to_dataregister(list, size, reg);
  957. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  958. move_if_needed(list, size, scratch_reg, reg);
  959. end;
  960. OP_DIV,
  961. OP_IDIV:
  962. begin
  963. internalerror(20020816);
  964. end;
  965. OP_MUL,
  966. OP_IMUL:
  967. begin
  968. { NOTE: better have this as fast as possible on every CPU in all cases,
  969. because the compiler uses OP_IMUL for array indexing... (KB) }
  970. { ColdFire doesn't support MULS/MULU <imm>,dX }
  971. if current_settings.cputype in cpu_coldfire then
  972. begin
  973. { move const to a register first }
  974. scratch_reg := getintregister(list,OS_INT);
  975. a_load_const_reg(list, size, a, scratch_reg);
  976. { do the multiplication }
  977. scratch_reg2 := force_to_dataregister(list, size, reg);
  978. sign_extend(list, size, scratch_reg2);
  979. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg,scratch_reg2));
  980. { move the value back to the original register }
  981. move_if_needed(list, size, scratch_reg2, reg);
  982. end
  983. else
  984. begin
  985. if current_settings.cputype = cpu_mc68020 then
  986. begin
  987. { do the multiplication }
  988. scratch_reg := force_to_dataregister(list, size, reg);
  989. sign_extend(list, size, scratch_reg);
  990. list.concat(taicpu.op_const_reg(opcode,S_L,a,scratch_reg));
  991. { move the value back to the original register }
  992. move_if_needed(list, size, scratch_reg, reg);
  993. end
  994. else
  995. { Fallback branch, plain 68000 for now }
  996. { FIX ME: this is slow as hell, but original 68000 doesn't have 32x32 -> 32bit MUL (KB) }
  997. if op = OP_MUL then
  998. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_dword')
  999. else
  1000. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_longint');
  1001. end;
  1002. end;
  1003. OP_ROL,
  1004. OP_ROR,
  1005. OP_SAR,
  1006. OP_SHL,
  1007. OP_SHR :
  1008. begin
  1009. scratch_reg := force_to_dataregister(list, size, reg);
  1010. sign_extend(list, size, scratch_reg);
  1011. if (a >= 1) and (a <= 8) then
  1012. begin
  1013. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1014. end
  1015. else
  1016. begin
  1017. { move const to a register first }
  1018. scratch_reg2 := getintregister(list,OS_INT);
  1019. a_load_const_reg(list, size, a, scratch_reg2);
  1020. { do the operation }
  1021. list.concat(taicpu.op_reg_reg(opcode, S_L, scratch_reg2, scratch_reg));
  1022. end;
  1023. { move the value back to the original register }
  1024. move_if_needed(list, size, scratch_reg, reg);
  1025. end;
  1026. else
  1027. internalerror(20020729);
  1028. end;
  1029. end;
  1030. procedure tcg68k.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1031. var
  1032. opcode: tasmop;
  1033. opsize: topsize;
  1034. href : treference;
  1035. begin
  1036. optimize_op_const(size, op, a);
  1037. opcode := topcg2tasmop[op];
  1038. opsize := TCGSize2OpSize[size];
  1039. { on ColdFire all arithmetic operations are only possible on 32bit }
  1040. if ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)
  1041. and not (op in [OP_NONE,OP_MOVE])) then
  1042. begin
  1043. inherited;
  1044. exit;
  1045. end;
  1046. case op of
  1047. OP_NONE :
  1048. begin
  1049. { opcode was optimized away }
  1050. end;
  1051. OP_MOVE :
  1052. begin
  1053. { Optimized, replaced with a simple load }
  1054. a_load_const_ref(list,size,a,ref);
  1055. end;
  1056. OP_ADD,
  1057. OP_SUB :
  1058. begin
  1059. href:=ref;
  1060. fixref(list,href);
  1061. { add/sub works the same way, so have it unified here }
  1062. if (a >= 1) and (a <= 8) then
  1063. begin
  1064. if (op = OP_ADD) then
  1065. opcode:=A_ADDQ
  1066. else
  1067. opcode:=A_SUBQ;
  1068. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1069. end
  1070. else
  1071. if not(current_settings.cputype in cpu_coldfire) then
  1072. list.concat(taicpu.op_const_ref(opcode, opsize, a, href))
  1073. else
  1074. { on ColdFire, ADDI/SUBI cannot act on memory
  1075. so we can only go through a register }
  1076. inherited;
  1077. end;
  1078. else begin
  1079. // list.concat(tai_comment.create(strpnew('a_op_const_ref inherited')));
  1080. inherited;
  1081. end;
  1082. end;
  1083. end;
  1084. procedure tcg68k.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1085. var
  1086. hreg1, hreg2: tregister;
  1087. opcode : tasmop;
  1088. opsize : topsize;
  1089. begin
  1090. opcode := topcg2tasmop[op];
  1091. if current_settings.cputype in cpu_coldfire then
  1092. opsize := S_L
  1093. else
  1094. opsize := TCGSize2OpSize[size];
  1095. case op of
  1096. OP_ADD,
  1097. OP_SUB:
  1098. begin
  1099. if current_settings.cputype in cpu_coldfire then
  1100. begin
  1101. { operation only allowed only a longword }
  1102. sign_extend(list, size, src);
  1103. sign_extend(list, size, dst);
  1104. end;
  1105. list.concat(taicpu.op_reg_reg(opcode, opsize, src, dst));
  1106. end;
  1107. OP_AND,OP_OR,
  1108. OP_SAR,OP_SHL,
  1109. OP_SHR,OP_XOR:
  1110. begin
  1111. { load to data registers }
  1112. hreg1 := force_to_dataregister(list, size, src);
  1113. hreg2 := force_to_dataregister(list, size, dst);
  1114. if current_settings.cputype in cpu_coldfire then
  1115. begin
  1116. { operation only allowed only a longword }
  1117. {!***************************************
  1118. in the case of shifts, the value to
  1119. shift by, should already be valid, so
  1120. no need to sign extend the value
  1121. !
  1122. }
  1123. if op in [OP_AND,OP_OR,OP_XOR] then
  1124. sign_extend(list, size, hreg1);
  1125. sign_extend(list, size, hreg2);
  1126. end;
  1127. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1128. { move back result into destination register }
  1129. move_if_needed(list, size, hreg2, dst);
  1130. end;
  1131. OP_DIV,
  1132. OP_IDIV :
  1133. begin
  1134. internalerror(20020816);
  1135. end;
  1136. OP_MUL,
  1137. OP_IMUL:
  1138. begin
  1139. if (current_settings.cputype <> cpu_mc68020) and
  1140. (not (current_settings.cputype in cpu_coldfire)) then
  1141. if op = OP_MUL then
  1142. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_dword')
  1143. else
  1144. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_longint')
  1145. else
  1146. begin
  1147. { 68020+ and ColdFire codepath, probably could be improved }
  1148. hreg1 := force_to_dataregister(list, size, src);
  1149. hreg2 := force_to_dataregister(list, size, dst);
  1150. sign_extend(list, size, hreg1);
  1151. sign_extend(list, size, hreg2);
  1152. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1153. { move back result into destination register }
  1154. move_if_needed(list, size, hreg2, dst);
  1155. end;
  1156. end;
  1157. OP_NEG,
  1158. OP_NOT :
  1159. begin
  1160. { if there are two operands, move the register,
  1161. since the operation will only be done on the result
  1162. register. }
  1163. if (src<>dst) then
  1164. a_load_reg_reg(list,size,size,src,dst);
  1165. hreg2 := force_to_dataregister(list, size, dst);
  1166. { coldfire only supports long version }
  1167. if current_settings.cputype in cpu_ColdFire then
  1168. sign_extend(list, size, hreg2);
  1169. list.concat(taicpu.op_reg(opcode, opsize, hreg2));
  1170. { move back the result to the result register if needed }
  1171. move_if_needed(list, size, hreg2, dst);
  1172. end;
  1173. else
  1174. internalerror(20020729);
  1175. end;
  1176. end;
  1177. procedure tcg68k.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference);
  1178. var
  1179. opcode : tasmop;
  1180. opsize : topsize;
  1181. href : treference;
  1182. begin
  1183. opcode := topcg2tasmop[op];
  1184. opsize := TCGSize2OpSize[size];
  1185. { on ColdFire all arithmetic operations are only possible on 32bit
  1186. and addressing modes are limited }
  1187. if ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)) then
  1188. begin
  1189. inherited;
  1190. exit;
  1191. end;
  1192. case op of
  1193. OP_ADD,
  1194. OP_SUB :
  1195. begin
  1196. href:=ref;
  1197. fixref(list,href);
  1198. { add/sub works the same way, so have it unified here }
  1199. list.concat(taicpu.op_reg_ref(opcode, opsize, reg, href));
  1200. end;
  1201. else begin
  1202. // list.concat(tai_comment.create(strpnew('a_op_reg_ref inherited')));
  1203. inherited;
  1204. end;
  1205. end;
  1206. end;
  1207. procedure tcg68k.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1208. l : tasmlabel);
  1209. var
  1210. hregister : tregister;
  1211. instr : taicpu;
  1212. need_temp_reg : boolean;
  1213. temp_size: topsize;
  1214. begin
  1215. need_temp_reg := false;
  1216. { plain 68000 doesn't support address registers for TST }
  1217. need_temp_reg := (current_settings.cputype = cpu_mc68000) and
  1218. (a = 0) and isaddressregister(reg);
  1219. { ColdFire doesn't support address registers for CMPI }
  1220. need_temp_reg := need_temp_reg or ((current_settings.cputype in cpu_coldfire)
  1221. and (a <> 0) and isaddressregister(reg));
  1222. if need_temp_reg then
  1223. begin
  1224. hregister := getintregister(list,OS_INT);
  1225. temp_size := TCGSize2OpSize[size];
  1226. if temp_size < S_W then
  1227. temp_size := S_W;
  1228. instr:=taicpu.op_reg_reg(A_MOVE,temp_size,reg,hregister);
  1229. add_move_instruction(instr);
  1230. list.concat(instr);
  1231. reg := hregister;
  1232. { do sign extension if size had to be modified }
  1233. if temp_size <> TCGSize2OpSize[size] then
  1234. begin
  1235. sign_extend(list, size, reg);
  1236. size:=OS_INT;
  1237. end;
  1238. end;
  1239. if a = 0 then
  1240. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg))
  1241. else
  1242. begin
  1243. { ColdFire ISA A also needs S_L for CMPI }
  1244. { Note: older QEMU pukes from CMPI sizes <> .L even on ISA B/C, but
  1245. it's actually *LEGAL*, see CFPRM, page 4-30, the bug also seems
  1246. fixed in recent QEMU, but only when CPU cfv4e is forced, not by
  1247. default. (KB) }
  1248. if current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c]} then
  1249. begin
  1250. sign_extend(list, size, reg);
  1251. size:=OS_INT;
  1252. end;
  1253. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  1254. end;
  1255. { emit the actual jump to the label }
  1256. a_jmp_cond(list,cmp_op,l);
  1257. end;
  1258. procedure tcg68k.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel);
  1259. var
  1260. tmpref: treference;
  1261. begin
  1262. { optimize for usage of TST here, so ref compares against zero, which is the
  1263. most common case by far in the RTL code at least (KB) }
  1264. if (a = 0) then
  1265. begin
  1266. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label with TST')));
  1267. tmpref:=ref;
  1268. fixref(list,tmpref);
  1269. list.concat(taicpu.op_ref(A_TST,tcgsize2opsize[size],tmpref));
  1270. a_jmp_cond(list,cmp_op,l);
  1271. end
  1272. else
  1273. begin
  1274. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label inherited')));
  1275. inherited;
  1276. end;
  1277. end;
  1278. procedure tcg68k.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1279. begin
  1280. if (current_settings.cputype in cpu_coldfire-[cpu_isa_b,cpu_isa_c]) then
  1281. begin
  1282. sign_extend(list,size,reg1);
  1283. sign_extend(list,size,reg2);
  1284. size:=OS_INT;
  1285. end;
  1286. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  1287. { emit the actual jump to the label }
  1288. a_jmp_cond(list,cmp_op,l);
  1289. end;
  1290. procedure tcg68k.a_jmp_name(list: TAsmList; const s: string);
  1291. var
  1292. ai: taicpu;
  1293. begin
  1294. ai := Taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s));
  1295. ai.is_jmp := true;
  1296. list.concat(ai);
  1297. end;
  1298. procedure tcg68k.a_jmp_always(list : TAsmList;l: tasmlabel);
  1299. var
  1300. ai: taicpu;
  1301. begin
  1302. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  1303. ai.is_jmp := true;
  1304. list.concat(ai);
  1305. end;
  1306. procedure tcg68k.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1307. var
  1308. ai : taicpu;
  1309. begin
  1310. ai := Taicpu.op_sym(A_BXX,S_NO,l);
  1311. ai.SetCondition(flags_to_cond(f));
  1312. ai.is_jmp := true;
  1313. list.concat(ai);
  1314. end;
  1315. procedure tcg68k.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1316. var
  1317. ai : taicpu;
  1318. hreg : tregister;
  1319. instr : taicpu;
  1320. begin
  1321. { move to a Dx register? }
  1322. if (isaddressregister(reg)) then
  1323. hreg:=getintregister(list,OS_INT)
  1324. else
  1325. hreg:=reg;
  1326. ai:=Taicpu.Op_reg(A_Sxx,S_B,hreg);
  1327. ai.SetCondition(flags_to_cond(f));
  1328. list.concat(ai);
  1329. { Scc stores a complete byte of 1s, but the compiler expects only one
  1330. bit set, so ensure this is the case }
  1331. list.concat(taicpu.op_const_reg(A_AND,S_L,1,hreg));
  1332. if hreg<>reg then
  1333. begin
  1334. instr:=taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg);
  1335. add_move_instruction(instr);
  1336. list.concat(instr);
  1337. end;
  1338. end;
  1339. procedure tcg68k.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1340. var
  1341. helpsize : longint;
  1342. i : byte;
  1343. hregister : tregister;
  1344. iregister : tregister;
  1345. jregister : tregister;
  1346. hp1 : treference;
  1347. hp2 : treference;
  1348. hl : tasmlabel;
  1349. srcref,dstref : treference;
  1350. orglen : tcgint;
  1351. begin
  1352. hregister := getintregister(list,OS_INT);
  1353. orglen:=len;
  1354. { from 12 bytes movs is being used }
  1355. if ((len<=8) or (not(cs_opt_size in current_settings.optimizerswitches) and (len<=12))) then
  1356. begin
  1357. srcref := source;
  1358. dstref := dest;
  1359. helpsize:=len div 4;
  1360. { move a dword x times }
  1361. for i:=1 to helpsize do
  1362. begin
  1363. a_load_ref_reg(list,OS_INT,OS_INT,srcref,hregister);
  1364. a_load_reg_ref(list,OS_INT,OS_INT,hregister,dstref);
  1365. inc(srcref.offset,4);
  1366. inc(dstref.offset,4);
  1367. dec(len,4);
  1368. end;
  1369. { move a word }
  1370. if len>1 then
  1371. begin
  1372. a_load_ref_reg(list,OS_16,OS_16,srcref,hregister);
  1373. a_load_reg_ref(list,OS_16,OS_16,hregister,dstref);
  1374. inc(srcref.offset,2);
  1375. inc(dstref.offset,2);
  1376. dec(len,2);
  1377. end;
  1378. { move a single byte }
  1379. if len>0 then
  1380. begin
  1381. a_load_ref_reg(list,OS_8,OS_8,srcref,hregister);
  1382. a_load_reg_ref(list,OS_8,OS_8,hregister,dstref);
  1383. end
  1384. end
  1385. else
  1386. begin
  1387. iregister:=getaddressregister(list);
  1388. jregister:=getaddressregister(list);
  1389. { reference for move (An)+,(An)+ }
  1390. reference_reset(hp1,source.alignment);
  1391. hp1.base := iregister; { source register }
  1392. hp1.direction := dir_inc;
  1393. reference_reset(hp2,dest.alignment);
  1394. hp2.base := jregister;
  1395. hp2.direction := dir_inc;
  1396. { iregister = source }
  1397. { jregister = destination }
  1398. a_loadaddr_ref_reg(list,source,iregister);
  1399. a_loadaddr_ref_reg(list,dest,jregister);
  1400. { double word move only on 68020+ machines }
  1401. { because of possible alignment problems }
  1402. { use fast loop mode }
  1403. if (current_settings.cputype=cpu_MC68020) then
  1404. begin
  1405. //list.concat(tai_comment.create(strpnew('g_concatcopy tight copy loop 020+')));
  1406. helpsize := len - len mod 4;
  1407. len := len mod 4;
  1408. a_load_const_reg(list,OS_INT,(helpsize div 4)-1,hregister);
  1409. current_asmdata.getjumplabel(hl);
  1410. a_label(list,hl);
  1411. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,hp1,hp2));
  1412. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1413. if len > 1 then
  1414. begin
  1415. dec(len,2);
  1416. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,hp1,hp2));
  1417. end;
  1418. if len = 1 then
  1419. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1420. end
  1421. else
  1422. begin
  1423. { Fast 68010 loop mode with no possible alignment problems }
  1424. //list.concat(tai_comment.create(strpnew('g_concatcopy tight byte copy loop')));
  1425. a_load_const_reg(list,OS_INT,len - 1,hregister);
  1426. current_asmdata.getjumplabel(hl);
  1427. a_label(list,hl);
  1428. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1429. if current_settings.cputype in cpu_coldfire then
  1430. begin
  1431. { Coldfire does not support DBRA }
  1432. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1433. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1434. end
  1435. else
  1436. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1437. end;
  1438. end;
  1439. end;
  1440. procedure tcg68k.g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef);
  1441. var
  1442. hl : tasmlabel;
  1443. ai : taicpu;
  1444. cond : TAsmCond;
  1445. begin
  1446. if not(cs_check_overflow in current_settings.localswitches) then
  1447. exit;
  1448. current_asmdata.getjumplabel(hl);
  1449. if not ((def.typ=pointerdef) or
  1450. ((def.typ=orddef) and
  1451. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1452. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1453. cond:=C_VC
  1454. else
  1455. cond:=C_CC;
  1456. ai:=Taicpu.Op_Sym(A_Bxx,S_NO,hl);
  1457. ai.SetCondition(cond);
  1458. ai.is_jmp:=true;
  1459. list.concat(ai);
  1460. a_call_name(list,'FPC_OVERFLOW',false);
  1461. a_label(list,hl);
  1462. end;
  1463. procedure tcg68k.g_proc_entry(list: TAsmList; localsize: longint; nostackframe:boolean);
  1464. begin
  1465. { Carl's original code used 2x MOVE instead of LINK when localsize = 0.
  1466. However, a LINK seems faster than two moves on everything from 68000
  1467. to '060, so the two move branch here was dropped. (KB) }
  1468. if not nostackframe then
  1469. begin
  1470. { size can't be negative }
  1471. if (localsize < 0) then
  1472. internalerror(2006122601);
  1473. if (localsize > high(smallint)) then
  1474. begin
  1475. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,0));
  1476. list.concat(taicpu.op_const_reg(A_SUBA,S_L,localsize,NR_STACK_POINTER_REG));
  1477. end
  1478. else
  1479. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,-localsize));
  1480. end;
  1481. end;
  1482. procedure tcg68k.g_proc_exit(list : TAsmList; parasize: longint; nostackframe: boolean);
  1483. var
  1484. r,hregister : TRegister;
  1485. ref : TReference;
  1486. ref2: TReference;
  1487. begin
  1488. if not nostackframe then
  1489. begin
  1490. list.concat(taicpu.op_reg(A_UNLK,S_NO,NR_FRAME_POINTER_REG));
  1491. { if parasize is less than zero here, we probably have a cdecl function.
  1492. According to the info here: http://www.makestuff.eu/wordpress/gcc-68000-abi/
  1493. 68k GCC uses two different methods to free the stack, depending if the target
  1494. architecture supports RTD or not, and one does callee side, the other does
  1495. caller side free, which looks like a PITA to support. We have to figure this
  1496. out later. More info welcomed. (KB) }
  1497. if (parasize > 0) and not (current_procinfo.procdef.proccalloption in clearstack_pocalls) then
  1498. begin
  1499. if current_settings.cputype=cpu_mc68020 then
  1500. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1501. else
  1502. begin
  1503. { We must pull the PC Counter from the stack, before }
  1504. { restoring the stack pointer, otherwise the PC would }
  1505. { point to nowhere! }
  1506. { Instead of doing a slow copy of the return address while trying }
  1507. { to feed it to the RTS instruction, load the PC to A0 (scratch reg) }
  1508. { then free up the stack allocated for paras, then use a JMP (A0) to }
  1509. { return to the caller with the paras freed. (KB) }
  1510. hregister:=NR_A0;
  1511. cg.a_reg_alloc(list,hregister);
  1512. reference_reset_base(ref,NR_STACK_POINTER_REG,0,4);
  1513. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1514. { instead of using a postincrement above (which also writes the }
  1515. { stackpointer reg) simply add 4 to the parasize, the instructions }
  1516. { below then take that size into account as well, so SP reg is only }
  1517. { written once (KB) }
  1518. parasize:=parasize+4;
  1519. r:=NR_SP;
  1520. { can we do a quick addition ... }
  1521. if (parasize < 9) then
  1522. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1523. else { nope ... }
  1524. begin
  1525. reference_reset_base(ref2,NR_STACK_POINTER_REG,parasize,4);
  1526. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,ref2,r));
  1527. end;
  1528. reference_reset_base(ref,hregister,0,4);
  1529. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1530. end;
  1531. end
  1532. else
  1533. list.concat(taicpu.op_none(A_RTS,S_NO));
  1534. end
  1535. else
  1536. begin
  1537. list.concat(taicpu.op_none(A_RTS,S_NO));
  1538. end;
  1539. { Routines with the poclearstack flag set use only a ret.
  1540. also routines with parasize=0 }
  1541. { TODO: figure out if these are still relevant to us (KB) }
  1542. (*
  1543. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1544. begin
  1545. { complex return values are removed from stack in C code PM }
  1546. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1547. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  1548. else
  1549. list.concat(taicpu.op_none(A_RTS,S_NO));
  1550. end
  1551. else if (parasize=0) then
  1552. begin
  1553. list.concat(taicpu.op_none(A_RTS,S_NO));
  1554. end
  1555. else
  1556. *)
  1557. end;
  1558. procedure tcg68k.g_save_registers(list:TAsmList);
  1559. var
  1560. dataregs: tcpuregisterset;
  1561. addrregs: tcpuregisterset;
  1562. href : treference;
  1563. hreg : tregister;
  1564. size : longint;
  1565. r : integer;
  1566. begin
  1567. { The code generated by the section below, particularly the movem.l
  1568. instruction is known to cause an issue when compiled by some GNU
  1569. assembler versions (I had it with 2.17, while 2.24 seems OK.)
  1570. when you run into this problem, just call inherited here instead
  1571. to skip the movem.l generation. But better just use working GNU
  1572. AS version instead. (KB) }
  1573. dataregs:=[];
  1574. addrregs:=[];
  1575. { calculate temp. size }
  1576. size:=0;
  1577. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1578. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1579. begin
  1580. hreg:=newreg(R_INTREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1581. inc(size,sizeof(aint));
  1582. dataregs:=dataregs + [saved_standard_registers[r]];
  1583. end;
  1584. if uses_registers(R_ADDRESSREGISTER) then
  1585. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1586. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1587. begin
  1588. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1589. inc(size,sizeof(aint));
  1590. addrregs:=addrregs + [saved_address_registers[r]];
  1591. end;
  1592. { 68k has no MM registers }
  1593. if uses_registers(R_MMREGISTER) then
  1594. internalerror(2014030201);
  1595. if size>0 then
  1596. begin
  1597. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1598. include(current_procinfo.flags,pi_has_saved_regs);
  1599. { Copy registers to temp }
  1600. { NOTE: virtual registers allocated here won't be translated --> no higher-level stuff. }
  1601. href:=current_procinfo.save_regs_ref;
  1602. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire) then
  1603. begin
  1604. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1605. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1606. reference_reset_base(href,NR_A0,0,sizeof(pint));
  1607. end;
  1608. if size = sizeof(aint) then
  1609. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hreg,href))
  1610. else
  1611. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,dataregs,addrregs,href));
  1612. end;
  1613. end;
  1614. procedure tcg68k.g_restore_registers(list:TAsmList);
  1615. var
  1616. dataregs: tcpuregisterset;
  1617. addrregs: tcpuregisterset;
  1618. href : treference;
  1619. r : integer;
  1620. hreg : tregister;
  1621. size : longint;
  1622. begin
  1623. { see the remark about buggy GNU AS versions in g_save_registers() (KB) }
  1624. dataregs:=[];
  1625. addrregs:=[];
  1626. if not(pi_has_saved_regs in current_procinfo.flags) then
  1627. exit;
  1628. { Copy registers from temp }
  1629. size:=0;
  1630. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1631. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1632. begin
  1633. inc(size,sizeof(aint));
  1634. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  1635. { Allocate register so the optimizer does not remove the load }
  1636. a_reg_alloc(list,hreg);
  1637. dataregs:=dataregs + [saved_standard_registers[r]];
  1638. end;
  1639. if uses_registers(R_ADDRESSREGISTER) then
  1640. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1641. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1642. begin
  1643. inc(size,sizeof(aint));
  1644. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1645. { Allocate register so the optimizer does not remove the load }
  1646. a_reg_alloc(list,hreg);
  1647. addrregs:=addrregs + [saved_address_registers[r]];
  1648. end;
  1649. { 68k has no MM registers }
  1650. if uses_registers(R_MMREGISTER) then
  1651. internalerror(2014030202);
  1652. { Restore registers from temp }
  1653. href:=current_procinfo.save_regs_ref;
  1654. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire) then
  1655. begin
  1656. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1657. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1658. reference_reset_base(href,NR_A0,0,sizeof(pint));
  1659. end;
  1660. if size = sizeof(aint) then
  1661. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,hreg))
  1662. else
  1663. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,href,dataregs,addrregs));
  1664. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1665. end;
  1666. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  1667. begin
  1668. case _newsize of
  1669. OS_S16, OS_16:
  1670. case _oldsize of
  1671. OS_S8:
  1672. begin { 8 -> 16 bit sign extend }
  1673. if (isaddressregister(reg)) then
  1674. internalerror(2014031201);
  1675. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1676. end;
  1677. OS_8: { 8 -> 16 bit zero extend }
  1678. begin
  1679. if (current_settings.cputype in cpu_coldfire) then
  1680. { ColdFire has no ANDI.W }
  1681. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg))
  1682. else
  1683. list.concat(taicpu.op_const_reg(A_AND,S_W,$FF,reg));
  1684. end;
  1685. end;
  1686. OS_S32, OS_32:
  1687. case _oldsize of
  1688. OS_S8:
  1689. begin { 8 -> 32 bit sign extend }
  1690. if (isaddressregister(reg)) then
  1691. internalerror(2014031202);
  1692. if (current_settings.cputype = cpu_MC68000) then
  1693. begin
  1694. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1695. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1696. end
  1697. else
  1698. begin
  1699. //list.concat(tai_comment.create(strpnew('sign extend byte')));
  1700. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1701. end;
  1702. end;
  1703. OS_8: { 8 -> 32 bit zero extend }
  1704. begin
  1705. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1706. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  1707. end;
  1708. OS_S16: { 16 -> 32 bit sign extend }
  1709. begin
  1710. if (isaddressregister(reg)) then
  1711. internalerror(2014031203);
  1712. //list.concat(tai_comment.create(strpnew('sign extend word')));
  1713. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1714. end;
  1715. OS_16:
  1716. begin
  1717. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1718. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  1719. end;
  1720. end;
  1721. end; { otherwise the size is already correct }
  1722. end;
  1723. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  1724. begin
  1725. sign_extend(list, _oldsize, OS_INT, reg);
  1726. end;
  1727. procedure tcg68k.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1728. var
  1729. ai : taicpu;
  1730. begin
  1731. if cond=OC_None then
  1732. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1733. else
  1734. begin
  1735. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  1736. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1737. end;
  1738. ai.is_jmp:=true;
  1739. list.concat(ai);
  1740. end;
  1741. { ensures a register is a dataregister. this is often used, as 68k can't do lots of
  1742. operations on an address register. if the register is a dataregister anyway, it
  1743. just returns it untouched.}
  1744. function tcg68k.force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  1745. var
  1746. scratch_reg: TRegister;
  1747. instr: Taicpu;
  1748. begin
  1749. if isaddressregister(reg) then
  1750. begin
  1751. scratch_reg:=getintregister(list,OS_INT);
  1752. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,scratch_reg);
  1753. add_move_instruction(instr);
  1754. list.concat(instr);
  1755. result:=scratch_reg;
  1756. end
  1757. else
  1758. result:=reg;
  1759. end;
  1760. { moves source register to destination register, if the two are not the same. can be used in pair
  1761. with force_to_dataregister() }
  1762. procedure tcg68k.move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  1763. var
  1764. instr: Taicpu;
  1765. begin
  1766. if (src <> dest) then
  1767. begin
  1768. instr:=taicpu.op_reg_reg(A_MOVE,S_L,src,dest);
  1769. add_move_instruction(instr);
  1770. list.concat(instr);
  1771. end;
  1772. end;
  1773. procedure tcg68k.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  1774. var
  1775. hsym : tsym;
  1776. href : treference;
  1777. paraloc : Pcgparalocation;
  1778. begin
  1779. { calculate the parameter info for the procdef }
  1780. procdef.init_paraloc_info(callerside);
  1781. hsym:=tsym(procdef.parast.Find('self'));
  1782. if not(assigned(hsym) and
  1783. (hsym.typ=paravarsym)) then
  1784. internalerror(2013100702);
  1785. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  1786. while paraloc<>nil do
  1787. with paraloc^ do
  1788. begin
  1789. case loc of
  1790. LOC_REGISTER:
  1791. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  1792. LOC_REFERENCE:
  1793. begin
  1794. { offset in the wrapper needs to be adjusted for the stored
  1795. return address }
  1796. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint));
  1797. { plain 68k could use SUBI on href directly, but this way it works on Coldfire too
  1798. and it's probably smaller code for the majority of cases (if ioffset small, the
  1799. load will use MOVEQ) (KB) }
  1800. a_load_const_reg(list,OS_ADDR,ioffset,NR_D0);
  1801. list.concat(taicpu.op_reg_ref(A_SUB,S_L,NR_D0,href));
  1802. end
  1803. else
  1804. internalerror(2013100703);
  1805. end;
  1806. paraloc:=next;
  1807. end;
  1808. end;
  1809. procedure tcg68k.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1810. begin
  1811. list.concat(taicpu.op_const_reg(A_SUB,S_L,localsize,NR_STACK_POINTER_REG));
  1812. end;
  1813. {****************************************************************************}
  1814. { TCG64F68K }
  1815. {****************************************************************************}
  1816. procedure tcg64f68k.a_op64_reg_reg(list : TAsmList;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  1817. var
  1818. opcode : tasmop;
  1819. xopcode : tasmop;
  1820. instr : taicpu;
  1821. begin
  1822. opcode := topcg2tasmop[op];
  1823. xopcode := topcg2tasmopx[op];
  1824. case op of
  1825. OP_ADD,OP_SUB:
  1826. begin
  1827. { if one of these three registers is an address
  1828. register, we'll really get into problems! }
  1829. if isaddressregister(regdst.reglo) or
  1830. isaddressregister(regdst.reghi) or
  1831. isaddressregister(regsrc.reghi) then
  1832. internalerror(2014030101);
  1833. list.concat(taicpu.op_reg_reg(opcode,S_L,regsrc.reglo,regdst.reglo));
  1834. list.concat(taicpu.op_reg_reg(xopcode,S_L,regsrc.reghi,regdst.reghi));
  1835. end;
  1836. OP_AND,OP_OR:
  1837. begin
  1838. { at least one of the registers must be a data register }
  1839. if (isaddressregister(regdst.reglo) and
  1840. isaddressregister(regsrc.reglo)) or
  1841. (isaddressregister(regsrc.reghi) and
  1842. isaddressregister(regdst.reghi)) then
  1843. internalerror(2014030102);
  1844. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1845. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1846. end;
  1847. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  1848. OP_IDIV,OP_DIV,
  1849. OP_IMUL,OP_MUL:
  1850. internalerror(2002081701);
  1851. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  1852. OP_SAR,OP_SHL,OP_SHR:
  1853. internalerror(2002081702);
  1854. OP_XOR:
  1855. begin
  1856. if isaddressregister(regdst.reglo) or
  1857. isaddressregister(regsrc.reglo) or
  1858. isaddressregister(regsrc.reghi) or
  1859. isaddressregister(regdst.reghi) then
  1860. internalerror(2014030103);
  1861. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1862. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1863. end;
  1864. OP_NEG,OP_NOT:
  1865. begin
  1866. if isaddressregister(regdst.reglo) or
  1867. isaddressregister(regdst.reghi) then
  1868. internalerror(2014030104);
  1869. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reglo,regdst.reglo);
  1870. cg.add_move_instruction(instr);
  1871. list.concat(instr);
  1872. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reghi,regdst.reghi);
  1873. cg.add_move_instruction(instr);
  1874. list.concat(instr);
  1875. if (op = OP_NOT) then
  1876. xopcode:=opcode;
  1877. list.concat(taicpu.op_reg(opcode,S_L,regdst.reglo));
  1878. list.concat(taicpu.op_reg(xopcode,S_L,regdst.reghi));
  1879. end;
  1880. end; { end case }
  1881. end;
  1882. procedure tcg64f68k.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  1883. var
  1884. tempref : treference;
  1885. begin
  1886. case op of
  1887. OP_NEG,OP_NOT:
  1888. begin
  1889. a_load64_ref_reg(list,ref,reg);
  1890. a_op64_reg_reg(list,op,size,reg,reg);
  1891. end;
  1892. OP_AND,OP_OR:
  1893. begin
  1894. tempref:=ref;
  1895. tcg68k(cg).fixref(list,tempref);
  1896. inc(tempref.offset,4);
  1897. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,tempref,reg.reglo));
  1898. dec(tempref.offset,4);
  1899. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,tempref,reg.reghi));
  1900. end;
  1901. else
  1902. { XOR does not allow reference for source; ADD/SUB do not allow reference for
  1903. high dword, although low dword can still be handled directly. }
  1904. inherited a_op64_ref_reg(list,op,size,ref,reg);
  1905. end;
  1906. end;
  1907. procedure tcg64f68k.a_op64_const_reg(list : TAsmList;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  1908. var
  1909. lowvalue : cardinal;
  1910. highvalue : cardinal;
  1911. opcode : tasmop;
  1912. xopcode : tasmop;
  1913. hreg : tregister;
  1914. begin
  1915. { is it optimized out ? }
  1916. { optimize64_op_const_reg doesn't seem to be used in any cg64f32 right now. why? (KB) }
  1917. { if cg.optimize64_op_const_reg(list,op,value,reg) then
  1918. exit; }
  1919. lowvalue := cardinal(value);
  1920. highvalue := value shr 32;
  1921. opcode := topcg2tasmop[op];
  1922. xopcode := topcg2tasmopx[op];
  1923. { the destination registers must be data registers }
  1924. if isaddressregister(regdst.reglo) or
  1925. isaddressregister(regdst.reghi) then
  1926. internalerror(2014030105);
  1927. case op of
  1928. OP_ADD,OP_SUB:
  1929. begin
  1930. hreg:=cg.getintregister(list,OS_INT);
  1931. { cg.a_load_const_reg provides optimized loading to register for special cases }
  1932. cg.a_load_const_reg(list,OS_S32,longint(highvalue),hreg);
  1933. { don't use cg.a_op_const_reg() here, because a possible optimized
  1934. ADDQ/SUBQ wouldn't set the eXtend bit }
  1935. list.concat(taicpu.op_const_reg(opcode,S_L,lowvalue,regdst.reglo));
  1936. list.concat(taicpu.op_reg_reg(xopcode,S_L,hreg,regdst.reghi));
  1937. end;
  1938. OP_AND,OP_OR,OP_XOR:
  1939. begin
  1940. cg.a_op_const_reg(list,op,OS_S32,longint(lowvalue),regdst.reglo);
  1941. cg.a_op_const_reg(list,op,OS_S32,longint(highvalue),regdst.reghi);
  1942. end;
  1943. { this is handled in 1st pass for 32-bit cpus (helper call) }
  1944. OP_IDIV,OP_DIV,
  1945. OP_IMUL,OP_MUL:
  1946. internalerror(2002081701);
  1947. { this is also handled in 1st pass for 32-bit cpus (helper call) }
  1948. OP_SAR,OP_SHL,OP_SHR:
  1949. internalerror(2002081702);
  1950. { these should have been handled already by earlier passes }
  1951. OP_NOT,OP_NEG:
  1952. internalerror(2012110403);
  1953. end; { end case }
  1954. end;
  1955. procedure create_codegen;
  1956. begin
  1957. cg := tcg68k.create;
  1958. cg64 :=tcg64f68k.create;
  1959. end;
  1960. end.