aasmcpu.pas 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Contains the assembler object for the ARM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,globtype,globals,verbose,
  22. aasmbase,aasmtai,aasmdata,
  23. ogbase,
  24. symtype,
  25. cpubase,cpuinfo,cgbase,cgutils;
  26. const
  27. { "mov reg,reg" source operand number }
  28. O_MOV_SOURCE = 1;
  29. { "mov reg,reg" source operand number }
  30. O_MOV_DEST = 0;
  31. { Operand types }
  32. OT_NONE = $00000000;
  33. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  34. OT_BITS16 = $00000002;
  35. OT_BITS32 = $00000004;
  36. OT_BITS64 = $00000008; { FPU only }
  37. OT_BITS80 = $00000010;
  38. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  39. OT_NEAR = $00000040;
  40. OT_SHORT = $00000080;
  41. OT_BITSTINY = $00000100; { fpu constant }
  42. OT_BITSSHIFTER =
  43. $00000200;
  44. OT_SIZE_MASK = $000003FF; { all the size attributes }
  45. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  46. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  47. OT_TO = $00000200; { operand is followed by a colon }
  48. { reverse effect in FADD, FSUB &c }
  49. OT_COLON = $00000400;
  50. OT_SHIFTEROP = $00000800;
  51. OT_REGISTER = $00001000;
  52. OT_IMMEDIATE = $00002000;
  53. OT_REGLIST = $00008000;
  54. OT_IMM8 = $00002001;
  55. OT_IMM24 = $00002002;
  56. OT_IMM32 = $00002004;
  57. OT_IMM64 = $00002008;
  58. OT_IMM80 = $00002010;
  59. OT_IMMTINY = $00002100;
  60. OT_IMMSHIFTER= $00002200;
  61. OT_IMMEDIATE24 = OT_IMM24;
  62. OT_SHIFTIMM = OT_SHIFTEROP or OT_IMMSHIFTER;
  63. OT_SHIFTIMMEDIATE = OT_SHIFTIMM;
  64. OT_IMMEDIATESHIFTER = OT_IMMSHIFTER;
  65. OT_IMMEDIATEFPU = OT_IMMTINY;
  66. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  67. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  68. OT_REG8 = $00201001;
  69. OT_REG16 = $00201002;
  70. OT_REG32 = $00201004;
  71. OT_REG64 = $00201008;
  72. OT_VREG = $00201010; { vector register }
  73. OT_MEMORY = $00204000; { register number in 'basereg' }
  74. OT_MEM8 = $00204001;
  75. OT_MEM16 = $00204002;
  76. OT_MEM32 = $00204004;
  77. OT_MEM64 = $00204008;
  78. OT_MEM80 = $00204010;
  79. { word/byte load/store }
  80. OT_AM2 = $00010000;
  81. { misc ld/st operations }
  82. OT_AM3 = $00020000;
  83. { multiple ld/st operations }
  84. OT_AM4 = $00040000;
  85. { co proc. ld/st operations }
  86. OT_AM5 = $00080000;
  87. OT_AMMASK = $000f0000;
  88. OT_MEMORYAM2 = OT_MEMORY or OT_AM2;
  89. OT_MEMORYAM3 = OT_MEMORY or OT_AM3;
  90. OT_MEMORYAM4 = OT_MEMORY or OT_AM4;
  91. OT_MEMORYAM5 = OT_MEMORY or OT_AM5;
  92. OT_FPUREG = $01000000; { floating point stack registers }
  93. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  94. { a mask for the following }
  95. OT_MEM_OFFS = $00604000; { special type of EA }
  96. { simple [address] offset }
  97. OT_ONENESS = $00800000; { special type of immediate operand }
  98. { so UNITY == IMMEDIATE | ONENESS }
  99. OT_UNITY = $00802000; { for shift/rotate instructions }
  100. instabentries = {$i armnop.inc}
  101. maxinfolen = 5;
  102. IF_NONE = $00000000;
  103. IF_ARMMASK = $000F0000;
  104. IF_ARM7 = $00070000;
  105. IF_FPMASK = $00F00000;
  106. IF_FPA = $00100000;
  107. { if the instruction can change in a second pass }
  108. IF_PASS2 = longint($80000000);
  109. type
  110. TInsTabCache=array[TasmOp] of longint;
  111. PInsTabCache=^TInsTabCache;
  112. tinsentry = record
  113. opcode : tasmop;
  114. ops : byte;
  115. optypes : array[0..3] of longint;
  116. code : array[0..maxinfolen] of char;
  117. flags : longint;
  118. end;
  119. pinsentry=^tinsentry;
  120. const
  121. InsTab : array[0..instabentries-1] of TInsEntry={$i armtab.inc}
  122. var
  123. InsTabCache : PInsTabCache;
  124. type
  125. taicpu = class(tai_cpu_abstract)
  126. oppostfix : TOpPostfix;
  127. roundingmode : troundingmode;
  128. procedure loadshifterop(opidx:longint;const so:tshifterop);
  129. procedure loadregset(opidx:longint;const s:tcpuregisterset);
  130. constructor op_none(op : tasmop);
  131. constructor op_reg(op : tasmop;_op1 : tregister);
  132. constructor op_const(op : tasmop;_op1 : longint);
  133. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  134. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  135. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  136. constructor op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  137. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  138. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  139. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  140. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  141. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  142. constructor op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  143. { SFM/LFM }
  144. constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  145. { *M*LL }
  146. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  147. { this is for Jmp instructions }
  148. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  149. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  150. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  151. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  152. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  153. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  154. function spilling_get_operation_type(opnr: longint): topertype;override;
  155. { assembler }
  156. public
  157. { the next will reset all instructions that can change in pass 2 }
  158. procedure ResetPass1;override;
  159. procedure ResetPass2;override;
  160. function CheckIfValid:boolean;
  161. function GetString:string;
  162. function Pass1(objdata:TObjData):longint;override;
  163. procedure Pass2(objdata:TObjData);override;
  164. protected
  165. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  166. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  167. procedure ppubuildderefimploper(var o:toper);override;
  168. procedure ppuderefoper(var o:toper);override;
  169. private
  170. { next fields are filled in pass1, so pass2 is faster }
  171. inssize : shortint;
  172. insoffset : longint;
  173. LastInsOffset : longint; { need to be public to be reset }
  174. insentry : PInsEntry;
  175. function InsEnd:longint;
  176. procedure create_ot(objdata:TObjData);
  177. function Matches(p:PInsEntry):longint;
  178. function calcsize(p:PInsEntry):shortint;
  179. procedure gencode(objdata:TObjData);
  180. function NeedAddrPrefix(opidx:byte):boolean;
  181. procedure Swapoperands;
  182. function FindInsentry(objdata:TObjData):boolean;
  183. end;
  184. tai_align = class(tai_align_abstract)
  185. { nothing to add }
  186. end;
  187. function spilling_create_load(const ref:treference;r:tregister): tai;
  188. function spilling_create_store(r:tregister; const ref:treference): tai;
  189. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  190. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  191. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  192. { inserts pc relative symbols at places where they are reachable }
  193. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  194. procedure InitAsm;
  195. procedure DoneAsm;
  196. implementation
  197. uses
  198. cutils,rgobj,itcpugas;
  199. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  200. begin
  201. allocate_oper(opidx+1);
  202. with oper[opidx]^ do
  203. begin
  204. if typ<>top_shifterop then
  205. begin
  206. clearop(opidx);
  207. new(shifterop);
  208. end;
  209. shifterop^:=so;
  210. typ:=top_shifterop;
  211. if assigned(add_reg_instruction_hook) then
  212. add_reg_instruction_hook(self,shifterop^.rs);
  213. end;
  214. end;
  215. procedure taicpu.loadregset(opidx:longint;const s:tcpuregisterset);
  216. var
  217. i : byte;
  218. begin
  219. allocate_oper(opidx+1);
  220. with oper[opidx]^ do
  221. begin
  222. if typ<>top_regset then
  223. clearop(opidx);
  224. new(regset);
  225. regset^:=s;
  226. typ:=top_regset;
  227. for i:=RS_R0 to RS_R15 do
  228. begin
  229. if assigned(add_reg_instruction_hook) and (i in regset^) then
  230. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,R_SUBWHOLE));
  231. end;
  232. end;
  233. end;
  234. {*****************************************************************************
  235. taicpu Constructors
  236. *****************************************************************************}
  237. constructor taicpu.op_none(op : tasmop);
  238. begin
  239. inherited create(op);
  240. end;
  241. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  242. begin
  243. inherited create(op);
  244. ops:=1;
  245. loadreg(0,_op1);
  246. end;
  247. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  248. begin
  249. inherited create(op);
  250. ops:=1;
  251. loadconst(0,aint(_op1));
  252. end;
  253. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  254. begin
  255. inherited create(op);
  256. ops:=2;
  257. loadreg(0,_op1);
  258. loadreg(1,_op2);
  259. end;
  260. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  261. begin
  262. inherited create(op);
  263. ops:=2;
  264. loadreg(0,_op1);
  265. loadconst(1,aint(_op2));
  266. end;
  267. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  268. begin
  269. inherited create(op);
  270. ops:=2;
  271. loadref(0,_op1);
  272. loadregset(1,_op2);
  273. end;
  274. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  275. begin
  276. inherited create(op);
  277. ops:=2;
  278. loadreg(0,_op1);
  279. loadref(1,_op2);
  280. end;
  281. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  282. begin
  283. inherited create(op);
  284. ops:=3;
  285. loadreg(0,_op1);
  286. loadreg(1,_op2);
  287. loadreg(2,_op3);
  288. end;
  289. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  290. begin
  291. inherited create(op);
  292. ops:=4;
  293. loadreg(0,_op1);
  294. loadreg(1,_op2);
  295. loadreg(2,_op3);
  296. loadreg(3,_op4);
  297. end;
  298. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  299. begin
  300. inherited create(op);
  301. ops:=3;
  302. loadreg(0,_op1);
  303. loadreg(1,_op2);
  304. loadconst(2,aint(_op3));
  305. end;
  306. constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  307. begin
  308. inherited create(op);
  309. ops:=3;
  310. loadreg(0,_op1);
  311. loadconst(1,_op2);
  312. loadref(2,_op3);
  313. end;
  314. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  315. begin
  316. inherited create(op);
  317. ops:=3;
  318. loadreg(0,_op1);
  319. loadreg(1,_op2);
  320. loadsymbol(0,_op3,_op3ofs);
  321. end;
  322. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  323. begin
  324. inherited create(op);
  325. ops:=3;
  326. loadreg(0,_op1);
  327. loadreg(1,_op2);
  328. loadref(2,_op3);
  329. end;
  330. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  331. begin
  332. inherited create(op);
  333. ops:=3;
  334. loadreg(0,_op1);
  335. loadreg(1,_op2);
  336. loadshifterop(2,_op3);
  337. end;
  338. constructor taicpu.op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  339. begin
  340. inherited create(op);
  341. ops:=4;
  342. loadreg(0,_op1);
  343. loadreg(1,_op2);
  344. loadreg(2,_op3);
  345. loadshifterop(3,_op4);
  346. end;
  347. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  348. begin
  349. inherited create(op);
  350. condition:=cond;
  351. ops:=1;
  352. loadsymbol(0,_op1,0);
  353. end;
  354. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  355. begin
  356. inherited create(op);
  357. ops:=1;
  358. loadsymbol(0,_op1,0);
  359. end;
  360. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  361. begin
  362. inherited create(op);
  363. ops:=1;
  364. loadsymbol(0,_op1,_op1ofs);
  365. end;
  366. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  367. begin
  368. inherited create(op);
  369. ops:=2;
  370. loadreg(0,_op1);
  371. loadsymbol(1,_op2,_op2ofs);
  372. end;
  373. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  374. begin
  375. inherited create(op);
  376. ops:=2;
  377. loadsymbol(0,_op1,_op1ofs);
  378. loadref(1,_op2);
  379. end;
  380. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  381. begin
  382. { allow the register allocator to remove unnecessary moves }
  383. result:=(((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  384. ((opcode=A_MVF) and (regtype = R_FPUREGISTER))
  385. ) and
  386. (condition=C_None) and
  387. (ops=2) and
  388. (oper[0]^.typ=top_reg) and
  389. (oper[1]^.typ=top_reg) and
  390. (oper[0]^.reg=oper[1]^.reg);
  391. end;
  392. function spilling_create_load(const ref:treference;r:tregister): tai;
  393. begin
  394. case getregtype(r) of
  395. R_INTREGISTER :
  396. result:=taicpu.op_reg_ref(A_LDR,r,ref);
  397. R_FPUREGISTER :
  398. { use lfm because we don't know the current internal format
  399. and avoid exceptions
  400. }
  401. result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
  402. else
  403. internalerror(200401041);
  404. end;
  405. end;
  406. function spilling_create_store(r:tregister; const ref:treference): tai;
  407. begin
  408. case getregtype(r) of
  409. R_INTREGISTER :
  410. result:=taicpu.op_reg_ref(A_STR,r,ref);
  411. R_FPUREGISTER :
  412. { use sfm because we don't know the current internal format
  413. and avoid exceptions
  414. }
  415. result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
  416. else
  417. internalerror(200401041);
  418. end;
  419. end;
  420. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  421. begin
  422. case opcode of
  423. A_ADC,A_ADD,A_AND,
  424. A_EOR,A_CLZ,
  425. A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
  426. A_LDRSH,A_LDRT,
  427. A_MOV,A_MVN,A_MLA,A_MUL,
  428. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  429. A_SWP,A_SWPB,
  430. A_LDF,A_FLT,A_FIX,
  431. A_ADF,A_DVF,A_FDV,A_FML,
  432. A_RFS,A_RFC,A_RDF,
  433. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  434. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  435. A_LFM:
  436. if opnr=0 then
  437. result:=operand_write
  438. else
  439. result:=operand_read;
  440. A_BIC,A_BKPT,A_B,A_BL,A_BLX,A_BX,
  441. A_CMN,A_CMP,A_TEQ,A_TST,
  442. A_CMF,A_CMFE,A_WFS,A_CNF:
  443. result:=operand_read;
  444. A_SMLAL,A_UMLAL:
  445. if opnr in [0,1] then
  446. result:=operand_readwrite
  447. else
  448. result:=operand_read;
  449. A_SMULL,A_UMULL:
  450. if opnr in [0,1] then
  451. result:=operand_write
  452. else
  453. result:=operand_read;
  454. A_STR,A_STRB,A_STRBT,
  455. A_STRH,A_STRT,A_STF,A_SFM:
  456. { important is what happens with the involved registers }
  457. if opnr=0 then
  458. result := operand_read
  459. else
  460. { check for pre/post indexed }
  461. result := operand_read;
  462. else
  463. internalerror(200403151);
  464. end;
  465. end;
  466. procedure BuildInsTabCache;
  467. var
  468. i : longint;
  469. begin
  470. new(instabcache);
  471. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  472. i:=0;
  473. while (i<InsTabEntries) do
  474. begin
  475. if InsTabCache^[InsTab[i].Opcode]=-1 then
  476. InsTabCache^[InsTab[i].Opcode]:=i;
  477. inc(i);
  478. end;
  479. end;
  480. procedure InitAsm;
  481. begin
  482. if not assigned(instabcache) then
  483. BuildInsTabCache;
  484. end;
  485. procedure DoneAsm;
  486. begin
  487. if assigned(instabcache) then
  488. begin
  489. dispose(instabcache);
  490. instabcache:=nil;
  491. end;
  492. end;
  493. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  494. begin
  495. i.oppostfix:=pf;
  496. result:=i;
  497. end;
  498. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  499. begin
  500. i.roundingmode:=rm;
  501. result:=i;
  502. end;
  503. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  504. begin
  505. i.condition:=c;
  506. result:=i;
  507. end;
  508. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  509. var
  510. curpos : longint;
  511. lastpos : longint;
  512. curop : longint;
  513. curtai : tai;
  514. curdatatai,hp : tai;
  515. curdata : TAsmList;
  516. l : tasmlabel;
  517. begin
  518. curdata:=TAsmList.create;
  519. lastpos:=-1;
  520. curpos:=0;
  521. curtai:=tai(list.first);
  522. while assigned(curtai) do
  523. begin
  524. { instruction? }
  525. if curtai.typ=ait_instruction then
  526. begin
  527. { walk through all operand of the instruction }
  528. for curop:=0 to taicpu(curtai).ops-1 do
  529. begin
  530. { reference? }
  531. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  532. begin
  533. { pc relative symbol? }
  534. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  535. if assigned(curdatatai) and
  536. { move only if we're at the first reference of a label }
  537. (taicpu(curtai).oper[curop]^.ref^.offset=0) then
  538. begin
  539. { if yes, insert till next symbol }
  540. repeat
  541. hp:=tai(curdatatai.next);
  542. listtoinsert.remove(curdatatai);
  543. curdata.concat(curdatatai);
  544. curdatatai:=hp;
  545. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  546. if lastpos=-1 then
  547. lastpos:=curpos;
  548. end;
  549. end;
  550. end;
  551. inc(curpos);
  552. end;
  553. { split only at real instructions else the test below fails }
  554. if ((curpos-lastpos)>1016) and (curtai.typ=ait_instruction) and
  555. (
  556. { don't split loads of pc to lr and the following move }
  557. not(
  558. (taicpu(curtai).opcode=A_MOV) and
  559. (taicpu(curtai).oper[0]^.typ=top_reg) and
  560. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  561. (taicpu(curtai).oper[1]^.typ=top_reg) and
  562. (taicpu(curtai).oper[1]^.reg=NR_PC)
  563. )
  564. ) then
  565. begin
  566. lastpos:=curpos;
  567. hp:=tai(curtai.next);
  568. current_asmdata.getjumplabel(l);
  569. curdata.insert(taicpu.op_sym(A_B,l));
  570. curdata.concat(tai_label.create(l));
  571. list.insertlistafter(curtai,curdata);
  572. curtai:=hp;
  573. end
  574. else
  575. curtai:=tai(curtai.next);
  576. end;
  577. list.concatlist(curdata);
  578. curdata.free;
  579. end;
  580. (*
  581. Floating point instruction format information, taken from the linux kernel
  582. ARM Floating Point Instruction Classes
  583. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  584. |c o n d|1 1 0 P|U|u|W|L| Rn |v| Fd |0|0|0|1| o f f s e t | CPDT
  585. |c o n d|1 1 0 P|U|w|W|L| Rn |x| Fd |0|0|1|0| o f f s e t | CPDT (copro 2)
  586. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  587. |c o n d|1 1 1 0|a|b|c|d|e| Fn |j| Fd |0|0|0|1|f|g|h|0|i| Fm | CPDO
  588. |c o n d|1 1 1 0|a|b|c|L|e| Fn | Rd |0|0|0|1|f|g|h|1|i| Fm | CPRT
  589. |c o n d|1 1 1 0|a|b|c|1|e| Fn |1|1|1|1|0|0|0|1|f|g|h|1|i| Fm | comparisons
  590. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  591. CPDT data transfer instructions
  592. LDF, STF, LFM (copro 2), SFM (copro 2)
  593. CPDO dyadic arithmetic instructions
  594. ADF, MUF, SUF, RSF, DVF, RDF,
  595. POW, RPW, RMF, FML, FDV, FRD, POL
  596. CPDO monadic arithmetic instructions
  597. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  598. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  599. CPRT joint arithmetic/data transfer instructions
  600. FIX (arithmetic followed by load/store)
  601. FLT (load/store followed by arithmetic)
  602. CMF, CNF CMFE, CNFE (comparisons)
  603. WFS, RFS (write/read floating point status register)
  604. WFC, RFC (write/read floating point control register)
  605. cond condition codes
  606. P pre/post index bit: 0 = postindex, 1 = preindex
  607. U up/down bit: 0 = stack grows down, 1 = stack grows up
  608. W write back bit: 1 = update base register (Rn)
  609. L load/store bit: 0 = store, 1 = load
  610. Rn base register
  611. Rd destination/source register
  612. Fd floating point destination register
  613. Fn floating point source register
  614. Fm floating point source register or floating point constant
  615. uv transfer length (TABLE 1)
  616. wx register count (TABLE 2)
  617. abcd arithmetic opcode (TABLES 3 & 4)
  618. ef destination size (rounding precision) (TABLE 5)
  619. gh rounding mode (TABLE 6)
  620. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  621. i constant bit: 1 = constant (TABLE 6)
  622. */
  623. /*
  624. TABLE 1
  625. +-------------------------+---+---+---------+---------+
  626. | Precision | u | v | FPSR.EP | length |
  627. +-------------------------+---+---+---------+---------+
  628. | Single | 0 | 0 | x | 1 words |
  629. | Double | 1 | 1 | x | 2 words |
  630. | Extended | 1 | 1 | x | 3 words |
  631. | Packed decimal | 1 | 1 | 0 | 3 words |
  632. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  633. +-------------------------+---+---+---------+---------+
  634. Note: x = don't care
  635. */
  636. /*
  637. TABLE 2
  638. +---+---+---------------------------------+
  639. | w | x | Number of registers to transfer |
  640. +---+---+---------------------------------+
  641. | 0 | 1 | 1 |
  642. | 1 | 0 | 2 |
  643. | 1 | 1 | 3 |
  644. | 0 | 0 | 4 |
  645. +---+---+---------------------------------+
  646. */
  647. /*
  648. TABLE 3: Dyadic Floating Point Opcodes
  649. +---+---+---+---+----------+-----------------------+-----------------------+
  650. | a | b | c | d | Mnemonic | Description | Operation |
  651. +---+---+---+---+----------+-----------------------+-----------------------+
  652. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  653. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  654. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  655. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  656. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  657. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  658. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  659. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  660. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  661. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  662. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  663. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  664. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  665. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  666. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  667. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  668. +---+---+---+---+----------+-----------------------+-----------------------+
  669. Note: POW, RPW, POL are deprecated, and are available for backwards
  670. compatibility only.
  671. */
  672. /*
  673. TABLE 4: Monadic Floating Point Opcodes
  674. +---+---+---+---+----------+-----------------------+-----------------------+
  675. | a | b | c | d | Mnemonic | Description | Operation |
  676. +---+---+---+---+----------+-----------------------+-----------------------+
  677. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  678. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  679. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  680. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  681. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  682. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  683. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  684. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  685. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  686. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  687. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  688. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  689. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  690. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  691. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  692. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  693. +---+---+---+---+----------+-----------------------+-----------------------+
  694. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  695. available for backwards compatibility only.
  696. */
  697. /*
  698. TABLE 5
  699. +-------------------------+---+---+
  700. | Rounding Precision | e | f |
  701. +-------------------------+---+---+
  702. | IEEE Single precision | 0 | 0 |
  703. | IEEE Double precision | 0 | 1 |
  704. | IEEE Extended precision | 1 | 0 |
  705. | undefined (trap) | 1 | 1 |
  706. +-------------------------+---+---+
  707. */
  708. /*
  709. TABLE 5
  710. +---------------------------------+---+---+
  711. | Rounding Mode | g | h |
  712. +---------------------------------+---+---+
  713. | Round to nearest (default) | 0 | 0 |
  714. | Round toward plus infinity | 0 | 1 |
  715. | Round toward negative infinity | 1 | 0 |
  716. | Round toward zero | 1 | 1 |
  717. +---------------------------------+---+---+
  718. *)
  719. function taicpu.GetString:string;
  720. var
  721. i : longint;
  722. s : string;
  723. addsize : boolean;
  724. begin
  725. s:='['+gas_op2str[opcode];
  726. for i:=0 to ops-1 do
  727. begin
  728. with oper[i]^ do
  729. begin
  730. if i=0 then
  731. s:=s+' '
  732. else
  733. s:=s+',';
  734. { type }
  735. addsize:=false;
  736. if (ot and OT_VREG)=OT_VREG then
  737. s:=s+'vreg'
  738. else
  739. if (ot and OT_FPUREG)=OT_FPUREG then
  740. s:=s+'fpureg'
  741. else
  742. if (ot and OT_REGISTER)=OT_REGISTER then
  743. begin
  744. s:=s+'reg';
  745. addsize:=true;
  746. end
  747. else
  748. if (ot and OT_REGLIST)=OT_REGLIST then
  749. begin
  750. s:=s+'reglist';
  751. addsize:=false;
  752. end
  753. else
  754. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  755. begin
  756. s:=s+'imm';
  757. addsize:=true;
  758. end
  759. else
  760. if (ot and OT_MEMORY)=OT_MEMORY then
  761. begin
  762. s:=s+'mem';
  763. addsize:=true;
  764. if (ot and OT_AM2)<>0 then
  765. s:=s+' am2 ';
  766. end
  767. else
  768. s:=s+'???';
  769. { size }
  770. if addsize then
  771. begin
  772. if (ot and OT_BITS8)<>0 then
  773. s:=s+'8'
  774. else
  775. if (ot and OT_BITS16)<>0 then
  776. s:=s+'24'
  777. else
  778. if (ot and OT_BITS32)<>0 then
  779. s:=s+'32'
  780. else
  781. if (ot and OT_BITSSHIFTER)<>0 then
  782. s:=s+'shifter'
  783. else
  784. s:=s+'??';
  785. { signed }
  786. if (ot and OT_SIGNED)<>0 then
  787. s:=s+'s';
  788. end;
  789. end;
  790. end;
  791. GetString:=s+']';
  792. end;
  793. procedure taicpu.ResetPass1;
  794. begin
  795. { we need to reset everything here, because the choosen insentry
  796. can be invalid for a new situation where the previously optimized
  797. insentry is not correct }
  798. InsEntry:=nil;
  799. InsSize:=0;
  800. LastInsOffset:=-1;
  801. end;
  802. procedure taicpu.ResetPass2;
  803. begin
  804. { we are here in a second pass, check if the instruction can be optimized }
  805. if assigned(InsEntry) and
  806. ((InsEntry^.flags and IF_PASS2)<>0) then
  807. begin
  808. InsEntry:=nil;
  809. InsSize:=0;
  810. end;
  811. LastInsOffset:=-1;
  812. end;
  813. function taicpu.CheckIfValid:boolean;
  814. begin
  815. end;
  816. function taicpu.Pass1(objdata:TObjData):longint;
  817. var
  818. ldr2op : array[PF_B..PF_T] of tasmop = (
  819. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  820. str2op : array[PF_B..PF_T] of tasmop = (
  821. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  822. begin
  823. Pass1:=0;
  824. { Save the old offset and set the new offset }
  825. InsOffset:=ObjData.CurrObjSec.Size;
  826. { Error? }
  827. if (Insentry=nil) and (InsSize=-1) then
  828. exit;
  829. { set the file postion }
  830. current_filepos:=fileinfo;
  831. { tranlate LDR+postfix to complete opcode }
  832. if (opcode=A_LDR) and (oppostfix<>PF_None) then
  833. begin
  834. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  835. opcode:=ldr2op[oppostfix]
  836. else
  837. internalerror(2005091001);
  838. if opcode=A_None then
  839. internalerror(2005091004);
  840. { postfix has been added to opcode }
  841. oppostfix:=PF_None;
  842. end
  843. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  844. begin
  845. if (oppostfix in [low(str2op)..high(str2op)]) then
  846. opcode:=str2op[oppostfix]
  847. else
  848. internalerror(2005091002);
  849. if opcode=A_None then
  850. internalerror(2005091003);
  851. { postfix has been added to opcode }
  852. oppostfix:=PF_None;
  853. end;
  854. { Get InsEntry }
  855. if FindInsEntry(objdata) then
  856. begin
  857. InsSize:=4;
  858. LastInsOffset:=InsOffset;
  859. Pass1:=InsSize;
  860. exit;
  861. end;
  862. LastInsOffset:=-1;
  863. end;
  864. procedure taicpu.Pass2(objdata:TObjData);
  865. begin
  866. { error in pass1 ? }
  867. if insentry=nil then
  868. exit;
  869. current_filepos:=fileinfo;
  870. { Generate the instruction }
  871. GenCode(objdata);
  872. end;
  873. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  874. begin
  875. end;
  876. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  877. begin
  878. end;
  879. procedure taicpu.ppubuildderefimploper(var o:toper);
  880. begin
  881. end;
  882. procedure taicpu.ppuderefoper(var o:toper);
  883. begin
  884. end;
  885. function taicpu.InsEnd:longint;
  886. begin
  887. end;
  888. procedure taicpu.create_ot(objdata:TObjData);
  889. var
  890. i,l,relsize : longint;
  891. dummy : byte;
  892. currsym : TObjSymbol;
  893. begin
  894. if ops=0 then
  895. exit;
  896. { update oper[].ot field }
  897. for i:=0 to ops-1 do
  898. with oper[i]^ do
  899. begin
  900. case typ of
  901. top_regset:
  902. begin
  903. ot:=OT_REGLIST;
  904. end;
  905. top_reg :
  906. begin
  907. case getregtype(reg) of
  908. R_INTREGISTER:
  909. ot:=OT_REG32 or OT_SHIFTEROP;
  910. R_FPUREGISTER:
  911. ot:=OT_FPUREG;
  912. else
  913. internalerror(2005090901);
  914. end;
  915. end;
  916. top_ref :
  917. begin
  918. if ref^.refaddr=addr_no then
  919. begin
  920. { create ot field }
  921. { we should get the size here dependend on the
  922. instruction }
  923. if (ot and OT_SIZE_MASK)=0 then
  924. ot:=OT_MEMORY or OT_BITS32
  925. else
  926. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  927. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  928. ot:=ot or OT_MEM_OFFS;
  929. { if we need to fix a reference, we do it here }
  930. { pc relative addressing }
  931. if (ref^.base=NR_NO) and
  932. (ref^.index=NR_NO) and
  933. (ref^.shiftmode=SM_None)
  934. { at least we should check if the destination symbol
  935. is in a text section }
  936. { and
  937. (ref^.symbol^.owner="text") } then
  938. ref^.base:=NR_PC;
  939. { determine possible address modes }
  940. if (ref^.base<>NR_NO) and
  941. (
  942. (
  943. (ref^.index=NR_NO) and
  944. (ref^.shiftmode=SM_None) and
  945. (ref^.offset>=-4097) and
  946. (ref^.offset<=4097)
  947. ) or
  948. (
  949. (ref^.shiftmode=SM_None) and
  950. (ref^.offset=0)
  951. ) or
  952. (
  953. (ref^.index<>NR_NO) and
  954. (ref^.shiftmode<>SM_None) and
  955. (ref^.shiftimm<=31) and
  956. (ref^.offset=0)
  957. )
  958. ) then
  959. ot:=ot or OT_AM2;
  960. if (ref^.index<>NR_NO) and
  961. (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  962. (
  963. (ref^.base=NR_NO) and
  964. (ref^.shiftmode=SM_None) and
  965. (ref^.offset=0)
  966. ) then
  967. ot:=ot or OT_AM4;
  968. end
  969. else
  970. begin
  971. l:=ref^.offset;
  972. currsym:=ObjData.symbolref(ref^.symbol);
  973. if assigned(currsym) then
  974. inc(l,currsym.address);
  975. relsize:=(InsOffset+2)-l;
  976. if (relsize<-33554428) or (relsize>33554428) then
  977. ot:=OT_IMM32
  978. else
  979. ot:=OT_IMM24;
  980. end;
  981. end;
  982. top_local :
  983. begin
  984. { we should get the size here dependend on the
  985. instruction }
  986. if (ot and OT_SIZE_MASK)=0 then
  987. ot:=OT_MEMORY or OT_BITS32
  988. else
  989. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  990. end;
  991. top_const :
  992. begin
  993. ot:=OT_IMMEDIATE;
  994. if is_shifter_const(val,dummy) then
  995. ot:=OT_IMMSHIFTER
  996. else
  997. ot:=OT_IMM32
  998. end;
  999. top_none :
  1000. begin
  1001. { generated when there was an error in the
  1002. assembler reader. It never happends when generating
  1003. assembler }
  1004. end;
  1005. top_shifterop:
  1006. begin
  1007. ot:=OT_SHIFTEROP;
  1008. end;
  1009. else
  1010. internalerror(200402261);
  1011. end;
  1012. end;
  1013. end;
  1014. function taicpu.Matches(p:PInsEntry):longint;
  1015. { * IF_SM stands for Size Match: any operand whose size is not
  1016. * explicitly specified by the template is `really' intended to be
  1017. * the same size as the first size-specified operand.
  1018. * Non-specification is tolerated in the input instruction, but
  1019. * _wrong_ specification is not.
  1020. *
  1021. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1022. * three-operand instructions such as SHLD: it implies that the
  1023. * first two operands must match in size, but that the third is
  1024. * required to be _unspecified_.
  1025. *
  1026. * IF_SB invokes Size Byte: operands with unspecified size in the
  1027. * template are really bytes, and so no non-byte specification in
  1028. * the input instruction will be tolerated. IF_SW similarly invokes
  1029. * Size Word, and IF_SD invokes Size Doubleword.
  1030. *
  1031. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1032. * that any operand with unspecified size in the template is
  1033. * required to have unspecified size in the instruction too...)
  1034. }
  1035. var
  1036. i,j,asize,oprs : longint;
  1037. siz : array[0..3] of longint;
  1038. begin
  1039. Matches:=100;
  1040. writeln(getstring,'---');
  1041. { Check the opcode and operands }
  1042. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1043. begin
  1044. Matches:=0;
  1045. exit;
  1046. end;
  1047. { Check that no spurious colons or TOs are present }
  1048. for i:=0 to p^.ops-1 do
  1049. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  1050. begin
  1051. Matches:=0;
  1052. exit;
  1053. end;
  1054. { Check that the operand flags all match up }
  1055. for i:=0 to p^.ops-1 do
  1056. begin
  1057. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  1058. ((p^.optypes[i] and OT_SIZE_MASK) and
  1059. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  1060. begin
  1061. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  1062. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  1063. begin
  1064. Matches:=0;
  1065. exit;
  1066. end
  1067. else
  1068. Matches:=1;
  1069. end;
  1070. end;
  1071. { check postfixes:
  1072. the existance of a certain postfix requires a
  1073. particular code }
  1074. { update condition flags
  1075. or floating point single }
  1076. if (oppostfix=PF_S) and
  1077. not(p^.code[0] in [#$04]) then
  1078. begin
  1079. Matches:=0;
  1080. exit;
  1081. end;
  1082. { floating point size }
  1083. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  1084. not(p^.code[0] in []) then
  1085. begin
  1086. Matches:=0;
  1087. exit;
  1088. end;
  1089. { multiple load/store address modes }
  1090. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1091. not(p^.code[0] in [
  1092. // ldr,str,ldrb,strb
  1093. #$17,
  1094. // stm,ldm
  1095. #$26
  1096. ]) then
  1097. begin
  1098. Matches:=0;
  1099. exit;
  1100. end;
  1101. { we shouldn't see any opsize prefixes here }
  1102. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  1103. begin
  1104. Matches:=0;
  1105. exit;
  1106. end;
  1107. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  1108. begin
  1109. Matches:=0;
  1110. exit;
  1111. end;
  1112. { Check operand sizes }
  1113. { as default an untyped size can get all the sizes, this is different
  1114. from nasm, but else we need to do a lot checking which opcodes want
  1115. size or not with the automatic size generation }
  1116. asize:=longint($ffffffff);
  1117. (*
  1118. if (p^.flags and IF_SB)<>0 then
  1119. asize:=OT_BITS8
  1120. else if (p^.flags and IF_SW)<>0 then
  1121. asize:=OT_BITS16
  1122. else if (p^.flags and IF_SD)<>0 then
  1123. asize:=OT_BITS32;
  1124. if (p^.flags and IF_ARMASK)<>0 then
  1125. begin
  1126. siz[0]:=0;
  1127. siz[1]:=0;
  1128. siz[2]:=0;
  1129. if (p^.flags and IF_AR0)<>0 then
  1130. siz[0]:=asize
  1131. else if (p^.flags and IF_AR1)<>0 then
  1132. siz[1]:=asize
  1133. else if (p^.flags and IF_AR2)<>0 then
  1134. siz[2]:=asize;
  1135. end
  1136. else
  1137. begin
  1138. { we can leave because the size for all operands is forced to be
  1139. the same
  1140. but not if IF_SB IF_SW or IF_SD is set PM }
  1141. if asize=-1 then
  1142. exit;
  1143. siz[0]:=asize;
  1144. siz[1]:=asize;
  1145. siz[2]:=asize;
  1146. end;
  1147. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1148. begin
  1149. if (p^.flags and IF_SM2)<>0 then
  1150. oprs:=2
  1151. else
  1152. oprs:=p^.ops;
  1153. for i:=0 to oprs-1 do
  1154. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1155. begin
  1156. for j:=0 to oprs-1 do
  1157. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1158. break;
  1159. end;
  1160. end
  1161. else
  1162. oprs:=2;
  1163. { Check operand sizes }
  1164. for i:=0 to p^.ops-1 do
  1165. begin
  1166. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1167. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1168. { Immediates can always include smaller size }
  1169. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1170. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1171. Matches:=2;
  1172. end;
  1173. *)
  1174. end;
  1175. function taicpu.calcsize(p:PInsEntry):shortint;
  1176. begin
  1177. result:=4;
  1178. end;
  1179. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  1180. begin
  1181. end;
  1182. procedure taicpu.Swapoperands;
  1183. begin
  1184. end;
  1185. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1186. var
  1187. i : longint;
  1188. begin
  1189. result:=false;
  1190. { Things which may only be done once, not when a second pass is done to
  1191. optimize }
  1192. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1193. begin
  1194. { create the .ot fields }
  1195. create_ot(objdata);
  1196. { set the file postion }
  1197. current_filepos:=fileinfo;
  1198. end
  1199. else
  1200. begin
  1201. { we've already an insentry so it's valid }
  1202. result:=true;
  1203. exit;
  1204. end;
  1205. { Lookup opcode in the table }
  1206. InsSize:=-1;
  1207. i:=instabcache^[opcode];
  1208. if i=-1 then
  1209. begin
  1210. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1211. exit;
  1212. end;
  1213. insentry:=@instab[i];
  1214. while (insentry^.opcode=opcode) do
  1215. begin
  1216. if matches(insentry)=100 then
  1217. begin
  1218. result:=true;
  1219. exit;
  1220. end;
  1221. inc(i);
  1222. insentry:=@instab[i];
  1223. end;
  1224. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1225. { No instruction found, set insentry to nil and inssize to -1 }
  1226. insentry:=nil;
  1227. inssize:=-1;
  1228. end;
  1229. procedure taicpu.gencode(objdata:TObjData);
  1230. var
  1231. bytes : dword;
  1232. i_field : byte;
  1233. procedure setshifterop(op : byte);
  1234. begin
  1235. case oper[op]^.typ of
  1236. top_const:
  1237. begin
  1238. i_field:=1;
  1239. bytes:=bytes or (oper[op]^.val and $fff);
  1240. end;
  1241. top_reg:
  1242. begin
  1243. i_field:=0;
  1244. bytes:=bytes or (getsupreg(oper[op]^.reg) shl 16);
  1245. { does a real shifter op follow? }
  1246. if (op+1<=op) and (oper[op+1]^.typ=top_shifterop) then
  1247. begin
  1248. end;
  1249. end;
  1250. else
  1251. internalerror(2005091103);
  1252. end;
  1253. end;
  1254. begin
  1255. bytes:=$0;
  1256. { evaluate and set condition code }
  1257. { condition code allowed? }
  1258. { setup rest of the instruction }
  1259. case insentry^.code[0] of
  1260. #$08:
  1261. begin
  1262. { set instruction code }
  1263. bytes:=bytes or (ord(insentry^.code[1]) shl 26);
  1264. bytes:=bytes or (ord(insentry^.code[2]) shl 21);
  1265. { set destination }
  1266. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  1267. { create shifter op }
  1268. setshifterop(1);
  1269. { set i field }
  1270. bytes:=bytes or (i_field shl 25);
  1271. { set s if necessary }
  1272. if oppostfix=PF_S then
  1273. bytes:=bytes or (1 shl 20);
  1274. end;
  1275. #$ff:
  1276. internalerror(2005091101);
  1277. else
  1278. internalerror(2005091102);
  1279. end;
  1280. { we're finished, write code }
  1281. objdata.writebytes(bytes,sizeof(bytes));
  1282. end;
  1283. end.
  1284. {$ifdef dummy}
  1285. (*
  1286. static void gencode (long segment, long offset, int bits,
  1287. insn *ins, char *codes, long insn_end)
  1288. {
  1289. int has_S_code; /* S - setflag */
  1290. int has_B_code; /* B - setflag */
  1291. int has_T_code; /* T - setflag */
  1292. int has_W_code; /* ! => W flag */
  1293. int has_F_code; /* ^ => S flag */
  1294. int keep;
  1295. unsigned char c;
  1296. unsigned char bytes[4];
  1297. long data, size;
  1298. static int cc_code[] = /* bit pattern of cc */
  1299. { /* order as enum in */
  1300. 0x0E, 0x03, 0x02, 0x00, /* nasm.h */
  1301. 0x0A, 0x0C, 0x08, 0x0D,
  1302. 0x09, 0x0B, 0x04, 0x01,
  1303. 0x05, 0x07, 0x06,
  1304. };
  1305. (*
  1306. #ifdef DEBUG
  1307. static char *CC[] =
  1308. { /* condition code names */
  1309. "AL", "CC", "CS", "EQ",
  1310. "GE", "GT", "HI", "LE",
  1311. "LS", "LT", "MI", "NE",
  1312. "PL", "VC", "VS", "",
  1313. "S"
  1314. };
  1315. *)
  1316. has_S_code = (ins->condition & C_SSETFLAG);
  1317. has_B_code = (ins->condition & C_BSETFLAG);
  1318. has_T_code = (ins->condition & C_TSETFLAG);
  1319. has_W_code = (ins->condition & C_EXSETFLAG);
  1320. has_F_code = (ins->condition & C_FSETFLAG);
  1321. ins->condition = (ins->condition & 0x0F);
  1322. (*
  1323. if (rt_debug)
  1324. {
  1325. printf ("gencode: instruction: %s%s", insn_names[ins->opcode],
  1326. CC[ins->condition & 0x0F]);
  1327. if (has_S_code)
  1328. printf ("S");
  1329. if (has_B_code)
  1330. printf ("B");
  1331. if (has_T_code)
  1332. printf ("T");
  1333. if (has_W_code)
  1334. printf ("!");
  1335. if (has_F_code)
  1336. printf ("^");
  1337. printf ("\n");
  1338. c = *codes;
  1339. printf (" (%d) decode - '0x%02X'\n", ins->operands, c);
  1340. bytes[0] = 0xB;
  1341. bytes[1] = 0xE;
  1342. bytes[2] = 0xE;
  1343. bytes[3] = 0xF;
  1344. }
  1345. *)
  1346. // First condition code in upper nibble
  1347. if (ins->condition < C_NONE)
  1348. {
  1349. c = cc_code[ins->condition] << 4;
  1350. }
  1351. else
  1352. {
  1353. c = cc_code[C_AL] << 4; // is often ALWAYS but not always
  1354. }
  1355. switch (keep = *codes)
  1356. {
  1357. case 1:
  1358. // B, BL
  1359. ++codes;
  1360. c |= *codes++;
  1361. bytes[0] = c;
  1362. if (ins->oprs[0].segment != segment)
  1363. {
  1364. // fais une relocation
  1365. c = 1;
  1366. data = 0; // Let the linker locate ??
  1367. }
  1368. else
  1369. {
  1370. c = 0;
  1371. data = ins->oprs[0].offset - (offset + 8);
  1372. if (data % 4)
  1373. {
  1374. errfunc (ERR_NONFATAL, "offset not aligned on 4 bytes");
  1375. }
  1376. }
  1377. if (data >= 0x1000)
  1378. {
  1379. errfunc (ERR_NONFATAL, "too long offset");
  1380. }
  1381. data = data >> 2;
  1382. bytes[1] = (data >> 16) & 0xFF;
  1383. bytes[2] = (data >> 8) & 0xFF;
  1384. bytes[3] = (data ) & 0xFF;
  1385. if (c == 1)
  1386. {
  1387. // out (offset, segment, &bytes[0], OUT_RAWDATA+1, NO_SEG, NO_SEG);
  1388. out (offset, segment, &bytes[0], OUT_REL3ADR+4, ins->oprs[0].segment, NO_SEG);
  1389. }
  1390. else
  1391. {
  1392. out (offset, segment, &bytes[0], OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1393. }
  1394. return;
  1395. case 2:
  1396. // SWI
  1397. ++codes;
  1398. c |= *codes++;
  1399. bytes[0] = c;
  1400. data = ins->oprs[0].offset;
  1401. bytes[1] = (data >> 16) & 0xFF;
  1402. bytes[2] = (data >> 8) & 0xFF;
  1403. bytes[3] = (data) & 0xFF;
  1404. out (offset, segment, &bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1405. return;
  1406. case 3:
  1407. // BX
  1408. ++codes;
  1409. c |= *codes++;
  1410. bytes[0] = c;
  1411. bytes[1] = *codes++;
  1412. bytes[2] = *codes++;
  1413. bytes[3] = *codes++;
  1414. c = regval (&ins->oprs[0],1);
  1415. if (c == 15) // PC
  1416. {
  1417. errfunc (ERR_WARNING, "'BX' with R15 has undefined behaviour");
  1418. }
  1419. else if (c > 15)
  1420. {
  1421. errfunc (ERR_NONFATAL, "Illegal register specified for 'BX'");
  1422. }
  1423. bytes[3] |= (c & 0x0F);
  1424. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1425. return;
  1426. case 4: // AND Rd,Rn,Rm
  1427. case 5: // AND Rd,Rn,Rm,<shift>Rs
  1428. case 6: // AND Rd,Rn,Rm,<shift>imm
  1429. case 7: // AND Rd,Rn,<shift>imm
  1430. ++codes;
  1431. #ifdef DEBUG
  1432. if (rt_debug)
  1433. {
  1434. printf (" decode - '0x%02X'\n", keep);
  1435. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1436. }
  1437. #endif
  1438. bytes[0] = c | *codes;
  1439. ++codes;
  1440. bytes[1] = *codes;
  1441. if (has_S_code)
  1442. bytes[1] |= 0x10;
  1443. c = regval (&ins->oprs[1],1);
  1444. // Rn in low nibble
  1445. bytes[1] |= c;
  1446. // Rd in high nibble
  1447. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1448. if (keep != 7)
  1449. {
  1450. // Rm in low nibble
  1451. bytes[3] = regval (&ins->oprs[2],1);
  1452. }
  1453. // Shifts if any
  1454. if (keep == 5 || keep == 6)
  1455. {
  1456. // Shift in bytes 2 and 3
  1457. if (keep == 5)
  1458. {
  1459. // Rs
  1460. c = regval (&ins->oprs[3],1);
  1461. bytes[2] |= c;
  1462. c = 0x10; // Set bit 4 in byte[3]
  1463. }
  1464. if (keep == 6)
  1465. {
  1466. c = (ins->oprs[3].offset) & 0x1F;
  1467. // #imm
  1468. bytes[2] |= c >> 1;
  1469. if (c & 0x01)
  1470. {
  1471. bytes[3] |= 0x80;
  1472. }
  1473. c = 0; // Clr bit 4 in byte[3]
  1474. }
  1475. // <shift>
  1476. c |= shiftval (&ins->oprs[3]) << 5;
  1477. bytes[3] |= c;
  1478. }
  1479. // reg,reg,imm
  1480. if (keep == 7)
  1481. {
  1482. int shimm;
  1483. shimm = imm_shift (ins->oprs[2].offset);
  1484. if (shimm == -1)
  1485. {
  1486. errfunc (ERR_NONFATAL, "cannot create that constant");
  1487. }
  1488. bytes[3] = shimm & 0xFF;
  1489. bytes[2] |= (shimm & 0xF00) >> 8;
  1490. }
  1491. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1492. return;
  1493. case 8: // MOV Rd,Rm
  1494. case 9: // MOV Rd,Rm,<shift>Rs
  1495. case 0xA: // MOV Rd,Rm,<shift>imm
  1496. case 0xB: // MOV Rd,<shift>imm
  1497. ++codes;
  1498. #ifdef DEBUG
  1499. if (rt_debug)
  1500. {
  1501. printf (" decode - '0x%02X'\n", keep);
  1502. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1503. }
  1504. #endif
  1505. bytes[0] = c | *codes;
  1506. ++codes;
  1507. bytes[1] = *codes;
  1508. if (has_S_code)
  1509. bytes[1] |= 0x10;
  1510. // Rd in high nibble
  1511. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1512. if (keep != 0x0B)
  1513. {
  1514. // Rm in low nibble
  1515. bytes[3] = regval (&ins->oprs[1],1);
  1516. }
  1517. // Shifts if any
  1518. if (keep == 0x09 || keep == 0x0A)
  1519. {
  1520. // Shift in bytes 2 and 3
  1521. if (keep == 0x09)
  1522. {
  1523. // Rs
  1524. c = regval (&ins->oprs[2],1);
  1525. bytes[2] |= c;
  1526. c = 0x10; // Set bit 4 in byte[3]
  1527. }
  1528. if (keep == 0x0A)
  1529. {
  1530. c = (ins->oprs[2].offset) & 0x1F;
  1531. // #imm
  1532. bytes[2] |= c >> 1;
  1533. if (c & 0x01)
  1534. {
  1535. bytes[3] |= 0x80;
  1536. }
  1537. c = 0; // Clr bit 4 in byte[3]
  1538. }
  1539. // <shift>
  1540. c |= shiftval (&ins->oprs[2]) << 5;
  1541. bytes[3] |= c;
  1542. }
  1543. // reg,imm
  1544. if (keep == 0x0B)
  1545. {
  1546. int shimm;
  1547. shimm = imm_shift (ins->oprs[1].offset);
  1548. if (shimm == -1)
  1549. {
  1550. errfunc (ERR_NONFATAL, "cannot create that constant");
  1551. }
  1552. bytes[3] = shimm & 0xFF;
  1553. bytes[2] |= (shimm & 0xF00) >> 8;
  1554. }
  1555. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1556. return;
  1557. case 0xC: // CMP Rn,Rm
  1558. case 0xD: // CMP Rn,Rm,<shift>Rs
  1559. case 0xE: // CMP Rn,Rm,<shift>imm
  1560. case 0xF: // CMP Rn,<shift>imm
  1561. ++codes;
  1562. bytes[0] = c | *codes++;
  1563. bytes[1] = *codes;
  1564. // Implicit S code
  1565. bytes[1] |= 0x10;
  1566. c = regval (&ins->oprs[0],1);
  1567. // Rn in low nibble
  1568. bytes[1] |= c;
  1569. // No destination
  1570. bytes[2] = 0;
  1571. if (keep != 0x0B)
  1572. {
  1573. // Rm in low nibble
  1574. bytes[3] = regval (&ins->oprs[1],1);
  1575. }
  1576. // Shifts if any
  1577. if (keep == 0x0D || keep == 0x0E)
  1578. {
  1579. // Shift in bytes 2 and 3
  1580. if (keep == 0x0D)
  1581. {
  1582. // Rs
  1583. c = regval (&ins->oprs[2],1);
  1584. bytes[2] |= c;
  1585. c = 0x10; // Set bit 4 in byte[3]
  1586. }
  1587. if (keep == 0x0E)
  1588. {
  1589. c = (ins->oprs[2].offset) & 0x1F;
  1590. // #imm
  1591. bytes[2] |= c >> 1;
  1592. if (c & 0x01)
  1593. {
  1594. bytes[3] |= 0x80;
  1595. }
  1596. c = 0; // Clr bit 4 in byte[3]
  1597. }
  1598. // <shift>
  1599. c |= shiftval (&ins->oprs[2]) << 5;
  1600. bytes[3] |= c;
  1601. }
  1602. // reg,imm
  1603. if (keep == 0x0F)
  1604. {
  1605. int shimm;
  1606. shimm = imm_shift (ins->oprs[1].offset);
  1607. if (shimm == -1)
  1608. {
  1609. errfunc (ERR_NONFATAL, "cannot create that constant");
  1610. }
  1611. bytes[3] = shimm & 0xFF;
  1612. bytes[2] |= (shimm & 0xF00) >> 8;
  1613. }
  1614. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1615. return;
  1616. case 0x10: // MRS Rd,<psr>
  1617. ++codes;
  1618. bytes[0] = c | *codes++;
  1619. bytes[1] = *codes++;
  1620. // Rd
  1621. c = regval (&ins->oprs[0],1);
  1622. bytes[2] = c << 4;
  1623. bytes[3] = 0;
  1624. c = ins->oprs[1].basereg;
  1625. if (c == R_CPSR || c == R_SPSR)
  1626. {
  1627. if (c == R_SPSR)
  1628. {
  1629. bytes[1] |= 0x40;
  1630. }
  1631. }
  1632. else
  1633. {
  1634. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1635. }
  1636. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1637. return;
  1638. case 0x11: // MSR <psr>,Rm
  1639. case 0x12: // MSR <psrf>,Rm
  1640. case 0x13: // MSR <psrf>,#expression
  1641. ++codes;
  1642. bytes[0] = c | *codes++;
  1643. bytes[1] = *codes++;
  1644. bytes[2] = *codes;
  1645. if (keep == 0x11 || keep == 0x12)
  1646. {
  1647. // Rm
  1648. c = regval (&ins->oprs[1],1);
  1649. bytes[3] = c;
  1650. }
  1651. else
  1652. {
  1653. int shimm;
  1654. shimm = imm_shift (ins->oprs[1].offset);
  1655. if (shimm == -1)
  1656. {
  1657. errfunc (ERR_NONFATAL, "cannot create that constant");
  1658. }
  1659. bytes[3] = shimm & 0xFF;
  1660. bytes[2] |= (shimm & 0xF00) >> 8;
  1661. }
  1662. c = ins->oprs[0].basereg;
  1663. if ( keep == 0x11)
  1664. {
  1665. if ( c == R_CPSR || c == R_SPSR)
  1666. {
  1667. if ( c== R_SPSR)
  1668. {
  1669. bytes[1] |= 0x40;
  1670. }
  1671. }
  1672. else
  1673. {
  1674. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1675. }
  1676. }
  1677. else
  1678. {
  1679. if ( c == R_CPSR_FLG || c == R_SPSR_FLG)
  1680. {
  1681. if ( c== R_SPSR_FLG)
  1682. {
  1683. bytes[1] |= 0x40;
  1684. }
  1685. }
  1686. else
  1687. {
  1688. errfunc (ERR_NONFATAL, "CPSR_flg or SPSR_flg expected");
  1689. }
  1690. }
  1691. break;
  1692. case 0x14: // MUL Rd,Rm,Rs
  1693. case 0x15: // MULA Rd,Rm,Rs,Rn
  1694. ++codes;
  1695. bytes[0] = c | *codes++;
  1696. bytes[1] = *codes++;
  1697. bytes[3] = *codes;
  1698. // Rd
  1699. bytes[1] |= regval (&ins->oprs[0],1);
  1700. if (has_S_code)
  1701. bytes[1] |= 0x10;
  1702. // Rm
  1703. bytes[3] |= regval (&ins->oprs[1],1);
  1704. // Rs
  1705. bytes[2] = regval (&ins->oprs[2],1);
  1706. if (keep == 0x15)
  1707. {
  1708. bytes[2] |= regval (&ins->oprs[3],1) << 4;
  1709. }
  1710. break;
  1711. case 0x16: // SMLAL RdHi,RdLo,Rm,Rs
  1712. ++codes;
  1713. bytes[0] = c | *codes++;
  1714. bytes[1] = *codes++;
  1715. bytes[3] = *codes;
  1716. // RdHi
  1717. bytes[1] |= regval (&ins->oprs[1],1);
  1718. if (has_S_code)
  1719. bytes[1] |= 0x10;
  1720. // RdLo
  1721. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1722. // Rm
  1723. bytes[3] |= regval (&ins->oprs[2],1);
  1724. // Rs
  1725. bytes[2] |= regval (&ins->oprs[3],1);
  1726. break;
  1727. case 0x17: // LDR Rd, expression
  1728. ++codes;
  1729. bytes[0] = c | *codes++;
  1730. bytes[1] = *codes++;
  1731. // Rd
  1732. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1733. if (has_B_code)
  1734. bytes[1] |= 0x40;
  1735. if (has_T_code)
  1736. {
  1737. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1738. }
  1739. if (has_W_code)
  1740. {
  1741. errfunc (ERR_NONFATAL, "'!' not allowed");
  1742. }
  1743. // Rn - implicit R15
  1744. bytes[1] |= 0xF;
  1745. if (ins->oprs[1].segment != segment)
  1746. {
  1747. errfunc (ERR_NONFATAL, "label not in same segment");
  1748. }
  1749. data = ins->oprs[1].offset - (offset + 8);
  1750. if (data < 0)
  1751. {
  1752. data = -data;
  1753. }
  1754. else
  1755. {
  1756. bytes[1] |= 0x80;
  1757. }
  1758. if (data >= 0x1000)
  1759. {
  1760. errfunc (ERR_NONFATAL, "too long offset");
  1761. }
  1762. bytes[2] |= ((data & 0xF00) >> 8);
  1763. bytes[3] = data & 0xFF;
  1764. break;
  1765. case 0x18: // LDR Rd, [Rn]
  1766. ++codes;
  1767. bytes[0] = c | *codes++;
  1768. bytes[1] = *codes++;
  1769. // Rd
  1770. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1771. if (has_B_code)
  1772. bytes[1] |= 0x40;
  1773. if (has_T_code)
  1774. {
  1775. bytes[1] |= 0x20; // write-back
  1776. }
  1777. else
  1778. {
  1779. bytes[0] |= 0x01; // implicit pre-index mode
  1780. }
  1781. if (has_W_code)
  1782. {
  1783. bytes[1] |= 0x20; // write-back
  1784. }
  1785. // Rn
  1786. c = regval (&ins->oprs[1],1);
  1787. bytes[1] |= c;
  1788. if (c == 0x15) // R15
  1789. data = -8;
  1790. else
  1791. data = 0;
  1792. if (data < 0)
  1793. {
  1794. data = -data;
  1795. }
  1796. else
  1797. {
  1798. bytes[1] |= 0x80;
  1799. }
  1800. bytes[2] |= ((data & 0xF00) >> 8);
  1801. bytes[3] = data & 0xFF;
  1802. break;
  1803. case 0x19: // LDR Rd, [Rn,#expression]
  1804. case 0x20: // LDR Rd, [Rn,Rm]
  1805. case 0x21: // LDR Rd, [Rn,Rm,shift]
  1806. ++codes;
  1807. bytes[0] = c | *codes++;
  1808. bytes[1] = *codes++;
  1809. // Rd
  1810. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1811. if (has_B_code)
  1812. bytes[1] |= 0x40;
  1813. // Rn
  1814. c = regval (&ins->oprs[1],1);
  1815. bytes[1] |= c;
  1816. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1817. {
  1818. bytes[0] |= 0x01; // pre-index mode
  1819. if (has_W_code)
  1820. {
  1821. bytes[1] |= 0x20;
  1822. }
  1823. if (has_T_code)
  1824. {
  1825. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1826. }
  1827. }
  1828. else
  1829. {
  1830. if (has_T_code) // Forced write-back in post-index mode
  1831. {
  1832. bytes[1] |= 0x20;
  1833. }
  1834. if (has_W_code)
  1835. {
  1836. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1837. }
  1838. }
  1839. if (keep == 0x19)
  1840. {
  1841. data = ins->oprs[2].offset;
  1842. if (data < 0)
  1843. {
  1844. data = -data;
  1845. }
  1846. else
  1847. {
  1848. bytes[1] |= 0x80;
  1849. }
  1850. if (data >= 0x1000)
  1851. {
  1852. errfunc (ERR_NONFATAL, "too long offset");
  1853. }
  1854. bytes[2] |= ((data & 0xF00) >> 8);
  1855. bytes[3] = data & 0xFF;
  1856. }
  1857. else
  1858. {
  1859. if (ins->oprs[2].minus == 0)
  1860. {
  1861. bytes[1] |= 0x80;
  1862. }
  1863. c = regval (&ins->oprs[2],1);
  1864. bytes[3] = c;
  1865. if (keep == 0x21)
  1866. {
  1867. c = ins->oprs[3].offset;
  1868. if (c > 0x1F)
  1869. {
  1870. errfunc (ERR_NONFATAL, "too large shiftvalue");
  1871. c = c & 0x1F;
  1872. }
  1873. bytes[2] |= c >> 1;
  1874. if (c & 0x01)
  1875. {
  1876. bytes[3] |= 0x80;
  1877. }
  1878. bytes[3] |= shiftval (&ins->oprs[3]) << 5;
  1879. }
  1880. }
  1881. break;
  1882. case 0x22: // LDRH Rd, expression
  1883. ++codes;
  1884. bytes[0] = c | 0x01; // Implicit pre-index
  1885. bytes[1] = *codes++;
  1886. // Rd
  1887. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1888. // Rn - implicit R15
  1889. bytes[1] |= 0xF;
  1890. if (ins->oprs[1].segment != segment)
  1891. {
  1892. errfunc (ERR_NONFATAL, "label not in same segment");
  1893. }
  1894. data = ins->oprs[1].offset - (offset + 8);
  1895. if (data < 0)
  1896. {
  1897. data = -data;
  1898. }
  1899. else
  1900. {
  1901. bytes[1] |= 0x80;
  1902. }
  1903. if (data >= 0x100)
  1904. {
  1905. errfunc (ERR_NONFATAL, "too long offset");
  1906. }
  1907. bytes[3] = *codes++;
  1908. bytes[2] |= ((data & 0xF0) >> 4);
  1909. bytes[3] |= data & 0xF;
  1910. break;
  1911. case 0x23: // LDRH Rd, Rn
  1912. ++codes;
  1913. bytes[0] = c | 0x01; // Implicit pre-index
  1914. bytes[1] = *codes++;
  1915. // Rd
  1916. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1917. // Rn
  1918. c = regval (&ins->oprs[1],1);
  1919. bytes[1] |= c;
  1920. if (c == 0x15) // R15
  1921. data = -8;
  1922. else
  1923. data = 0;
  1924. if (data < 0)
  1925. {
  1926. data = -data;
  1927. }
  1928. else
  1929. {
  1930. bytes[1] |= 0x80;
  1931. }
  1932. if (data >= 0x100)
  1933. {
  1934. errfunc (ERR_NONFATAL, "too long offset");
  1935. }
  1936. bytes[3] = *codes++;
  1937. bytes[2] |= ((data & 0xF0) >> 4);
  1938. bytes[3] |= data & 0xF;
  1939. break;
  1940. case 0x24: // LDRH Rd, Rn, expression
  1941. case 0x25: // LDRH Rd, Rn, Rm
  1942. ++codes;
  1943. bytes[0] = c;
  1944. bytes[1] = *codes++;
  1945. // Rd
  1946. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1947. // Rn
  1948. c = regval (&ins->oprs[1],1);
  1949. bytes[1] |= c;
  1950. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1951. {
  1952. bytes[0] |= 0x01; // pre-index mode
  1953. if (has_W_code)
  1954. {
  1955. bytes[1] |= 0x20;
  1956. }
  1957. }
  1958. else
  1959. {
  1960. if (has_W_code)
  1961. {
  1962. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1963. }
  1964. }
  1965. bytes[3] = *codes++;
  1966. if (keep == 0x24)
  1967. {
  1968. data = ins->oprs[2].offset;
  1969. if (data < 0)
  1970. {
  1971. data = -data;
  1972. }
  1973. else
  1974. {
  1975. bytes[1] |= 0x80;
  1976. }
  1977. if (data >= 0x100)
  1978. {
  1979. errfunc (ERR_NONFATAL, "too long offset");
  1980. }
  1981. bytes[2] |= ((data & 0xF0) >> 4);
  1982. bytes[3] |= data & 0xF;
  1983. }
  1984. else
  1985. {
  1986. if (ins->oprs[2].minus == 0)
  1987. {
  1988. bytes[1] |= 0x80;
  1989. }
  1990. c = regval (&ins->oprs[2],1);
  1991. bytes[3] |= c;
  1992. }
  1993. break;
  1994. case 0x26: // LDM/STM Rn, {reg-list}
  1995. ++codes;
  1996. bytes[0] = c;
  1997. bytes[0] |= ( *codes >> 4) & 0xF;
  1998. bytes[1] = ( *codes << 4) & 0xF0;
  1999. ++codes;
  2000. if (has_W_code)
  2001. {
  2002. bytes[1] |= 0x20;
  2003. }
  2004. if (has_F_code)
  2005. {
  2006. bytes[1] |= 0x40;
  2007. }
  2008. // Rn
  2009. bytes[1] |= regval (&ins->oprs[0],1);
  2010. data = ins->oprs[1].basereg;
  2011. bytes[2] = ((data >> 8) & 0xFF);
  2012. bytes[3] = (data & 0xFF);
  2013. break;
  2014. case 0x27: // SWP Rd, Rm, [Rn]
  2015. ++codes;
  2016. bytes[0] = c;
  2017. bytes[0] |= *codes++;
  2018. bytes[1] = regval (&ins->oprs[2],1);
  2019. if (has_B_code)
  2020. {
  2021. bytes[1] |= 0x40;
  2022. }
  2023. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2024. bytes[3] = *codes++;
  2025. bytes[3] |= regval (&ins->oprs[1],1);
  2026. break;
  2027. default:
  2028. errfunc (ERR_FATAL, "unknown decoding of instruction");
  2029. bytes[0] = c;
  2030. // And a fix nibble
  2031. ++codes;
  2032. bytes[0] |= *codes++;
  2033. if ( *codes == 0x01) // An I bit
  2034. {
  2035. }
  2036. if ( *codes == 0x02) // An I bit
  2037. {
  2038. }
  2039. ++codes;
  2040. }
  2041. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2042. }
  2043. *)
  2044. {$endif dummy
  2045. }