aasmcpu.pas 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409
  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. 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; ausermode: boolean=false);
  134. procedure loadconditioncode(opidx:longint;const cond:tasmcond);
  135. procedure loadmodeflags(opidx:longint;const flags:tcpumodeflags);
  136. procedure loadspecialreg(opidx:longint;const areg:tregister; const aflags:tspecialregflags);
  137. constructor op_none(op : tasmop);
  138. constructor op_reg(op : tasmop;_op1 : tregister);
  139. constructor op_ref(op : tasmop;const _op1 : treference);
  140. constructor op_const(op : tasmop;_op1 : longint);
  141. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  142. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  143. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  144. constructor op_regset(op:tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
  145. constructor op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
  146. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  147. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  148. constructor op_reg_const_const(op : tasmop;_op1 : tregister; _op2,_op3: aint);
  149. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  150. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  151. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  152. constructor op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  153. { SFM/LFM }
  154. constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  155. { ITxxx }
  156. constructor op_cond(op: tasmop; cond: tasmcond);
  157. { CPSxx }
  158. constructor op_modeflags(op: tasmop; flags: tcpumodeflags);
  159. constructor op_modeflags_const(op: tasmop; flags: tcpumodeflags; a: aint);
  160. { MSR }
  161. constructor op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
  162. { *M*LL }
  163. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  164. { this is for Jmp instructions }
  165. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  166. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  167. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  168. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  169. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  170. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  171. function spilling_get_operation_type(opnr: longint): topertype;override;
  172. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;override;
  173. { assembler }
  174. public
  175. { the next will reset all instructions that can change in pass 2 }
  176. procedure ResetPass1;override;
  177. procedure ResetPass2;override;
  178. function CheckIfValid:boolean;
  179. function GetString:string;
  180. function Pass1(objdata:TObjData):longint;override;
  181. procedure Pass2(objdata:TObjData);override;
  182. protected
  183. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  184. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  185. procedure ppubuildderefimploper(var o:toper);override;
  186. procedure ppuderefoper(var o:toper);override;
  187. private
  188. { next fields are filled in pass1, so pass2 is faster }
  189. inssize : shortint;
  190. insoffset : longint;
  191. LastInsOffset : longint; { need to be public to be reset }
  192. insentry : PInsEntry;
  193. function InsEnd:longint;
  194. procedure create_ot(objdata:TObjData);
  195. function Matches(p:PInsEntry):longint;
  196. function calcsize(p:PInsEntry):shortint;
  197. procedure gencode(objdata:TObjData);
  198. function NeedAddrPrefix(opidx:byte):boolean;
  199. procedure Swapoperands;
  200. function FindInsentry(objdata:TObjData):boolean;
  201. end;
  202. tai_align = class(tai_align_abstract)
  203. { nothing to add }
  204. end;
  205. tai_thumb_func = class(tai)
  206. constructor create;
  207. end;
  208. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  209. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  210. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  211. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  212. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  213. { inserts pc relative symbols at places where they are reachable
  214. and transforms special instructions to valid instruction encodings }
  215. procedure finalizearmcode(list,listtoinsert : TAsmList);
  216. { inserts .pdata section and dummy function prolog needed for arm-wince exception handling }
  217. procedure InsertPData;
  218. procedure InitAsm;
  219. procedure DoneAsm;
  220. implementation
  221. uses
  222. itcpugas,aoptcpu;
  223. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  224. begin
  225. allocate_oper(opidx+1);
  226. with oper[opidx]^ do
  227. begin
  228. if typ<>top_shifterop then
  229. begin
  230. clearop(opidx);
  231. new(shifterop);
  232. end;
  233. shifterop^:=so;
  234. typ:=top_shifterop;
  235. if assigned(add_reg_instruction_hook) then
  236. add_reg_instruction_hook(self,shifterop^.rs);
  237. end;
  238. end;
  239. procedure taicpu.loadregset(opidx:longint; regsetregtype: tregistertype; regsetsubregtype: tsubregister; const s:tcpuregisterset; ausermode: boolean);
  240. var
  241. i : byte;
  242. begin
  243. allocate_oper(opidx+1);
  244. with oper[opidx]^ do
  245. begin
  246. if typ<>top_regset then
  247. begin
  248. clearop(opidx);
  249. new(regset);
  250. end;
  251. regset^:=s;
  252. regtyp:=regsetregtype;
  253. subreg:=regsetsubregtype;
  254. usermode:=ausermode;
  255. typ:=top_regset;
  256. case regsetregtype of
  257. R_INTREGISTER:
  258. for i:=RS_R0 to RS_R15 do
  259. begin
  260. if assigned(add_reg_instruction_hook) and (i in regset^) then
  261. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,regsetsubregtype));
  262. end;
  263. R_MMREGISTER:
  264. { both RS_S0 and RS_D0 range from 0 to 31 }
  265. for i:=RS_D0 to RS_D31 do
  266. begin
  267. if assigned(add_reg_instruction_hook) and (i in regset^) then
  268. add_reg_instruction_hook(self,newreg(R_MMREGISTER,i,regsetsubregtype));
  269. end;
  270. end;
  271. end;
  272. end;
  273. procedure taicpu.loadconditioncode(opidx:longint;const cond:tasmcond);
  274. begin
  275. allocate_oper(opidx+1);
  276. with oper[opidx]^ do
  277. begin
  278. if typ<>top_conditioncode then
  279. clearop(opidx);
  280. cc:=cond;
  281. typ:=top_conditioncode;
  282. end;
  283. end;
  284. procedure taicpu.loadmodeflags(opidx: longint; const flags: tcpumodeflags);
  285. begin
  286. allocate_oper(opidx+1);
  287. with oper[opidx]^ do
  288. begin
  289. if typ<>top_modeflags then
  290. clearop(opidx);
  291. modeflags:=flags;
  292. typ:=top_modeflags;
  293. end;
  294. end;
  295. procedure taicpu.loadspecialreg(opidx: longint; const areg: tregister; const aflags: tspecialregflags);
  296. begin
  297. allocate_oper(opidx+1);
  298. with oper[opidx]^ do
  299. begin
  300. if typ<>top_specialreg then
  301. clearop(opidx);
  302. specialreg:=areg;
  303. specialflags:=aflags;
  304. typ:=top_specialreg;
  305. end;
  306. end;
  307. {*****************************************************************************
  308. taicpu Constructors
  309. *****************************************************************************}
  310. constructor taicpu.op_none(op : tasmop);
  311. begin
  312. inherited create(op);
  313. end;
  314. { for pld }
  315. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  316. begin
  317. inherited create(op);
  318. ops:=1;
  319. loadref(0,_op1);
  320. end;
  321. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  322. begin
  323. inherited create(op);
  324. ops:=1;
  325. loadreg(0,_op1);
  326. end;
  327. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  328. begin
  329. inherited create(op);
  330. ops:=1;
  331. loadconst(0,aint(_op1));
  332. end;
  333. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  334. begin
  335. inherited create(op);
  336. ops:=2;
  337. loadreg(0,_op1);
  338. loadreg(1,_op2);
  339. end;
  340. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  341. begin
  342. inherited create(op);
  343. ops:=2;
  344. loadreg(0,_op1);
  345. loadconst(1,aint(_op2));
  346. end;
  347. constructor taicpu.op_regset(op: tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
  348. begin
  349. inherited create(op);
  350. ops:=1;
  351. loadregset(0,regtype,subreg,_op1);
  352. end;
  353. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
  354. begin
  355. inherited create(op);
  356. ops:=2;
  357. loadref(0,_op1);
  358. loadregset(1,regtype,subreg,_op2);
  359. end;
  360. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  361. begin
  362. inherited create(op);
  363. ops:=2;
  364. loadreg(0,_op1);
  365. loadref(1,_op2);
  366. end;
  367. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  368. begin
  369. inherited create(op);
  370. ops:=3;
  371. loadreg(0,_op1);
  372. loadreg(1,_op2);
  373. loadreg(2,_op3);
  374. end;
  375. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  376. begin
  377. inherited create(op);
  378. ops:=4;
  379. loadreg(0,_op1);
  380. loadreg(1,_op2);
  381. loadreg(2,_op3);
  382. loadreg(3,_op4);
  383. end;
  384. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  385. begin
  386. inherited create(op);
  387. ops:=3;
  388. loadreg(0,_op1);
  389. loadreg(1,_op2);
  390. loadconst(2,aint(_op3));
  391. end;
  392. constructor taicpu.op_reg_const_const(op : tasmop;_op1 : tregister; _op2,_op3: aint);
  393. begin
  394. inherited create(op);
  395. ops:=3;
  396. loadreg(0,_op1);
  397. loadconst(1,aint(_op2));
  398. loadconst(2,aint(_op3));
  399. end;
  400. constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  401. begin
  402. inherited create(op);
  403. ops:=3;
  404. loadreg(0,_op1);
  405. loadconst(1,_op2);
  406. loadref(2,_op3);
  407. end;
  408. constructor taicpu.op_cond(op: tasmop; cond: tasmcond);
  409. begin
  410. inherited create(op);
  411. ops:=1;
  412. loadconditioncode(0, cond);
  413. end;
  414. constructor taicpu.op_modeflags(op: tasmop; flags: tcpumodeflags);
  415. begin
  416. inherited create(op);
  417. ops := 1;
  418. loadmodeflags(0,flags);
  419. end;
  420. constructor taicpu.op_modeflags_const(op: tasmop; flags: tcpumodeflags; a: aint);
  421. begin
  422. inherited create(op);
  423. ops := 2;
  424. loadmodeflags(0,flags);
  425. loadconst(1,a);
  426. end;
  427. constructor taicpu.op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
  428. begin
  429. inherited create(op);
  430. ops:=2;
  431. loadspecialreg(0,specialreg,specialregflags);
  432. loadreg(1,_op2);
  433. end;
  434. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  435. begin
  436. inherited create(op);
  437. ops:=3;
  438. loadreg(0,_op1);
  439. loadreg(1,_op2);
  440. loadsymbol(0,_op3,_op3ofs);
  441. end;
  442. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  443. begin
  444. inherited create(op);
  445. ops:=3;
  446. loadreg(0,_op1);
  447. loadreg(1,_op2);
  448. loadref(2,_op3);
  449. end;
  450. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  451. begin
  452. inherited create(op);
  453. ops:=3;
  454. loadreg(0,_op1);
  455. loadreg(1,_op2);
  456. loadshifterop(2,_op3);
  457. end;
  458. constructor taicpu.op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  459. begin
  460. inherited create(op);
  461. ops:=4;
  462. loadreg(0,_op1);
  463. loadreg(1,_op2);
  464. loadreg(2,_op3);
  465. loadshifterop(3,_op4);
  466. end;
  467. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  468. begin
  469. inherited create(op);
  470. condition:=cond;
  471. ops:=1;
  472. loadsymbol(0,_op1,0);
  473. end;
  474. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  475. begin
  476. inherited create(op);
  477. ops:=1;
  478. loadsymbol(0,_op1,0);
  479. end;
  480. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  481. begin
  482. inherited create(op);
  483. ops:=1;
  484. loadsymbol(0,_op1,_op1ofs);
  485. end;
  486. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  487. begin
  488. inherited create(op);
  489. ops:=2;
  490. loadreg(0,_op1);
  491. loadsymbol(1,_op2,_op2ofs);
  492. end;
  493. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  494. begin
  495. inherited create(op);
  496. ops:=2;
  497. loadsymbol(0,_op1,_op1ofs);
  498. loadref(1,_op2);
  499. end;
  500. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  501. begin
  502. { allow the register allocator to remove unnecessary moves }
  503. result:=(
  504. ((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  505. ((opcode=A_MVF) and (regtype = R_FPUREGISTER)) or
  506. ((opcode in [A_FCPYS, A_FCPYD]) and (regtype = R_MMREGISTER))
  507. ) and
  508. (oppostfix in [PF_None,PF_D]) and
  509. (condition=C_None) and
  510. (ops=2) and
  511. (oper[0]^.typ=top_reg) and
  512. (oper[1]^.typ=top_reg) and
  513. (oper[0]^.reg=oper[1]^.reg);
  514. end;
  515. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  516. var
  517. op: tasmop;
  518. begin
  519. case getregtype(r) of
  520. R_INTREGISTER :
  521. result:=taicpu.op_reg_ref(A_LDR,r,ref);
  522. R_FPUREGISTER :
  523. { use lfm because we don't know the current internal format
  524. and avoid exceptions
  525. }
  526. result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
  527. R_MMREGISTER :
  528. begin
  529. case getsubreg(r) of
  530. R_SUBFD:
  531. op:=A_FLDD;
  532. R_SUBFS:
  533. op:=A_FLDS;
  534. R_SUBNONE:
  535. op:=A_VLDR;
  536. else
  537. internalerror(2009112905);
  538. end;
  539. result:=taicpu.op_reg_ref(op,r,ref);
  540. end;
  541. else
  542. internalerror(200401041);
  543. end;
  544. end;
  545. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  546. var
  547. op: tasmop;
  548. begin
  549. case getregtype(r) of
  550. R_INTREGISTER :
  551. result:=taicpu.op_reg_ref(A_STR,r,ref);
  552. R_FPUREGISTER :
  553. { use sfm because we don't know the current internal format
  554. and avoid exceptions
  555. }
  556. result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
  557. R_MMREGISTER :
  558. begin
  559. case getsubreg(r) of
  560. R_SUBFD:
  561. op:=A_FSTD;
  562. R_SUBFS:
  563. op:=A_FSTS;
  564. R_SUBNONE:
  565. op:=A_VSTR;
  566. else
  567. internalerror(2009112904);
  568. end;
  569. result:=taicpu.op_reg_ref(op,r,ref);
  570. end;
  571. else
  572. internalerror(200401041);
  573. end;
  574. end;
  575. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  576. begin
  577. case opcode of
  578. A_ADC,A_ADD,A_AND,A_BIC,
  579. A_EOR,A_CLZ,A_RBIT,
  580. A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
  581. A_LDRSH,A_LDRT,
  582. A_MOV,A_MVN,A_MLA,A_MUL,
  583. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  584. A_SWP,A_SWPB,
  585. A_LDF,A_FLT,A_FIX,
  586. A_ADF,A_DVF,A_FDV,A_FML,
  587. A_RFS,A_RFC,A_RDF,
  588. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  589. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  590. A_LFM,
  591. A_FLDS,A_FLDD,
  592. A_FMRX,A_FMXR,A_FMSTAT,
  593. A_FMSR,A_FMRS,A_FMDRR,
  594. A_FCPYS,A_FCPYD,A_FCVTSD,A_FCVTDS,
  595. A_FABSS,A_FABSD,A_FSQRTS,A_FSQRTD,A_FMULS,A_FMULD,
  596. A_FADDS,A_FADDD,A_FSUBS,A_FSUBD,A_FDIVS,A_FDIVD,
  597. A_FMACS,A_FMACD,A_FMSCS,A_FMSCD,A_FNMACS,A_FNMACD,
  598. A_FNMSCS,A_FNMSCD,A_FNMULS,A_FNMULD,
  599. A_FMDHR,A_FMRDH,A_FMDLR,A_FMRDL,
  600. A_FNEGS,A_FNEGD,
  601. A_FSITOS,A_FSITOD,A_FTOSIS,A_FTOSID,
  602. A_FTOUIS,A_FTOUID,A_FUITOS,A_FUITOD,
  603. A_SXTB16,A_UXTB16,
  604. A_UXTB,A_UXTH,A_SXTB,A_SXTH,
  605. A_NEG,
  606. A_VABS,A_VADD,A_VCVT,A_VDIV,A_VLDR,A_VMOV,A_VMUL,A_VNEG,A_VSQRT,A_VSUB:
  607. if opnr=0 then
  608. result:=operand_write
  609. else
  610. result:=operand_read;
  611. A_BKPT,A_B,A_BL,A_BLX,A_BX,
  612. A_CMN,A_CMP,A_TEQ,A_TST,
  613. A_CMF,A_CMFE,A_WFS,A_CNF,
  614. A_FCMPS,A_FCMPD,A_FCMPES,A_FCMPED,A_FCMPEZS,A_FCMPEZD,
  615. A_FCMPZS,A_FCMPZD,
  616. A_VCMP,A_VCMPE:
  617. result:=operand_read;
  618. A_SMLAL,A_UMLAL:
  619. if opnr in [0,1] then
  620. result:=operand_readwrite
  621. else
  622. result:=operand_read;
  623. A_SMULL,A_UMULL,
  624. A_FMRRD:
  625. if opnr in [0,1] then
  626. result:=operand_write
  627. else
  628. result:=operand_read;
  629. A_STR,A_STRB,A_STRBT,
  630. A_STRH,A_STRT,A_STF,A_SFM,
  631. A_FSTS,A_FSTD,
  632. A_VSTR:
  633. { important is what happens with the involved registers }
  634. if opnr=0 then
  635. result := operand_read
  636. else
  637. { check for pre/post indexed }
  638. result := operand_read;
  639. //Thumb2
  640. A_LSL, A_LSR, A_ROR, A_ASR, A_SDIV, A_UDIV, A_MOVW, A_MOVT, A_MLS, A_BFI:
  641. if opnr in [0] then
  642. result:=operand_write
  643. else
  644. result:=operand_read;
  645. A_BFC:
  646. if opnr in [0] then
  647. result:=operand_readwrite
  648. else
  649. result:=operand_read;
  650. A_LDREX:
  651. if opnr in [0] then
  652. result:=operand_write
  653. else
  654. result:=operand_read;
  655. A_STREX:
  656. result:=operand_write;
  657. else
  658. internalerror(200403151);
  659. end;
  660. end;
  661. function taicpu.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  662. begin
  663. result := operand_read;
  664. if (oper[opnr]^.ref^.base = reg) and
  665. (oper[opnr]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) then
  666. result := operand_readwrite;
  667. end;
  668. procedure BuildInsTabCache;
  669. var
  670. i : longint;
  671. begin
  672. new(instabcache);
  673. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  674. i:=0;
  675. while (i<InsTabEntries) do
  676. begin
  677. if InsTabCache^[InsTab[i].Opcode]=-1 then
  678. InsTabCache^[InsTab[i].Opcode]:=i;
  679. inc(i);
  680. end;
  681. end;
  682. procedure InitAsm;
  683. begin
  684. if not assigned(instabcache) then
  685. BuildInsTabCache;
  686. end;
  687. procedure DoneAsm;
  688. begin
  689. if assigned(instabcache) then
  690. begin
  691. dispose(instabcache);
  692. instabcache:=nil;
  693. end;
  694. end;
  695. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  696. begin
  697. i.oppostfix:=pf;
  698. result:=i;
  699. end;
  700. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  701. begin
  702. i.roundingmode:=rm;
  703. result:=i;
  704. end;
  705. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  706. begin
  707. i.condition:=c;
  708. result:=i;
  709. end;
  710. Function SimpleGetNextInstruction(Current: tai; Var Next: tai): Boolean;
  711. Begin
  712. Current:=tai(Current.Next);
  713. While Assigned(Current) And (Current.typ In SkipInstr) Do
  714. Current:=tai(Current.Next);
  715. Next:=Current;
  716. If Assigned(Next) And Not(Next.typ In SkipInstr) Then
  717. Result:=True
  718. Else
  719. Begin
  720. Next:=Nil;
  721. Result:=False;
  722. End;
  723. End;
  724. (*
  725. function armconstequal(hp1,hp2: tai): boolean;
  726. begin
  727. result:=false;
  728. if hp1.typ<>hp2.typ then
  729. exit;
  730. case hp1.typ of
  731. tai_const:
  732. result:=
  733. (tai_const(hp2).sym=tai_const(hp).sym) and
  734. (tai_const(hp2).value=tai_const(hp).value) and
  735. (tai(hp2.previous).typ=ait_label);
  736. tai_const:
  737. result:=
  738. (tai_const(hp2).sym=tai_const(hp).sym) and
  739. (tai_const(hp2).value=tai_const(hp).value) and
  740. (tai(hp2.previous).typ=ait_label);
  741. end;
  742. end;
  743. *)
  744. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  745. var
  746. limit: longint;
  747. { FLD/FST VFP instructions have a limit of +/- 1024, not 4096, this
  748. function checks the next count instructions if the limit must be
  749. decreased }
  750. procedure CheckLimit(hp : tai;count : integer);
  751. var
  752. i : Integer;
  753. begin
  754. for i:=1 to count do
  755. if SimpleGetNextInstruction(hp,hp) and
  756. (tai(hp).typ=ait_instruction) and
  757. ((taicpu(hp).opcode=A_FLDS) or
  758. (taicpu(hp).opcode=A_FLDD) or
  759. (taicpu(hp).opcode=A_VLDR)) then
  760. limit:=254;
  761. end;
  762. function is_case_dispatch(hp: taicpu): boolean;
  763. begin
  764. result:=
  765. ((taicpu(hp).opcode in [A_ADD,A_LDR]) and
  766. not(GenerateThumbCode or GenerateThumb2Code) and
  767. (taicpu(hp).oper[0]^.typ=top_reg) and
  768. (taicpu(hp).oper[0]^.reg=NR_PC)) or
  769. ((taicpu(hp).opcode=A_MOV) and (GenerateThumbCode) and
  770. (taicpu(hp).oper[0]^.typ=top_reg) and
  771. (taicpu(hp).oper[0]^.reg=NR_PC)) or
  772. (taicpu(hp).opcode=A_TBH) or
  773. (taicpu(hp).opcode=A_TBB);
  774. end;
  775. var
  776. curinspos,
  777. penalty,
  778. lastinspos,
  779. { increased for every data element > 4 bytes inserted }
  780. currentsize,
  781. extradataoffset,
  782. curop : longint;
  783. curtai,
  784. inserttai : tai;
  785. ai_label : tai_label;
  786. curdatatai,hp,hp2 : tai;
  787. curdata : TAsmList;
  788. l : tasmlabel;
  789. doinsert,
  790. removeref : boolean;
  791. multiplier : byte;
  792. begin
  793. curdata:=TAsmList.create;
  794. lastinspos:=-1;
  795. curinspos:=0;
  796. extradataoffset:=0;
  797. if GenerateThumbCode then
  798. begin
  799. multiplier:=2;
  800. limit:=504;
  801. end
  802. else
  803. begin
  804. limit:=1016;
  805. multiplier:=1;
  806. end;
  807. curtai:=tai(list.first);
  808. doinsert:=false;
  809. while assigned(curtai) do
  810. begin
  811. { instruction? }
  812. case curtai.typ of
  813. ait_instruction:
  814. begin
  815. { walk through all operand of the instruction }
  816. for curop:=0 to taicpu(curtai).ops-1 do
  817. begin
  818. { reference? }
  819. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  820. begin
  821. { pc relative symbol? }
  822. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  823. if assigned(curdatatai) then
  824. begin
  825. { create a new copy of a data entry on arm thumb if the entry has been inserted already
  826. before because arm thumb does not allow pc relative negative offsets }
  827. if (GenerateThumbCode) and
  828. tai_label(curdatatai).inserted then
  829. begin
  830. current_asmdata.getjumplabel(l);
  831. hp:=tai_label.create(l);
  832. listtoinsert.Concat(hp);
  833. hp2:=tai(curdatatai.Next.GetCopy);
  834. hp2.Next:=nil;
  835. hp2.Previous:=nil;
  836. listtoinsert.Concat(hp2);
  837. taicpu(curtai).oper[curop]^.ref^.symboldata:=hp;
  838. taicpu(curtai).oper[curop]^.ref^.symbol:=l;
  839. curdatatai:=hp;
  840. end;
  841. { move only if we're at the first reference of a label }
  842. if not(tai_label(curdatatai).moved) then
  843. begin
  844. tai_label(curdatatai).moved:=true;
  845. { check if symbol already used. }
  846. { if yes, reuse the symbol }
  847. hp:=tai(curdatatai.next);
  848. removeref:=false;
  849. if assigned(hp) then
  850. begin
  851. case hp.typ of
  852. ait_const:
  853. begin
  854. if (tai_const(hp).consttype=aitconst_64bit) then
  855. inc(extradataoffset,multiplier);
  856. end;
  857. ait_realconst:
  858. begin
  859. inc(extradataoffset,multiplier*(((tai_realconst(hp).savesize-4)+3) div 4));
  860. end;
  861. end;
  862. { check if the same constant has been already inserted into the currently handled list,
  863. if yes, reuse it }
  864. if (hp.typ=ait_const) then
  865. begin
  866. hp2:=tai(curdata.first);
  867. while assigned(hp2) do
  868. begin
  869. if (hp2.typ=ait_const) and (tai_const(hp2).sym=tai_const(hp).sym)
  870. and (tai_const(hp2).value=tai_const(hp).value) and (tai(hp2.previous).typ=ait_label)
  871. then
  872. begin
  873. with taicpu(curtai).oper[curop]^.ref^ do
  874. begin
  875. symboldata:=hp2.previous;
  876. symbol:=tai_label(hp2.previous).labsym;
  877. end;
  878. removeref:=true;
  879. break;
  880. end;
  881. hp2:=tai(hp2.next);
  882. end;
  883. end;
  884. end;
  885. { move or remove symbol reference }
  886. repeat
  887. hp:=tai(curdatatai.next);
  888. listtoinsert.remove(curdatatai);
  889. if removeref then
  890. curdatatai.free
  891. else
  892. curdata.concat(curdatatai);
  893. curdatatai:=hp;
  894. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  895. if lastinspos=-1 then
  896. lastinspos:=curinspos;
  897. end;
  898. end;
  899. end;
  900. end;
  901. inc(curinspos,multiplier);
  902. end;
  903. ait_align:
  904. begin
  905. { code is always 4 byte aligned, so we don't have to take care of .align 2 which would
  906. requires also incrementing curinspos by 1 }
  907. inc(curinspos,(tai_align(curtai).aligntype div 4)*multiplier);
  908. end;
  909. ait_const:
  910. begin
  911. inc(curinspos,multiplier);
  912. if (tai_const(curtai).consttype=aitconst_64bit) then
  913. inc(curinspos,multiplier);
  914. end;
  915. ait_realconst:
  916. begin
  917. inc(curinspos,multiplier*((tai_realconst(hp).savesize+3) div 4));
  918. end;
  919. end;
  920. { special case for case jump tables }
  921. penalty:=0;
  922. if SimpleGetNextInstruction(curtai,hp) and
  923. (tai(hp).typ=ait_instruction) then
  924. begin
  925. case taicpu(hp).opcode of
  926. A_MOV,
  927. A_LDR,
  928. A_ADD,
  929. A_TBH,
  930. A_TBB:
  931. { approximation if we hit a case jump table }
  932. if is_case_dispatch(taicpu(hp)) then
  933. begin
  934. penalty:=multiplier;
  935. hp:=tai(hp.next);
  936. { skip register allocations and comments inserted by the optimizer as well as a label
  937. as jump tables for thumb might have }
  938. while assigned(hp) and (hp.typ in [ait_comment,ait_regalloc,ait_label]) do
  939. hp:=tai(hp.next);
  940. while assigned(hp) and (hp.typ=ait_const) do
  941. begin
  942. inc(penalty,multiplier);
  943. hp:=tai(hp.next);
  944. end;
  945. end;
  946. A_IT:
  947. begin
  948. if GenerateThumb2Code then
  949. penalty:=multiplier;
  950. { check if the next instruction fits as well
  951. or if we splitted after the it so split before }
  952. CheckLimit(hp,1);
  953. end;
  954. A_ITE,
  955. A_ITT:
  956. begin
  957. if GenerateThumb2Code then
  958. penalty:=2*multiplier;
  959. { check if the next two instructions fit as well
  960. or if we splitted them so split before }
  961. CheckLimit(hp,2);
  962. end;
  963. A_ITEE,
  964. A_ITTE,
  965. A_ITET,
  966. A_ITTT:
  967. begin
  968. if GenerateThumb2Code then
  969. penalty:=3*multiplier;
  970. { check if the next three instructions fit as well
  971. or if we splitted them so split before }
  972. CheckLimit(hp,3);
  973. end;
  974. A_ITEEE,
  975. A_ITTEE,
  976. A_ITETE,
  977. A_ITTTE,
  978. A_ITEET,
  979. A_ITTET,
  980. A_ITETT,
  981. A_ITTTT:
  982. begin
  983. if GenerateThumb2Code then
  984. penalty:=4*multiplier;
  985. { check if the next three instructions fit as well
  986. or if we splitted them so split before }
  987. CheckLimit(hp,4);
  988. end;
  989. end;
  990. end;
  991. CheckLimit(curtai,1);
  992. { don't miss an insert }
  993. doinsert:=doinsert or
  994. (not(curdata.empty) and
  995. (curinspos-lastinspos+penalty+extradataoffset>limit));
  996. { split only at real instructions else the test below fails }
  997. if doinsert and (curtai.typ=ait_instruction) and
  998. (
  999. { don't split loads of pc to lr and the following move }
  1000. not(
  1001. (taicpu(curtai).opcode=A_MOV) and
  1002. (taicpu(curtai).oper[0]^.typ=top_reg) and
  1003. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  1004. (taicpu(curtai).oper[1]^.typ=top_reg) and
  1005. (taicpu(curtai).oper[1]^.reg=NR_PC)
  1006. )
  1007. ) and
  1008. (
  1009. { do not insert data after a B instruction due to their limited range }
  1010. not((GenerateThumbCode) and
  1011. (taicpu(curtai).opcode=A_B)
  1012. )
  1013. ) then
  1014. begin
  1015. lastinspos:=-1;
  1016. extradataoffset:=0;
  1017. if GenerateThumbCode then
  1018. limit:=502
  1019. else
  1020. limit:=1016;
  1021. { if this is an add/tbh/tbb-based jumptable, go back to the
  1022. previous instruction, because inserting data between the
  1023. dispatch instruction and the table would mess up the
  1024. addresses }
  1025. inserttai:=curtai;
  1026. if is_case_dispatch(taicpu(inserttai)) and
  1027. ((taicpu(inserttai).opcode=A_ADD) or
  1028. (taicpu(inserttai).opcode=A_TBH) or
  1029. (taicpu(inserttai).opcode=A_TBB)) then
  1030. begin
  1031. repeat
  1032. inserttai:=tai(inserttai.previous);
  1033. until inserttai.typ=ait_instruction;
  1034. { if it's an add-based jump table, then also skip the
  1035. pc-relative load }
  1036. if taicpu(curtai).opcode=A_ADD then
  1037. repeat
  1038. inserttai:=tai(inserttai.previous);
  1039. until inserttai.typ=ait_instruction;
  1040. end
  1041. else
  1042. { on arm thumb, insert the data always after all labels etc. following an instruction so it
  1043. is prevent that a bxx yyy; bl xxx; yyyy: sequence gets separated ( we never insert on arm thumb after
  1044. bxx) and the distance of bxx gets too long }
  1045. if GenerateThumbCode then
  1046. while assigned(tai(inserttai.Next)) and (tai(inserttai.Next).typ in SkipInstr+[ait_label]) do
  1047. inserttai:=tai(inserttai.next);
  1048. doinsert:=false;
  1049. current_asmdata.getjumplabel(l);
  1050. { align jump in thumb .text section to 4 bytes }
  1051. if not(curdata.empty) and (GenerateThumbCode) then
  1052. curdata.Insert(tai_align.Create(4));
  1053. curdata.insert(taicpu.op_sym(A_B,l));
  1054. curdata.concat(tai_label.create(l));
  1055. { mark all labels as inserted, arm thumb
  1056. needs this, so data referencing an already inserted label can be
  1057. duplicated because arm thumb does not allow negative pc relative offset }
  1058. hp2:=tai(curdata.first);
  1059. while assigned(hp2) do
  1060. begin
  1061. if hp2.typ=ait_label then
  1062. tai_label(hp2).inserted:=true;
  1063. hp2:=tai(hp2.next);
  1064. end;
  1065. { continue with the last inserted label because we use later
  1066. on SimpleGetNextInstruction, so if we used curtai.next (which
  1067. is then equal curdata.last.previous) we could over see one
  1068. instruction }
  1069. hp:=tai(curdata.Last);
  1070. list.insertlistafter(inserttai,curdata);
  1071. curtai:=hp;
  1072. end
  1073. else
  1074. curtai:=tai(curtai.next);
  1075. end;
  1076. { align jump in thumb .text section to 4 bytes }
  1077. if not(curdata.empty) and (GenerateThumbCode or GenerateThumb2Code) then
  1078. curdata.Insert(tai_align.Create(4));
  1079. list.concatlist(curdata);
  1080. curdata.free;
  1081. end;
  1082. procedure ensurethumb2encodings(list: TAsmList);
  1083. var
  1084. curtai: tai;
  1085. op2reg: TRegister;
  1086. begin
  1087. { Do Thumb-2 16bit -> 32bit transformations }
  1088. curtai:=tai(list.first);
  1089. while assigned(curtai) do
  1090. begin
  1091. case curtai.typ of
  1092. ait_instruction:
  1093. begin
  1094. case taicpu(curtai).opcode of
  1095. A_ADD:
  1096. begin
  1097. { Set wide flag for ADD Rd,Rn,Rm where registers are over R7(high register set) }
  1098. if taicpu(curtai).ops = 3 then
  1099. begin
  1100. if taicpu(curtai).oper[2]^.typ in [top_reg,top_shifterop] then
  1101. begin
  1102. if taicpu(curtai).oper[2]^.typ = top_reg then
  1103. op2reg := taicpu(curtai).oper[2]^.reg
  1104. else if taicpu(curtai).oper[2]^.shifterop^.rs <> NR_NO then
  1105. op2reg := taicpu(curtai).oper[2]^.shifterop^.rs
  1106. else
  1107. op2reg := NR_NO;
  1108. if op2reg <> NR_NO then
  1109. begin
  1110. if (taicpu(curtai).oper[0]^.reg >= NR_R8) or
  1111. (taicpu(curtai).oper[1]^.reg >= NR_R8) or
  1112. (op2reg >= NR_R8) then
  1113. begin
  1114. taicpu(curtai).wideformat:=true;
  1115. { Handle special cases where register rules are violated by optimizer/user }
  1116. { if d == 13 || (d == 15 && S == ‘0’) || n == 15 || m IN [13,15] then UNPREDICTABLE; }
  1117. { Transform ADD.W Rx, Ry, R13 into ADD.W Rx, R13, Ry }
  1118. if (op2reg = NR_R13) and (taicpu(curtai).oper[2]^.typ = top_reg) then
  1119. begin
  1120. taicpu(curtai).oper[2]^.reg := taicpu(curtai).oper[1]^.reg;
  1121. taicpu(curtai).oper[1]^.reg := op2reg;
  1122. end;
  1123. end;
  1124. end;
  1125. end;
  1126. end;
  1127. end;
  1128. end;
  1129. end;
  1130. end;
  1131. curtai:=tai(curtai.Next);
  1132. end;
  1133. end;
  1134. function getMergedInstruction(FirstOp,LastOp:TAsmOp;InvertLast:boolean) : TAsmOp;
  1135. const
  1136. opTable: array[A_IT..A_ITTTT] of string =
  1137. ('T','TE','TT','TEE','TTE','TET','TTT',
  1138. 'TEEE','TTEE','TETE','TTTE',
  1139. 'TEET','TTET','TETT','TTTT');
  1140. invertedOpTable: array[A_IT..A_ITTTT] of string =
  1141. ('E','ET','EE','ETT','EET','ETE','EEE',
  1142. 'ETTT','EETT','ETET','EEET',
  1143. 'ETTE','EETE','ETEE','EEEE');
  1144. var
  1145. resStr : string;
  1146. i : TAsmOp;
  1147. begin
  1148. if InvertLast then
  1149. resStr := opTable[FirstOp]+invertedOpTable[LastOp]
  1150. else
  1151. resStr := opTable[FirstOp]+opTable[LastOp];
  1152. if length(resStr) > 4 then
  1153. internalerror(2012100805);
  1154. for i := low(opTable) to high(opTable) do
  1155. if opTable[i] = resStr then
  1156. exit(i);
  1157. internalerror(2012100806);
  1158. end;
  1159. procedure foldITInstructions(list: TAsmList);
  1160. var
  1161. curtai,hp1 : tai;
  1162. levels,i : LongInt;
  1163. begin
  1164. curtai:=tai(list.First);
  1165. while assigned(curtai) do
  1166. begin
  1167. case curtai.typ of
  1168. ait_instruction:
  1169. if IsIT(taicpu(curtai).opcode) then
  1170. begin
  1171. levels := GetITLevels(taicpu(curtai).opcode);
  1172. if levels < 4 then
  1173. begin
  1174. i:=levels;
  1175. hp1:=tai(curtai.Next);
  1176. while assigned(hp1) and
  1177. (i > 0) do
  1178. begin
  1179. if hp1.typ=ait_instruction then
  1180. begin
  1181. dec(i);
  1182. if (i = 0) and
  1183. mustbelast(hp1) then
  1184. begin
  1185. hp1:=nil;
  1186. break;
  1187. end;
  1188. end;
  1189. hp1:=tai(hp1.Next);
  1190. end;
  1191. if assigned(hp1) then
  1192. begin
  1193. // We are pointing at the first instruction after the IT block
  1194. while assigned(hp1) and
  1195. (hp1.typ<>ait_instruction) do
  1196. hp1:=tai(hp1.Next);
  1197. if assigned(hp1) and
  1198. (hp1.typ=ait_instruction) and
  1199. IsIT(taicpu(hp1).opcode) then
  1200. begin
  1201. if (levels+GetITLevels(taicpu(hp1).opcode) <= 4) and
  1202. ((taicpu(curtai).oper[0]^.cc=taicpu(hp1).oper[0]^.cc) or
  1203. (taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc))) then
  1204. begin
  1205. taicpu(curtai).opcode:=getMergedInstruction(taicpu(curtai).opcode,
  1206. taicpu(hp1).opcode,
  1207. taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc));
  1208. list.Remove(hp1);
  1209. hp1.Free;
  1210. end;
  1211. end;
  1212. end;
  1213. end;
  1214. end;
  1215. end;
  1216. curtai:=tai(curtai.Next);
  1217. end;
  1218. end;
  1219. procedure finalizearmcode(list, listtoinsert: TAsmList);
  1220. begin
  1221. { Do Thumb-2 16bit -> 32bit transformations }
  1222. if GenerateThumb2Code then
  1223. begin
  1224. ensurethumb2encodings(list);
  1225. foldITInstructions(list);
  1226. end;
  1227. insertpcrelativedata(list, listtoinsert);
  1228. end;
  1229. procedure InsertPData;
  1230. var
  1231. prolog: TAsmList;
  1232. begin
  1233. prolog:=TAsmList.create;
  1234. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(pint),secorder_begin);
  1235. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  1236. prolog.concat(Tai_const.Create_32bit(0));
  1237. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_DATA,0));
  1238. { dummy function }
  1239. prolog.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14));
  1240. current_asmdata.asmlists[al_start].insertList(prolog);
  1241. prolog.Free;
  1242. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(pint));
  1243. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  1244. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit(longint($ffffff01)));
  1245. end;
  1246. (*
  1247. Floating point instruction format information, taken from the linux kernel
  1248. ARM Floating Point Instruction Classes
  1249. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1250. |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
  1251. |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)
  1252. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1253. |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
  1254. |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
  1255. |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
  1256. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1257. CPDT data transfer instructions
  1258. LDF, STF, LFM (copro 2), SFM (copro 2)
  1259. CPDO dyadic arithmetic instructions
  1260. ADF, MUF, SUF, RSF, DVF, RDF,
  1261. POW, RPW, RMF, FML, FDV, FRD, POL
  1262. CPDO monadic arithmetic instructions
  1263. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  1264. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  1265. CPRT joint arithmetic/data transfer instructions
  1266. FIX (arithmetic followed by load/store)
  1267. FLT (load/store followed by arithmetic)
  1268. CMF, CNF CMFE, CNFE (comparisons)
  1269. WFS, RFS (write/read floating point status register)
  1270. WFC, RFC (write/read floating point control register)
  1271. cond condition codes
  1272. P pre/post index bit: 0 = postindex, 1 = preindex
  1273. U up/down bit: 0 = stack grows down, 1 = stack grows up
  1274. W write back bit: 1 = update base register (Rn)
  1275. L load/store bit: 0 = store, 1 = load
  1276. Rn base register
  1277. Rd destination/source register
  1278. Fd floating point destination register
  1279. Fn floating point source register
  1280. Fm floating point source register or floating point constant
  1281. uv transfer length (TABLE 1)
  1282. wx register count (TABLE 2)
  1283. abcd arithmetic opcode (TABLES 3 & 4)
  1284. ef destination size (rounding precision) (TABLE 5)
  1285. gh rounding mode (TABLE 6)
  1286. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  1287. i constant bit: 1 = constant (TABLE 6)
  1288. */
  1289. /*
  1290. TABLE 1
  1291. +-------------------------+---+---+---------+---------+
  1292. | Precision | u | v | FPSR.EP | length |
  1293. +-------------------------+---+---+---------+---------+
  1294. | Single | 0 | 0 | x | 1 words |
  1295. | Double | 1 | 1 | x | 2 words |
  1296. | Extended | 1 | 1 | x | 3 words |
  1297. | Packed decimal | 1 | 1 | 0 | 3 words |
  1298. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  1299. +-------------------------+---+---+---------+---------+
  1300. Note: x = don't care
  1301. */
  1302. /*
  1303. TABLE 2
  1304. +---+---+---------------------------------+
  1305. | w | x | Number of registers to transfer |
  1306. +---+---+---------------------------------+
  1307. | 0 | 1 | 1 |
  1308. | 1 | 0 | 2 |
  1309. | 1 | 1 | 3 |
  1310. | 0 | 0 | 4 |
  1311. +---+---+---------------------------------+
  1312. */
  1313. /*
  1314. TABLE 3: Dyadic Floating Point Opcodes
  1315. +---+---+---+---+----------+-----------------------+-----------------------+
  1316. | a | b | c | d | Mnemonic | Description | Operation |
  1317. +---+---+---+---+----------+-----------------------+-----------------------+
  1318. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  1319. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  1320. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  1321. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  1322. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  1323. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  1324. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  1325. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  1326. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  1327. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  1328. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  1329. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  1330. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  1331. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  1332. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  1333. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  1334. +---+---+---+---+----------+-----------------------+-----------------------+
  1335. Note: POW, RPW, POL are deprecated, and are available for backwards
  1336. compatibility only.
  1337. */
  1338. /*
  1339. TABLE 4: Monadic Floating Point Opcodes
  1340. +---+---+---+---+----------+-----------------------+-----------------------+
  1341. | a | b | c | d | Mnemonic | Description | Operation |
  1342. +---+---+---+---+----------+-----------------------+-----------------------+
  1343. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  1344. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  1345. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  1346. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  1347. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  1348. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  1349. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  1350. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  1351. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  1352. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  1353. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  1354. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  1355. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  1356. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  1357. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  1358. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  1359. +---+---+---+---+----------+-----------------------+-----------------------+
  1360. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  1361. available for backwards compatibility only.
  1362. */
  1363. /*
  1364. TABLE 5
  1365. +-------------------------+---+---+
  1366. | Rounding Precision | e | f |
  1367. +-------------------------+---+---+
  1368. | IEEE Single precision | 0 | 0 |
  1369. | IEEE Double precision | 0 | 1 |
  1370. | IEEE Extended precision | 1 | 0 |
  1371. | undefined (trap) | 1 | 1 |
  1372. +-------------------------+---+---+
  1373. */
  1374. /*
  1375. TABLE 5
  1376. +---------------------------------+---+---+
  1377. | Rounding Mode | g | h |
  1378. +---------------------------------+---+---+
  1379. | Round to nearest (default) | 0 | 0 |
  1380. | Round toward plus infinity | 0 | 1 |
  1381. | Round toward negative infinity | 1 | 0 |
  1382. | Round toward zero | 1 | 1 |
  1383. +---------------------------------+---+---+
  1384. *)
  1385. function taicpu.GetString:string;
  1386. var
  1387. i : longint;
  1388. s : string;
  1389. addsize : boolean;
  1390. begin
  1391. s:='['+gas_op2str[opcode];
  1392. for i:=0 to ops-1 do
  1393. begin
  1394. with oper[i]^ do
  1395. begin
  1396. if i=0 then
  1397. s:=s+' '
  1398. else
  1399. s:=s+',';
  1400. { type }
  1401. addsize:=false;
  1402. if (ot and OT_VREG)=OT_VREG then
  1403. s:=s+'vreg'
  1404. else
  1405. if (ot and OT_FPUREG)=OT_FPUREG then
  1406. s:=s+'fpureg'
  1407. else
  1408. if (ot and OT_REGISTER)=OT_REGISTER then
  1409. begin
  1410. s:=s+'reg';
  1411. addsize:=true;
  1412. end
  1413. else
  1414. if (ot and OT_REGLIST)=OT_REGLIST then
  1415. begin
  1416. s:=s+'reglist';
  1417. addsize:=false;
  1418. end
  1419. else
  1420. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  1421. begin
  1422. s:=s+'imm';
  1423. addsize:=true;
  1424. end
  1425. else
  1426. if (ot and OT_MEMORY)=OT_MEMORY then
  1427. begin
  1428. s:=s+'mem';
  1429. addsize:=true;
  1430. if (ot and OT_AM2)<>0 then
  1431. s:=s+' am2 ';
  1432. end
  1433. else
  1434. if (ot and OT_SHIFTEROP)=OT_SHIFTEROP then
  1435. begin
  1436. s:=s+'shifterop';
  1437. addsize:=false;
  1438. end
  1439. else
  1440. s:=s+'???';
  1441. { size }
  1442. if addsize then
  1443. begin
  1444. if (ot and OT_BITS8)<>0 then
  1445. s:=s+'8'
  1446. else
  1447. if (ot and OT_BITS16)<>0 then
  1448. s:=s+'24'
  1449. else
  1450. if (ot and OT_BITS32)<>0 then
  1451. s:=s+'32'
  1452. else
  1453. if (ot and OT_BITSSHIFTER)<>0 then
  1454. s:=s+'shifter'
  1455. else
  1456. s:=s+'??';
  1457. { signed }
  1458. if (ot and OT_SIGNED)<>0 then
  1459. s:=s+'s';
  1460. end;
  1461. end;
  1462. end;
  1463. GetString:=s+']';
  1464. end;
  1465. procedure taicpu.ResetPass1;
  1466. begin
  1467. { we need to reset everything here, because the choosen insentry
  1468. can be invalid for a new situation where the previously optimized
  1469. insentry is not correct }
  1470. InsEntry:=nil;
  1471. InsSize:=0;
  1472. LastInsOffset:=-1;
  1473. end;
  1474. procedure taicpu.ResetPass2;
  1475. begin
  1476. { we are here in a second pass, check if the instruction can be optimized }
  1477. if assigned(InsEntry) and
  1478. ((InsEntry^.flags and IF_PASS2)<>0) then
  1479. begin
  1480. InsEntry:=nil;
  1481. InsSize:=0;
  1482. end;
  1483. LastInsOffset:=-1;
  1484. end;
  1485. function taicpu.CheckIfValid:boolean;
  1486. begin
  1487. Result:=False; { unimplemented }
  1488. end;
  1489. function taicpu.Pass1(objdata:TObjData):longint;
  1490. var
  1491. ldr2op : array[PF_B..PF_T] of tasmop = (
  1492. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  1493. str2op : array[PF_B..PF_T] of tasmop = (
  1494. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  1495. begin
  1496. Pass1:=0;
  1497. { Save the old offset and set the new offset }
  1498. InsOffset:=ObjData.CurrObjSec.Size;
  1499. { Error? }
  1500. if (Insentry=nil) and (InsSize=-1) then
  1501. exit;
  1502. { set the file postion }
  1503. current_filepos:=fileinfo;
  1504. { tranlate LDR+postfix to complete opcode }
  1505. if (opcode=A_LDR) and (oppostfix<>PF_None) then
  1506. begin
  1507. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  1508. opcode:=ldr2op[oppostfix]
  1509. else
  1510. internalerror(2005091001);
  1511. if opcode=A_None then
  1512. internalerror(2005091004);
  1513. { postfix has been added to opcode }
  1514. oppostfix:=PF_None;
  1515. end
  1516. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  1517. begin
  1518. if (oppostfix in [low(str2op)..high(str2op)]) then
  1519. opcode:=str2op[oppostfix]
  1520. else
  1521. internalerror(2005091002);
  1522. if opcode=A_None then
  1523. internalerror(2005091003);
  1524. { postfix has been added to opcode }
  1525. oppostfix:=PF_None;
  1526. end;
  1527. { Get InsEntry }
  1528. if FindInsEntry(objdata) then
  1529. begin
  1530. InsSize:=4;
  1531. LastInsOffset:=InsOffset;
  1532. Pass1:=InsSize;
  1533. exit;
  1534. end;
  1535. LastInsOffset:=-1;
  1536. end;
  1537. procedure taicpu.Pass2(objdata:TObjData);
  1538. begin
  1539. { error in pass1 ? }
  1540. if insentry=nil then
  1541. exit;
  1542. current_filepos:=fileinfo;
  1543. { Generate the instruction }
  1544. GenCode(objdata);
  1545. end;
  1546. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1547. begin
  1548. end;
  1549. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1550. begin
  1551. end;
  1552. procedure taicpu.ppubuildderefimploper(var o:toper);
  1553. begin
  1554. end;
  1555. procedure taicpu.ppuderefoper(var o:toper);
  1556. begin
  1557. end;
  1558. function taicpu.InsEnd:longint;
  1559. begin
  1560. Result:=0; { unimplemented }
  1561. end;
  1562. procedure taicpu.create_ot(objdata:TObjData);
  1563. var
  1564. i,l,relsize : longint;
  1565. dummy : byte;
  1566. currsym : TObjSymbol;
  1567. begin
  1568. if ops=0 then
  1569. exit;
  1570. { update oper[].ot field }
  1571. for i:=0 to ops-1 do
  1572. with oper[i]^ do
  1573. begin
  1574. case typ of
  1575. top_regset:
  1576. begin
  1577. ot:=OT_REGLIST;
  1578. end;
  1579. top_reg :
  1580. begin
  1581. case getregtype(reg) of
  1582. R_INTREGISTER:
  1583. ot:=OT_REG32 or OT_SHIFTEROP;
  1584. R_FPUREGISTER:
  1585. ot:=OT_FPUREG;
  1586. else
  1587. internalerror(2005090901);
  1588. end;
  1589. end;
  1590. top_ref :
  1591. begin
  1592. if ref^.refaddr=addr_no then
  1593. begin
  1594. { create ot field }
  1595. { we should get the size here dependend on the
  1596. instruction }
  1597. if (ot and OT_SIZE_MASK)=0 then
  1598. ot:=OT_MEMORY or OT_BITS32
  1599. else
  1600. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1601. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1602. ot:=ot or OT_MEM_OFFS;
  1603. { if we need to fix a reference, we do it here }
  1604. { pc relative addressing }
  1605. if (ref^.base=NR_NO) and
  1606. (ref^.index=NR_NO) and
  1607. (ref^.shiftmode=SM_None)
  1608. { at least we should check if the destination symbol
  1609. is in a text section }
  1610. { and
  1611. (ref^.symbol^.owner="text") } then
  1612. ref^.base:=NR_PC;
  1613. { determine possible address modes }
  1614. if (ref^.base<>NR_NO) and
  1615. (
  1616. (
  1617. (ref^.index=NR_NO) and
  1618. (ref^.shiftmode=SM_None) and
  1619. (ref^.offset>=-4097) and
  1620. (ref^.offset<=4097)
  1621. ) or
  1622. (
  1623. (ref^.shiftmode=SM_None) and
  1624. (ref^.offset=0)
  1625. ) or
  1626. (
  1627. (ref^.index<>NR_NO) and
  1628. (ref^.shiftmode<>SM_None) and
  1629. (ref^.shiftimm<=31) and
  1630. (ref^.offset=0)
  1631. )
  1632. ) then
  1633. ot:=ot or OT_AM2;
  1634. if (ref^.index<>NR_NO) and
  1635. (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1636. (
  1637. (ref^.base=NR_NO) and
  1638. (ref^.shiftmode=SM_None) and
  1639. (ref^.offset=0)
  1640. ) then
  1641. ot:=ot or OT_AM4;
  1642. end
  1643. else
  1644. begin
  1645. l:=ref^.offset;
  1646. currsym:=ObjData.symbolref(ref^.symbol);
  1647. if assigned(currsym) then
  1648. inc(l,currsym.address);
  1649. relsize:=(InsOffset+2)-l;
  1650. if (relsize<-33554428) or (relsize>33554428) then
  1651. ot:=OT_IMM32
  1652. else
  1653. ot:=OT_IMM24;
  1654. end;
  1655. end;
  1656. top_local :
  1657. begin
  1658. { we should get the size here dependend on the
  1659. instruction }
  1660. if (ot and OT_SIZE_MASK)=0 then
  1661. ot:=OT_MEMORY or OT_BITS32
  1662. else
  1663. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1664. end;
  1665. top_const :
  1666. begin
  1667. ot:=OT_IMMEDIATE;
  1668. if is_shifter_const(val,dummy) then
  1669. ot:=OT_IMMSHIFTER
  1670. else
  1671. ot:=OT_IMM32
  1672. end;
  1673. top_none :
  1674. begin
  1675. { generated when there was an error in the
  1676. assembler reader. It never happends when generating
  1677. assembler }
  1678. end;
  1679. top_shifterop:
  1680. begin
  1681. ot:=OT_SHIFTEROP;
  1682. end;
  1683. top_conditioncode:
  1684. ot:=OT_CONDITION;
  1685. else
  1686. internalerror(2004022623);
  1687. end;
  1688. end;
  1689. end;
  1690. function taicpu.Matches(p:PInsEntry):longint;
  1691. { * IF_SM stands for Size Match: any operand whose size is not
  1692. * explicitly specified by the template is `really' intended to be
  1693. * the same size as the first size-specified operand.
  1694. * Non-specification is tolerated in the input instruction, but
  1695. * _wrong_ specification is not.
  1696. *
  1697. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1698. * three-operand instructions such as SHLD: it implies that the
  1699. * first two operands must match in size, but that the third is
  1700. * required to be _unspecified_.
  1701. *
  1702. * IF_SB invokes Size Byte: operands with unspecified size in the
  1703. * template are really bytes, and so no non-byte specification in
  1704. * the input instruction will be tolerated. IF_SW similarly invokes
  1705. * Size Word, and IF_SD invokes Size Doubleword.
  1706. *
  1707. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1708. * that any operand with unspecified size in the template is
  1709. * required to have unspecified size in the instruction too...)
  1710. }
  1711. var
  1712. i{,j,asize,oprs} : longint;
  1713. {siz : array[0..3] of longint;}
  1714. begin
  1715. Matches:=100;
  1716. { Check the opcode and operands }
  1717. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1718. begin
  1719. Matches:=0;
  1720. exit;
  1721. end;
  1722. { Check that no spurious colons or TOs are present }
  1723. for i:=0 to p^.ops-1 do
  1724. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  1725. begin
  1726. Matches:=0;
  1727. exit;
  1728. end;
  1729. { Check that the operand flags all match up }
  1730. for i:=0 to p^.ops-1 do
  1731. begin
  1732. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  1733. ((p^.optypes[i] and OT_SIZE_MASK) and
  1734. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  1735. begin
  1736. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  1737. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  1738. begin
  1739. Matches:=0;
  1740. exit;
  1741. end
  1742. else
  1743. Matches:=1;
  1744. end;
  1745. end;
  1746. { check postfixes:
  1747. the existance of a certain postfix requires a
  1748. particular code }
  1749. { update condition flags
  1750. or floating point single }
  1751. if (oppostfix=PF_S) and
  1752. not(p^.code[0] in [#$04..#$0B]) then
  1753. begin
  1754. Matches:=0;
  1755. exit;
  1756. end;
  1757. { floating point size }
  1758. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  1759. not(p^.code[0] in []) then
  1760. begin
  1761. Matches:=0;
  1762. exit;
  1763. end;
  1764. { multiple load/store address modes }
  1765. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1766. not(p^.code[0] in [
  1767. // ldr,str,ldrb,strb
  1768. #$17,
  1769. // stm,ldm
  1770. #$26
  1771. ]) then
  1772. begin
  1773. Matches:=0;
  1774. exit;
  1775. end;
  1776. { we shouldn't see any opsize prefixes here }
  1777. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  1778. begin
  1779. Matches:=0;
  1780. exit;
  1781. end;
  1782. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  1783. begin
  1784. Matches:=0;
  1785. exit;
  1786. end;
  1787. { Check operand sizes }
  1788. { as default an untyped size can get all the sizes, this is different
  1789. from nasm, but else we need to do a lot checking which opcodes want
  1790. size or not with the automatic size generation }
  1791. (*
  1792. asize:=longint($ffffffff);
  1793. if (p^.flags and IF_SB)<>0 then
  1794. asize:=OT_BITS8
  1795. else if (p^.flags and IF_SW)<>0 then
  1796. asize:=OT_BITS16
  1797. else if (p^.flags and IF_SD)<>0 then
  1798. asize:=OT_BITS32;
  1799. if (p^.flags and IF_ARMASK)<>0 then
  1800. begin
  1801. siz[0]:=0;
  1802. siz[1]:=0;
  1803. siz[2]:=0;
  1804. if (p^.flags and IF_AR0)<>0 then
  1805. siz[0]:=asize
  1806. else if (p^.flags and IF_AR1)<>0 then
  1807. siz[1]:=asize
  1808. else if (p^.flags and IF_AR2)<>0 then
  1809. siz[2]:=asize;
  1810. end
  1811. else
  1812. begin
  1813. { we can leave because the size for all operands is forced to be
  1814. the same
  1815. but not if IF_SB IF_SW or IF_SD is set PM }
  1816. if asize=-1 then
  1817. exit;
  1818. siz[0]:=asize;
  1819. siz[1]:=asize;
  1820. siz[2]:=asize;
  1821. end;
  1822. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1823. begin
  1824. if (p^.flags and IF_SM2)<>0 then
  1825. oprs:=2
  1826. else
  1827. oprs:=p^.ops;
  1828. for i:=0 to oprs-1 do
  1829. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1830. begin
  1831. for j:=0 to oprs-1 do
  1832. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1833. break;
  1834. end;
  1835. end
  1836. else
  1837. oprs:=2;
  1838. { Check operand sizes }
  1839. for i:=0 to p^.ops-1 do
  1840. begin
  1841. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1842. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1843. { Immediates can always include smaller size }
  1844. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1845. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1846. Matches:=2;
  1847. end;
  1848. *)
  1849. end;
  1850. function taicpu.calcsize(p:PInsEntry):shortint;
  1851. begin
  1852. result:=4;
  1853. end;
  1854. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  1855. begin
  1856. Result:=False; { unimplemented }
  1857. end;
  1858. procedure taicpu.Swapoperands;
  1859. begin
  1860. end;
  1861. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1862. var
  1863. i : longint;
  1864. begin
  1865. result:=false;
  1866. { Things which may only be done once, not when a second pass is done to
  1867. optimize }
  1868. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1869. begin
  1870. { create the .ot fields }
  1871. create_ot(objdata);
  1872. { set the file postion }
  1873. current_filepos:=fileinfo;
  1874. end
  1875. else
  1876. begin
  1877. { we've already an insentry so it's valid }
  1878. result:=true;
  1879. exit;
  1880. end;
  1881. { Lookup opcode in the table }
  1882. InsSize:=-1;
  1883. i:=instabcache^[opcode];
  1884. if i=-1 then
  1885. begin
  1886. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1887. exit;
  1888. end;
  1889. insentry:=@instab[i];
  1890. while (insentry^.opcode=opcode) do
  1891. begin
  1892. if matches(insentry)=100 then
  1893. begin
  1894. result:=true;
  1895. exit;
  1896. end;
  1897. inc(i);
  1898. insentry:=@instab[i];
  1899. end;
  1900. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1901. { No instruction found, set insentry to nil and inssize to -1 }
  1902. insentry:=nil;
  1903. inssize:=-1;
  1904. end;
  1905. procedure taicpu.gencode(objdata:TObjData);
  1906. const
  1907. CondVal : array[TAsmCond] of byte=(
  1908. $E, $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $A,
  1909. $B, $C, $D, $E, 0);
  1910. var
  1911. bytes : dword;
  1912. i_field : byte;
  1913. currsym : TObjSymbol;
  1914. offset : longint;
  1915. procedure setshifterop(op : byte);
  1916. var
  1917. r : byte;
  1918. imm : dword;
  1919. begin
  1920. case oper[op]^.typ of
  1921. top_const:
  1922. begin
  1923. i_field:=1;
  1924. if oper[op]^.val and $ff=oper[op]^.val then
  1925. bytes:=bytes or dword(oper[op]^.val)
  1926. else
  1927. begin
  1928. { calc rotate and adjust imm }
  1929. r:=0;
  1930. imm:=dword(oper[op]^.val);
  1931. repeat
  1932. imm:=RolDWord(imm, 2);
  1933. inc(r)
  1934. until imm and $ff=imm;
  1935. bytes:=bytes or (r shl 8) or imm;
  1936. end;
  1937. end;
  1938. top_reg:
  1939. begin
  1940. i_field:=0;
  1941. bytes:=bytes or getsupreg(oper[op]^.reg);
  1942. { does a real shifter op follow? }
  1943. if (op+1<opercnt) and (oper[op+1]^.typ=top_shifterop) then
  1944. with oper[op+1]^.shifterop^ do
  1945. begin
  1946. bytes:=bytes or (shiftimm shl 7);
  1947. if shiftmode<>SM_RRX then
  1948. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  1949. else
  1950. bytes:=bytes or (3 shl 5);
  1951. if getregtype(rs) <> R_INVALIDREGISTER then
  1952. begin
  1953. bytes:=bytes or (1 shl 4);
  1954. bytes:=bytes or (getsupreg(rs) shl 8);
  1955. end
  1956. end;
  1957. end;
  1958. else
  1959. internalerror(2005091103);
  1960. end;
  1961. end;
  1962. function MakeRegList(reglist: tcpuregisterset): word;
  1963. var
  1964. i, w: word;
  1965. begin
  1966. result:=0;
  1967. w:=1;
  1968. for i:=RS_R0 to RS_R15 do
  1969. begin
  1970. if i in reglist then
  1971. result:=result or w;
  1972. w:=w shl 1
  1973. end;
  1974. end;
  1975. begin
  1976. bytes:=$0;
  1977. i_field:=0;
  1978. { evaluate and set condition code }
  1979. bytes:=bytes or (CondVal[condition] shl 28);
  1980. { condition code allowed? }
  1981. { setup rest of the instruction }
  1982. case insentry^.code[0] of
  1983. #$01: // B/BL
  1984. begin
  1985. { set instruction code }
  1986. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  1987. { set offset }
  1988. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  1989. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  1990. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24)
  1991. else
  1992. bytes:=bytes or (((currsym.offset-insoffset-8) shr 2) and $ffffff);
  1993. end;
  1994. #$04..#$07: // SUB
  1995. begin
  1996. { set instruction code }
  1997. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  1998. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  1999. { set destination }
  2000. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2001. { set Rn }
  2002. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  2003. { create shifter op }
  2004. setshifterop(2);
  2005. { set I field }
  2006. bytes:=bytes or (i_field shl 25);
  2007. { set S if necessary }
  2008. if oppostfix=PF_S then
  2009. bytes:=bytes or (1 shl 20);
  2010. end;
  2011. #$08,#$0A,#$0B: // MOV
  2012. begin
  2013. { set instruction code }
  2014. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2015. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2016. { set destination }
  2017. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2018. { create shifter op }
  2019. setshifterop(1);
  2020. { set I field }
  2021. bytes:=bytes or (i_field shl 25);
  2022. { set S if necessary }
  2023. if oppostfix=PF_S then
  2024. bytes:=bytes or (1 shl 20);
  2025. end;
  2026. #$0C,#$0E,#$0F: // CMP
  2027. begin
  2028. { set instruction code }
  2029. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2030. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2031. { set destination }
  2032. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  2033. { create shifter op }
  2034. setshifterop(1);
  2035. { set I field }
  2036. bytes:=bytes or (i_field shl 25);
  2037. { always set S bit }
  2038. bytes:=bytes or (1 shl 20);
  2039. end;
  2040. #$14: // MUL/MLA r1,r2,r3
  2041. begin
  2042. { set instruction code }
  2043. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2044. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2045. bytes:=bytes or ord(insentry^.code[3]);
  2046. { set regs }
  2047. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2048. bytes:=bytes or getsupreg(oper[1]^.reg);
  2049. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2050. end;
  2051. #$15: // MUL/MLA r1,r2,r3,r4
  2052. begin
  2053. { set instruction code }
  2054. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2055. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2056. bytes:=bytes or ord(insentry^.code[3]);
  2057. { set regs }
  2058. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2059. bytes:=bytes or getsupreg(oper[1]^.reg);
  2060. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2061. bytes:=bytes or getsupreg(oper[3]^.reg) shl 12;
  2062. end;
  2063. #$16: // MULL r1,r2,r3,r4
  2064. begin
  2065. { set instruction code }
  2066. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2067. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2068. bytes:=bytes or ord(insentry^.code[3]);
  2069. { set regs }
  2070. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2071. bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
  2072. bytes:=bytes or getsupreg(oper[2]^.reg);
  2073. bytes:=bytes or getsupreg(oper[3]^.reg) shl 8;
  2074. end;
  2075. #$17: // LDR/STR
  2076. begin
  2077. { set instruction code }
  2078. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2079. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2080. { set Rn and Rd }
  2081. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2082. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2083. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2084. begin
  2085. { set offset }
  2086. offset:=0;
  2087. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  2088. if assigned(currsym) then
  2089. offset:=currsym.offset-insoffset-8;
  2090. offset:=offset+oper[1]^.ref^.offset;
  2091. if offset>=0 then
  2092. begin
  2093. { set U flag }
  2094. bytes:=bytes or (1 shl 23);
  2095. bytes:=bytes or offset
  2096. end
  2097. else
  2098. begin
  2099. offset:=-offset;
  2100. bytes:=bytes or offset
  2101. end;
  2102. end
  2103. else
  2104. begin
  2105. { set U flag }
  2106. if oper[1]^.ref^.signindex>0 then
  2107. bytes:=bytes or (1 shl 23);
  2108. { set I flag }
  2109. bytes:=bytes or (1 shl 25);
  2110. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  2111. { set shift }
  2112. with oper[1]^.ref^ do
  2113. if shiftmode<>SM_None then
  2114. begin
  2115. bytes:=bytes or (shiftimm shl 7);
  2116. if shiftmode<>SM_RRX then
  2117. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2118. else
  2119. bytes:=bytes or (3 shl 5);
  2120. end
  2121. end;
  2122. { set W bit }
  2123. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  2124. bytes:=bytes or (1 shl 21);
  2125. { set P bit if necessary }
  2126. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  2127. bytes:=bytes or (1 shl 24);
  2128. end;
  2129. #$22: // LDRH/STRH
  2130. begin
  2131. { set instruction code }
  2132. bytes:=bytes or (ord(insentry^.code[1]) shl 16);
  2133. bytes:=bytes or ord(insentry^.code[2]);
  2134. { src/dest register (Rd) }
  2135. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2136. { base register (Rn) }
  2137. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2138. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2139. begin
  2140. bytes:=bytes or (1 shl 22); // with immediate offset
  2141. if oper[1]^.ref^.offset < 0 then
  2142. begin
  2143. bytes:=bytes or ((-oper[1]^.ref^.offset) and $f0 shl 4);
  2144. bytes:=bytes or ((-oper[1]^.ref^.offset) and $f);
  2145. end
  2146. else
  2147. begin
  2148. { set U bit }
  2149. bytes:=bytes or (1 shl 23);
  2150. bytes:=bytes or (oper[1]^.ref^.offset and $f0 shl 4);
  2151. bytes:=bytes or (oper[1]^.ref^.offset and $f);
  2152. end;
  2153. end
  2154. else
  2155. begin
  2156. { set U flag }
  2157. bytes:=bytes or (1 shl 23);
  2158. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  2159. end;
  2160. { set W bit }
  2161. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  2162. bytes:=bytes or (1 shl 21);
  2163. { set P bit if necessary }
  2164. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  2165. bytes:=bytes or (1 shl 24);
  2166. end;
  2167. #$26: // LDM/STM
  2168. begin
  2169. { set instruction code }
  2170. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  2171. if oper[0]^.typ=top_ref then
  2172. begin
  2173. { set W bit }
  2174. if oper[0]^.ref^.addressmode=AM_PREINDEXED then
  2175. bytes:=bytes or (1 shl 21);
  2176. { set Rn }
  2177. bytes:=bytes or (getsupreg(oper[0]^.ref^.index) shl 16);
  2178. end
  2179. else { typ=top_reg }
  2180. begin
  2181. { set Rn }
  2182. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  2183. end;
  2184. { reglist }
  2185. bytes:=bytes or MakeRegList(oper[1]^.regset^);
  2186. { set P bit }
  2187. if (opcode=A_LDM) and (oppostfix in [PF_ED,PF_EA,PF_IB,PF_DB])
  2188. or (opcode=A_STM) and (oppostfix in [PF_FA,PF_FD,PF_IB,PF_DB]) then
  2189. bytes:=bytes or (1 shl 24);
  2190. { set U bit }
  2191. if (opcode=A_LDM) and (oppostfix in [PF_ED,PF_FD,PF_IB,PF_IA])
  2192. or (opcode=A_STM) and (oppostfix in [PF_FA,PF_EA,PF_IB,PF_IA]) then
  2193. bytes:=bytes or (1 shl 23);
  2194. end;
  2195. #$27: // SWP/SWPB
  2196. begin
  2197. { set instruction code }
  2198. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  2199. bytes:=bytes or (ord(insentry^.code[2]) shl 4);
  2200. { set regs }
  2201. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2202. bytes:=bytes or getsupreg(oper[1]^.reg);
  2203. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  2204. end;
  2205. #$03: // BX
  2206. begin
  2207. writeln(objdata.CurrObjSec.fullname);
  2208. Comment(v_warning,'BX instruction');
  2209. // TBD
  2210. end;
  2211. #$ff:
  2212. internalerror(2005091101);
  2213. else
  2214. internalerror(2005091102);
  2215. end;
  2216. { we're finished, write code }
  2217. objdata.writebytes(bytes,sizeof(bytes));
  2218. end;
  2219. {$ifdef dummy}
  2220. (*
  2221. static void gencode (long segment, long offset, int bits,
  2222. insn *ins, char *codes, long insn_end)
  2223. {
  2224. int has_S_code; /* S - setflag */
  2225. int has_B_code; /* B - setflag */
  2226. int has_T_code; /* T - setflag */
  2227. int has_W_code; /* ! => W flag */
  2228. int has_F_code; /* ^ => S flag */
  2229. int keep;
  2230. unsigned char c;
  2231. unsigned char bytes[4];
  2232. long data, size;
  2233. static int cc_code[] = /* bit pattern of cc */
  2234. { /* order as enum in */
  2235. 0x0E, 0x03, 0x02, 0x00, /* nasm.h */
  2236. 0x0A, 0x0C, 0x08, 0x0D,
  2237. 0x09, 0x0B, 0x04, 0x01,
  2238. 0x05, 0x07, 0x06,
  2239. };
  2240. #ifdef DEBUG
  2241. static char *CC[] =
  2242. { /* condition code names */
  2243. "AL", "CC", "CS", "EQ",
  2244. "GE", "GT", "HI", "LE",
  2245. "LS", "LT", "MI", "NE",
  2246. "PL", "VC", "VS", "",
  2247. "S"
  2248. };
  2249. has_S_code = (ins->condition & C_SSETFLAG);
  2250. has_B_code = (ins->condition & C_BSETFLAG);
  2251. has_T_code = (ins->condition & C_TSETFLAG);
  2252. has_W_code = (ins->condition & C_EXSETFLAG);
  2253. has_F_code = (ins->condition & C_FSETFLAG);
  2254. ins->condition = (ins->condition & 0x0F);
  2255. if (rt_debug)
  2256. {
  2257. printf ("gencode: instruction: %s%s", insn_names[ins->opcode],
  2258. CC[ins->condition & 0x0F]);
  2259. if (has_S_code)
  2260. printf ("S");
  2261. if (has_B_code)
  2262. printf ("B");
  2263. if (has_T_code)
  2264. printf ("T");
  2265. if (has_W_code)
  2266. printf ("!");
  2267. if (has_F_code)
  2268. printf ("^");
  2269. printf ("\n");
  2270. c = *codes;
  2271. printf (" (%d) decode - '0x%02X'\n", ins->operands, c);
  2272. bytes[0] = 0xB;
  2273. bytes[1] = 0xE;
  2274. bytes[2] = 0xE;
  2275. bytes[3] = 0xF;
  2276. }
  2277. // First condition code in upper nibble
  2278. if (ins->condition < C_NONE)
  2279. {
  2280. c = cc_code[ins->condition] << 4;
  2281. }
  2282. else
  2283. {
  2284. c = cc_code[C_AL] << 4; // is often ALWAYS but not always
  2285. }
  2286. switch (keep = *codes)
  2287. {
  2288. case 1:
  2289. // B, BL
  2290. ++codes;
  2291. c |= *codes++;
  2292. bytes[0] = c;
  2293. if (ins->oprs[0].segment != segment)
  2294. {
  2295. // fais une relocation
  2296. c = 1;
  2297. data = 0; // Let the linker locate ??
  2298. }
  2299. else
  2300. {
  2301. c = 0;
  2302. data = ins->oprs[0].offset - (offset + 8);
  2303. if (data % 4)
  2304. {
  2305. errfunc (ERR_NONFATAL, "offset not aligned on 4 bytes");
  2306. }
  2307. }
  2308. if (data >= 0x1000)
  2309. {
  2310. errfunc (ERR_NONFATAL, "too long offset");
  2311. }
  2312. data = data >> 2;
  2313. bytes[1] = (data >> 16) & 0xFF;
  2314. bytes[2] = (data >> 8) & 0xFF;
  2315. bytes[3] = (data ) & 0xFF;
  2316. if (c == 1)
  2317. {
  2318. // out (offset, segment, &bytes[0], OUT_RAWDATA+1, NO_SEG, NO_SEG);
  2319. out (offset, segment, &bytes[0], OUT_REL3ADR+4, ins->oprs[0].segment, NO_SEG);
  2320. }
  2321. else
  2322. {
  2323. out (offset, segment, &bytes[0], OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2324. }
  2325. return;
  2326. case 2:
  2327. // SWI
  2328. ++codes;
  2329. c |= *codes++;
  2330. bytes[0] = c;
  2331. data = ins->oprs[0].offset;
  2332. bytes[1] = (data >> 16) & 0xFF;
  2333. bytes[2] = (data >> 8) & 0xFF;
  2334. bytes[3] = (data) & 0xFF;
  2335. out (offset, segment, &bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2336. return;
  2337. case 3:
  2338. // BX
  2339. ++codes;
  2340. c |= *codes++;
  2341. bytes[0] = c;
  2342. bytes[1] = *codes++;
  2343. bytes[2] = *codes++;
  2344. bytes[3] = *codes++;
  2345. c = regval (&ins->oprs[0],1);
  2346. if (c == 15) // PC
  2347. {
  2348. errfunc (ERR_WARNING, "'BX' with R15 has undefined behaviour");
  2349. }
  2350. else if (c > 15)
  2351. {
  2352. errfunc (ERR_NONFATAL, "Illegal register specified for 'BX'");
  2353. }
  2354. bytes[3] |= (c & 0x0F);
  2355. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2356. return;
  2357. case 4: // AND Rd,Rn,Rm
  2358. case 5: // AND Rd,Rn,Rm,<shift>Rs
  2359. case 6: // AND Rd,Rn,Rm,<shift>imm
  2360. case 7: // AND Rd,Rn,<shift>imm
  2361. ++codes;
  2362. #ifdef DEBUG
  2363. if (rt_debug)
  2364. {
  2365. printf (" decode - '0x%02X'\n", keep);
  2366. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  2367. }
  2368. #endif
  2369. bytes[0] = c | *codes;
  2370. ++codes;
  2371. bytes[1] = *codes;
  2372. if (has_S_code)
  2373. bytes[1] |= 0x10;
  2374. c = regval (&ins->oprs[1],1);
  2375. // Rn in low nibble
  2376. bytes[1] |= c;
  2377. // Rd in high nibble
  2378. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2379. if (keep != 7)
  2380. {
  2381. // Rm in low nibble
  2382. bytes[3] = regval (&ins->oprs[2],1);
  2383. }
  2384. // Shifts if any
  2385. if (keep == 5 || keep == 6)
  2386. {
  2387. // Shift in bytes 2 and 3
  2388. if (keep == 5)
  2389. {
  2390. // Rs
  2391. c = regval (&ins->oprs[3],1);
  2392. bytes[2] |= c;
  2393. c = 0x10; // Set bit 4 in byte[3]
  2394. }
  2395. if (keep == 6)
  2396. {
  2397. c = (ins->oprs[3].offset) & 0x1F;
  2398. // #imm
  2399. bytes[2] |= c >> 1;
  2400. if (c & 0x01)
  2401. {
  2402. bytes[3] |= 0x80;
  2403. }
  2404. c = 0; // Clr bit 4 in byte[3]
  2405. }
  2406. // <shift>
  2407. c |= shiftval (&ins->oprs[3]) << 5;
  2408. bytes[3] |= c;
  2409. }
  2410. // reg,reg,imm
  2411. if (keep == 7)
  2412. {
  2413. int shimm;
  2414. shimm = imm_shift (ins->oprs[2].offset);
  2415. if (shimm == -1)
  2416. {
  2417. errfunc (ERR_NONFATAL, "cannot create that constant");
  2418. }
  2419. bytes[3] = shimm & 0xFF;
  2420. bytes[2] |= (shimm & 0xF00) >> 8;
  2421. }
  2422. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2423. return;
  2424. case 8: // MOV Rd,Rm
  2425. case 9: // MOV Rd,Rm,<shift>Rs
  2426. case 0xA: // MOV Rd,Rm,<shift>imm
  2427. case 0xB: // MOV Rd,<shift>imm
  2428. ++codes;
  2429. #ifdef DEBUG
  2430. if (rt_debug)
  2431. {
  2432. printf (" decode - '0x%02X'\n", keep);
  2433. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  2434. }
  2435. #endif
  2436. bytes[0] = c | *codes;
  2437. ++codes;
  2438. bytes[1] = *codes;
  2439. if (has_S_code)
  2440. bytes[1] |= 0x10;
  2441. // Rd in high nibble
  2442. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2443. if (keep != 0x0B)
  2444. {
  2445. // Rm in low nibble
  2446. bytes[3] = regval (&ins->oprs[1],1);
  2447. }
  2448. // Shifts if any
  2449. if (keep == 0x09 || keep == 0x0A)
  2450. {
  2451. // Shift in bytes 2 and 3
  2452. if (keep == 0x09)
  2453. {
  2454. // Rs
  2455. c = regval (&ins->oprs[2],1);
  2456. bytes[2] |= c;
  2457. c = 0x10; // Set bit 4 in byte[3]
  2458. }
  2459. if (keep == 0x0A)
  2460. {
  2461. c = (ins->oprs[2].offset) & 0x1F;
  2462. // #imm
  2463. bytes[2] |= c >> 1;
  2464. if (c & 0x01)
  2465. {
  2466. bytes[3] |= 0x80;
  2467. }
  2468. c = 0; // Clr bit 4 in byte[3]
  2469. }
  2470. // <shift>
  2471. c |= shiftval (&ins->oprs[2]) << 5;
  2472. bytes[3] |= c;
  2473. }
  2474. // reg,imm
  2475. if (keep == 0x0B)
  2476. {
  2477. int shimm;
  2478. shimm = imm_shift (ins->oprs[1].offset);
  2479. if (shimm == -1)
  2480. {
  2481. errfunc (ERR_NONFATAL, "cannot create that constant");
  2482. }
  2483. bytes[3] = shimm & 0xFF;
  2484. bytes[2] |= (shimm & 0xF00) >> 8;
  2485. }
  2486. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2487. return;
  2488. case 0xC: // CMP Rn,Rm
  2489. case 0xD: // CMP Rn,Rm,<shift>Rs
  2490. case 0xE: // CMP Rn,Rm,<shift>imm
  2491. case 0xF: // CMP Rn,<shift>imm
  2492. ++codes;
  2493. bytes[0] = c | *codes++;
  2494. bytes[1] = *codes;
  2495. // Implicit S code
  2496. bytes[1] |= 0x10;
  2497. c = regval (&ins->oprs[0],1);
  2498. // Rn in low nibble
  2499. bytes[1] |= c;
  2500. // No destination
  2501. bytes[2] = 0;
  2502. if (keep != 0x0B)
  2503. {
  2504. // Rm in low nibble
  2505. bytes[3] = regval (&ins->oprs[1],1);
  2506. }
  2507. // Shifts if any
  2508. if (keep == 0x0D || keep == 0x0E)
  2509. {
  2510. // Shift in bytes 2 and 3
  2511. if (keep == 0x0D)
  2512. {
  2513. // Rs
  2514. c = regval (&ins->oprs[2],1);
  2515. bytes[2] |= c;
  2516. c = 0x10; // Set bit 4 in byte[3]
  2517. }
  2518. if (keep == 0x0E)
  2519. {
  2520. c = (ins->oprs[2].offset) & 0x1F;
  2521. // #imm
  2522. bytes[2] |= c >> 1;
  2523. if (c & 0x01)
  2524. {
  2525. bytes[3] |= 0x80;
  2526. }
  2527. c = 0; // Clr bit 4 in byte[3]
  2528. }
  2529. // <shift>
  2530. c |= shiftval (&ins->oprs[2]) << 5;
  2531. bytes[3] |= c;
  2532. }
  2533. // reg,imm
  2534. if (keep == 0x0F)
  2535. {
  2536. int shimm;
  2537. shimm = imm_shift (ins->oprs[1].offset);
  2538. if (shimm == -1)
  2539. {
  2540. errfunc (ERR_NONFATAL, "cannot create that constant");
  2541. }
  2542. bytes[3] = shimm & 0xFF;
  2543. bytes[2] |= (shimm & 0xF00) >> 8;
  2544. }
  2545. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2546. return;
  2547. case 0x10: // MRS Rd,<psr>
  2548. ++codes;
  2549. bytes[0] = c | *codes++;
  2550. bytes[1] = *codes++;
  2551. // Rd
  2552. c = regval (&ins->oprs[0],1);
  2553. bytes[2] = c << 4;
  2554. bytes[3] = 0;
  2555. c = ins->oprs[1].basereg;
  2556. if (c == R_CPSR || c == R_SPSR)
  2557. {
  2558. if (c == R_SPSR)
  2559. {
  2560. bytes[1] |= 0x40;
  2561. }
  2562. }
  2563. else
  2564. {
  2565. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  2566. }
  2567. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2568. return;
  2569. case 0x11: // MSR <psr>,Rm
  2570. case 0x12: // MSR <psrf>,Rm
  2571. case 0x13: // MSR <psrf>,#expression
  2572. ++codes;
  2573. bytes[0] = c | *codes++;
  2574. bytes[1] = *codes++;
  2575. bytes[2] = *codes;
  2576. if (keep == 0x11 || keep == 0x12)
  2577. {
  2578. // Rm
  2579. c = regval (&ins->oprs[1],1);
  2580. bytes[3] = c;
  2581. }
  2582. else
  2583. {
  2584. int shimm;
  2585. shimm = imm_shift (ins->oprs[1].offset);
  2586. if (shimm == -1)
  2587. {
  2588. errfunc (ERR_NONFATAL, "cannot create that constant");
  2589. }
  2590. bytes[3] = shimm & 0xFF;
  2591. bytes[2] |= (shimm & 0xF00) >> 8;
  2592. }
  2593. c = ins->oprs[0].basereg;
  2594. if ( keep == 0x11)
  2595. {
  2596. if ( c == R_CPSR || c == R_SPSR)
  2597. {
  2598. if ( c== R_SPSR)
  2599. {
  2600. bytes[1] |= 0x40;
  2601. }
  2602. }
  2603. else
  2604. {
  2605. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  2606. }
  2607. }
  2608. else
  2609. {
  2610. if ( c == R_CPSR_FLG || c == R_SPSR_FLG)
  2611. {
  2612. if ( c== R_SPSR_FLG)
  2613. {
  2614. bytes[1] |= 0x40;
  2615. }
  2616. }
  2617. else
  2618. {
  2619. errfunc (ERR_NONFATAL, "CPSR_flg or SPSR_flg expected");
  2620. }
  2621. }
  2622. break;
  2623. case 0x14: // MUL Rd,Rm,Rs
  2624. case 0x15: // MULA Rd,Rm,Rs,Rn
  2625. ++codes;
  2626. bytes[0] = c | *codes++;
  2627. bytes[1] = *codes++;
  2628. bytes[3] = *codes;
  2629. // Rd
  2630. bytes[1] |= regval (&ins->oprs[0],1);
  2631. if (has_S_code)
  2632. bytes[1] |= 0x10;
  2633. // Rm
  2634. bytes[3] |= regval (&ins->oprs[1],1);
  2635. // Rs
  2636. bytes[2] = regval (&ins->oprs[2],1);
  2637. if (keep == 0x15)
  2638. {
  2639. bytes[2] |= regval (&ins->oprs[3],1) << 4;
  2640. }
  2641. break;
  2642. case 0x16: // SMLAL RdHi,RdLo,Rm,Rs
  2643. ++codes;
  2644. bytes[0] = c | *codes++;
  2645. bytes[1] = *codes++;
  2646. bytes[3] = *codes;
  2647. // RdHi
  2648. bytes[1] |= regval (&ins->oprs[1],1);
  2649. if (has_S_code)
  2650. bytes[1] |= 0x10;
  2651. // RdLo
  2652. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2653. // Rm
  2654. bytes[3] |= regval (&ins->oprs[2],1);
  2655. // Rs
  2656. bytes[2] |= regval (&ins->oprs[3],1);
  2657. break;
  2658. case 0x17: // LDR Rd, expression
  2659. ++codes;
  2660. bytes[0] = c | *codes++;
  2661. bytes[1] = *codes++;
  2662. // Rd
  2663. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2664. if (has_B_code)
  2665. bytes[1] |= 0x40;
  2666. if (has_T_code)
  2667. {
  2668. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  2669. }
  2670. if (has_W_code)
  2671. {
  2672. errfunc (ERR_NONFATAL, "'!' not allowed");
  2673. }
  2674. // Rn - implicit R15
  2675. bytes[1] |= 0xF;
  2676. if (ins->oprs[1].segment != segment)
  2677. {
  2678. errfunc (ERR_NONFATAL, "label not in same segment");
  2679. }
  2680. data = ins->oprs[1].offset - (offset + 8);
  2681. if (data < 0)
  2682. {
  2683. data = -data;
  2684. }
  2685. else
  2686. {
  2687. bytes[1] |= 0x80;
  2688. }
  2689. if (data >= 0x1000)
  2690. {
  2691. errfunc (ERR_NONFATAL, "too long offset");
  2692. }
  2693. bytes[2] |= ((data & 0xF00) >> 8);
  2694. bytes[3] = data & 0xFF;
  2695. break;
  2696. case 0x18: // LDR Rd, [Rn]
  2697. ++codes;
  2698. bytes[0] = c | *codes++;
  2699. bytes[1] = *codes++;
  2700. // Rd
  2701. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2702. if (has_B_code)
  2703. bytes[1] |= 0x40;
  2704. if (has_T_code)
  2705. {
  2706. bytes[1] |= 0x20; // write-back
  2707. }
  2708. else
  2709. {
  2710. bytes[0] |= 0x01; // implicit pre-index mode
  2711. }
  2712. if (has_W_code)
  2713. {
  2714. bytes[1] |= 0x20; // write-back
  2715. }
  2716. // Rn
  2717. c = regval (&ins->oprs[1],1);
  2718. bytes[1] |= c;
  2719. if (c == 0x15) // R15
  2720. data = -8;
  2721. else
  2722. data = 0;
  2723. if (data < 0)
  2724. {
  2725. data = -data;
  2726. }
  2727. else
  2728. {
  2729. bytes[1] |= 0x80;
  2730. }
  2731. bytes[2] |= ((data & 0xF00) >> 8);
  2732. bytes[3] = data & 0xFF;
  2733. break;
  2734. case 0x19: // LDR Rd, [Rn,#expression]
  2735. case 0x20: // LDR Rd, [Rn,Rm]
  2736. case 0x21: // LDR Rd, [Rn,Rm,shift]
  2737. ++codes;
  2738. bytes[0] = c | *codes++;
  2739. bytes[1] = *codes++;
  2740. // Rd
  2741. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2742. if (has_B_code)
  2743. bytes[1] |= 0x40;
  2744. // Rn
  2745. c = regval (&ins->oprs[1],1);
  2746. bytes[1] |= c;
  2747. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  2748. {
  2749. bytes[0] |= 0x01; // pre-index mode
  2750. if (has_W_code)
  2751. {
  2752. bytes[1] |= 0x20;
  2753. }
  2754. if (has_T_code)
  2755. {
  2756. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  2757. }
  2758. }
  2759. else
  2760. {
  2761. if (has_T_code) // Forced write-back in post-index mode
  2762. {
  2763. bytes[1] |= 0x20;
  2764. }
  2765. if (has_W_code)
  2766. {
  2767. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  2768. }
  2769. }
  2770. if (keep == 0x19)
  2771. {
  2772. data = ins->oprs[2].offset;
  2773. if (data < 0)
  2774. {
  2775. data = -data;
  2776. }
  2777. else
  2778. {
  2779. bytes[1] |= 0x80;
  2780. }
  2781. if (data >= 0x1000)
  2782. {
  2783. errfunc (ERR_NONFATAL, "too long offset");
  2784. }
  2785. bytes[2] |= ((data & 0xF00) >> 8);
  2786. bytes[3] = data & 0xFF;
  2787. }
  2788. else
  2789. {
  2790. if (ins->oprs[2].minus == 0)
  2791. {
  2792. bytes[1] |= 0x80;
  2793. }
  2794. c = regval (&ins->oprs[2],1);
  2795. bytes[3] = c;
  2796. if (keep == 0x21)
  2797. {
  2798. c = ins->oprs[3].offset;
  2799. if (c > 0x1F)
  2800. {
  2801. errfunc (ERR_NONFATAL, "too large shiftvalue");
  2802. c = c & 0x1F;
  2803. }
  2804. bytes[2] |= c >> 1;
  2805. if (c & 0x01)
  2806. {
  2807. bytes[3] |= 0x80;
  2808. }
  2809. bytes[3] |= shiftval (&ins->oprs[3]) << 5;
  2810. }
  2811. }
  2812. break;
  2813. case 0x22: // LDRH Rd, expression
  2814. ++codes;
  2815. bytes[0] = c | 0x01; // Implicit pre-index
  2816. bytes[1] = *codes++;
  2817. // Rd
  2818. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2819. // Rn - implicit R15
  2820. bytes[1] |= 0xF;
  2821. if (ins->oprs[1].segment != segment)
  2822. {
  2823. errfunc (ERR_NONFATAL, "label not in same segment");
  2824. }
  2825. data = ins->oprs[1].offset - (offset + 8);
  2826. if (data < 0)
  2827. {
  2828. data = -data;
  2829. }
  2830. else
  2831. {
  2832. bytes[1] |= 0x80;
  2833. }
  2834. if (data >= 0x100)
  2835. {
  2836. errfunc (ERR_NONFATAL, "too long offset");
  2837. }
  2838. bytes[3] = *codes++;
  2839. bytes[2] |= ((data & 0xF0) >> 4);
  2840. bytes[3] |= data & 0xF;
  2841. break;
  2842. case 0x23: // LDRH Rd, Rn
  2843. ++codes;
  2844. bytes[0] = c | 0x01; // Implicit pre-index
  2845. bytes[1] = *codes++;
  2846. // Rd
  2847. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2848. // Rn
  2849. c = regval (&ins->oprs[1],1);
  2850. bytes[1] |= c;
  2851. if (c == 0x15) // R15
  2852. data = -8;
  2853. else
  2854. data = 0;
  2855. if (data < 0)
  2856. {
  2857. data = -data;
  2858. }
  2859. else
  2860. {
  2861. bytes[1] |= 0x80;
  2862. }
  2863. if (data >= 0x100)
  2864. {
  2865. errfunc (ERR_NONFATAL, "too long offset");
  2866. }
  2867. bytes[3] = *codes++;
  2868. bytes[2] |= ((data & 0xF0) >> 4);
  2869. bytes[3] |= data & 0xF;
  2870. break;
  2871. case 0x24: // LDRH Rd, Rn, expression
  2872. case 0x25: // LDRH Rd, Rn, Rm
  2873. ++codes;
  2874. bytes[0] = c;
  2875. bytes[1] = *codes++;
  2876. // Rd
  2877. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2878. // Rn
  2879. c = regval (&ins->oprs[1],1);
  2880. bytes[1] |= c;
  2881. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  2882. {
  2883. bytes[0] |= 0x01; // pre-index mode
  2884. if (has_W_code)
  2885. {
  2886. bytes[1] |= 0x20;
  2887. }
  2888. }
  2889. else
  2890. {
  2891. if (has_W_code)
  2892. {
  2893. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  2894. }
  2895. }
  2896. bytes[3] = *codes++;
  2897. if (keep == 0x24)
  2898. {
  2899. data = ins->oprs[2].offset;
  2900. if (data < 0)
  2901. {
  2902. data = -data;
  2903. }
  2904. else
  2905. {
  2906. bytes[1] |= 0x80;
  2907. }
  2908. if (data >= 0x100)
  2909. {
  2910. errfunc (ERR_NONFATAL, "too long offset");
  2911. }
  2912. bytes[2] |= ((data & 0xF0) >> 4);
  2913. bytes[3] |= data & 0xF;
  2914. }
  2915. else
  2916. {
  2917. if (ins->oprs[2].minus == 0)
  2918. {
  2919. bytes[1] |= 0x80;
  2920. }
  2921. c = regval (&ins->oprs[2],1);
  2922. bytes[3] |= c;
  2923. }
  2924. break;
  2925. case 0x26: // LDM/STM Rn, {reg-list}
  2926. ++codes;
  2927. bytes[0] = c;
  2928. bytes[0] |= ( *codes >> 4) & 0xF;
  2929. bytes[1] = ( *codes << 4) & 0xF0;
  2930. ++codes;
  2931. if (has_W_code)
  2932. {
  2933. bytes[1] |= 0x20;
  2934. }
  2935. if (has_F_code)
  2936. {
  2937. bytes[1] |= 0x40;
  2938. }
  2939. // Rn
  2940. bytes[1] |= regval (&ins->oprs[0],1);
  2941. data = ins->oprs[1].basereg;
  2942. bytes[2] = ((data >> 8) & 0xFF);
  2943. bytes[3] = (data & 0xFF);
  2944. break;
  2945. case 0x27: // SWP Rd, Rm, [Rn]
  2946. ++codes;
  2947. bytes[0] = c;
  2948. bytes[0] |= *codes++;
  2949. bytes[1] = regval (&ins->oprs[2],1);
  2950. if (has_B_code)
  2951. {
  2952. bytes[1] |= 0x40;
  2953. }
  2954. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2955. bytes[3] = *codes++;
  2956. bytes[3] |= regval (&ins->oprs[1],1);
  2957. break;
  2958. default:
  2959. errfunc (ERR_FATAL, "unknown decoding of instruction");
  2960. bytes[0] = c;
  2961. // And a fix nibble
  2962. ++codes;
  2963. bytes[0] |= *codes++;
  2964. if ( *codes == 0x01) // An I bit
  2965. {
  2966. }
  2967. if ( *codes == 0x02) // An I bit
  2968. {
  2969. }
  2970. ++codes;
  2971. }
  2972. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2973. }
  2974. *)
  2975. {$endif dummy}
  2976. constructor tai_thumb_func.create;
  2977. begin
  2978. inherited create;
  2979. typ:=ait_thumb_func;
  2980. end;
  2981. begin
  2982. cai_align:=tai_align;
  2983. end.