aasmcpu.pas 95 KB

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