cgcpu.pas 81 KB

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