aasmcpu.pas 84 KB

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