cgcpu.pas 82 KB

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