ra386att.pas 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134
  1. {
  2. $Id$
  3. Copyright (c) 1997-98 by Carl Eric Codere
  4. Does the parsing for the AT&T styled inline assembler.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit Ra386att;
  19. {$ifdef TP}
  20. {$N+,E+}
  21. {$endif TP}
  22. {**********************************************************************}
  23. { WARNING }
  24. {**********************************************************************}
  25. { Any modification in the order or removal of terms in the tables }
  26. { in i386.pas and intasmi3.pas will BREAK the code in this unit, }
  27. { unless the appropriate changes are made to this unit. Addition }
  28. { of terms though, will not change the code herein. }
  29. {**********************************************************************}
  30. {--------------------------------------------------------------------}
  31. { LEFT TO DO: }
  32. {--------------------------------------------------------------------}
  33. { o Handle record offsets }
  34. { o Add support imul,shld and shrd. }
  35. { o Add support for nor operators. }
  36. { o Bugfix of ao_imm8s for IMUL. (Currently the 3 operand imul will }
  37. { be considered as invalid because I use ao_imm8 and the table }
  38. { uses ao_imm8s). }
  39. { o In ConcatOpCode add more checking regarding suffixes and }
  40. { destination registers. (started but unfinished). }
  41. {--------------------------------------------------------------------}
  42. Interface
  43. uses
  44. globtype,i386,tree;
  45. function assemble: ptree;
  46. const
  47. { this variable is TRUE if the lookup tables have already been setup }
  48. { for fast access. On the first call to assemble the tables are setup }
  49. { and stay set up. }
  50. _asmsorted: boolean = FALSE;
  51. firstreg = R_EAX;
  52. lastreg = R_ST7;
  53. type
  54. tiasmops = array[firstop..lastop] of string[7];
  55. piasmops = ^tiasmops;
  56. var
  57. previous_was_id : boolean;
  58. { sorted tables of opcodes }
  59. iasmops: piasmops;
  60. { uppercased tables of registers }
  61. iasmregs: array[firstreg..lastreg] of string[6];
  62. Implementation
  63. Uses
  64. strings,cobjects,systems,verbose,globals,
  65. files,aasm,types,symtable,scanner,hcodegen,
  66. rautils;
  67. type
  68. tinteltoken = (
  69. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_HEXNUM,AS_OCTALNUM,
  70. AS_BINNUM,AS_REALNUM,AS_COMMA,AS_LPAREN,
  71. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,AS_INTNUM,
  72. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,AS_DOLLAR,
  73. {------------------ Assembler directives --------------------}
  74. AS_DB,AS_DW,AS_DD,AS_DQ,AS_GLOBAL,AS_ALIGN,AS_ASCII,
  75. AS_ASCIIZ,AS_LCOMM,AS_COMM,AS_SINGLE,AS_DOUBLE,AS_EXTENDED,
  76. AS_DATA,AS_TEXT,AS_END,
  77. {------------------ Assembler Operators --------------------}
  78. AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR,AS_NOR);
  79. tasmkeyword = string[8];
  80. const
  81. { These tokens should be modified accordingly to the modifications }
  82. { in the different enumerations. }
  83. firstdirective = AS_DB;
  84. lastdirective = AS_END;
  85. firstsreg = R_CS;
  86. lastsreg = R_SS;
  87. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  88. _count_asmprefixes = 5;
  89. _count_asmspecialops = 25;
  90. _count_asmoverrides = 3;
  91. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  92. ('.byte','.word','.long','.quad','.globl','.align','.ascii',
  93. '.asciz','.lcomm','.comm','.single','.double','.tfloat',
  94. '.data','.text','END');
  95. {------------------ Missing opcodes from std list ----------------}
  96. _asmprefixes: array[0.._count_asmprefixes] of tasmkeyword = (
  97. 'REPNE','REPE','REP','REPZ','REPNZ','LOCK');
  98. _prefixtokens: array[0.._count_asmprefixes] of tasmop = (
  99. A_REPNE,A_REPE,A_REP,A_REPE,A_REPNE,A_LOCK);
  100. _specialops: array[0.._count_asmspecialops] of tasmkeyword = (
  101. 'CMPSB','CMPSW','CMPSL','INSB','INSW','INSL','OUTSB','OUTSW','OUTSL',
  102. 'SCASB','SCASW','SCASL','STOSB','STOSW','STOSL','MOVSB','MOVSW','MOVSL',
  103. 'LODSB','LODSW','LODSL','LOCK','SEGCS','SEGDS','SEGES','SEGSS');
  104. _specialopstokens: array[0.._count_asmspecialops] of tasmop = (
  105. A_CMPS,A_CMPS,A_CMPS,A_INS,A_INS,A_INS,A_OUTS,A_OUTS,A_OUTS,
  106. A_SCAS,A_SCAS,A_SCAS,A_STOS,A_STOS,A_STOS,A_MOVS,A_MOVS,A_MOVS,
  107. A_LODS,A_LODS,A_LODS,A_LOCK,A_NONE,A_NONE,A_NONE,A_NONE);
  108. {------------------------------------------------------------------}
  109. { converts from AT&T style to non-specific style... }
  110. _fpusizes:array[A_FILDQ..A_FIDIVRS] of topsize = (
  111. {'fildq','filds',}
  112. S_IQ,S_IS,
  113. {'fildl','fldl','fldt','fistq','fists','fistl','fstl','fsts',}
  114. S_IL,S_FL,S_FX,S_IQ,S_IS,S_IL,S_FL,S_FS,
  115. {'fstps','fistpl','fstpl','fistps','fistpq','fstpt','fcomps',}
  116. S_FS,S_IL,S_FL,S_IS,S_IQ,S_FX,S_FS,
  117. {'ficompl','fcompl','ficomps','fcoms','ficoml','fcoml','ficoms',}
  118. S_IL,S_FL,S_IS,S_FS,S_IL,S_FL,S_IS,
  119. {'fiaddl','faddl','fiadds','fisubl','fsubl','fisubs','fsubs',}
  120. S_IL,S_FL,S_IS,S_IL,S_FL,S_FS,S_IS,S_FS,
  121. {'fsubr','fsubrs','fisubrl','fsubrl','fisubrs','fmuls','fimull',}
  122. S_NO,S_FS,S_IL,S_FL,S_IS,S_FS,S_IL,
  123. {'fmull','fimuls','fdivs','fidivl','fdivl','fidivs','fdivrs',}
  124. S_FL,S_IL,S_FS,S_IL,S_FL,S_IS,S_FS,
  125. {'fidivrl','fdivrl',}
  126. S_IL,S_FL);
  127. _fpuopcodes:array[A_FILDQ..A_FIDIVRS] of tasmop = (
  128. A_FILD,A_FILD,A_FILD,A_FLD,A_FLD,A_FIST,A_FIST,A_FIST,A_FST,A_FST,
  129. A_FSTP,A_FISTP,A_FSTP,A_FISTP,A_FISTP,A_FSTP,
  130. A_FCOMP,A_FICOMP,A_FCOMP,A_FICOMP,
  131. A_FCOM,A_FICOM,A_FCOM,A_FICOM,A_FIADD,A_FADD,A_FIADD,
  132. A_FISUB,A_FSUB,A_FISUB,A_FSUB,A_FSUB,A_FSUBR,A_FISUBR,
  133. A_FSUBR,A_FISUBR,A_FMUL,A_FIMUL,A_FMUL,A_FIMUL,A_FDIV,A_FIDIV,
  134. A_FDIV,A_FIDIV,A_FDIVR,A_FIDIVR,A_FDIVR,A_FIDIVR);
  135. const
  136. newline = #10;
  137. firsttoken : boolean = TRUE;
  138. operandnum : byte = 0;
  139. charcount: byte = 0;
  140. var
  141. inexpression : boolean;
  142. p : paasmoutput;
  143. actasmtoken : tinteltoken;
  144. actasmpattern : string;
  145. c : char;
  146. Instr : TInstruction;
  147. labellist : TAsmLabelList;
  148. Procedure SetupTables;
  149. { creates uppercased symbol tables. }
  150. var
  151. i: tasmop;
  152. j: tregister;
  153. Begin
  154. Message(assem_d_creating_lookup_tables);
  155. { opcodes }
  156. new(iasmops);
  157. for i:=firstop to lastop do
  158. iasmops^[i] := upper(att_op2str[i]);
  159. { opcodes }
  160. for j:=firstreg to lastreg do
  161. iasmregs[j] := upper(att_reg2str[j]);
  162. end;
  163. {---------------------------------------------------------------------}
  164. { Routines for the tokenizing }
  165. {---------------------------------------------------------------------}
  166. function is_asmopcode(const s: string):Boolean;
  167. {*********************************************************************}
  168. { FUNCTION is_asmopcode(s: string):Boolean }
  169. { Description: Determines if the s string is a valid opcode }
  170. { if so returns TRUE otherwise returns FALSE. }
  171. {*********************************************************************}
  172. var
  173. i: tasmop;
  174. j: byte;
  175. hs: topsize;
  176. hid: string;
  177. Begin
  178. is_asmopcode := FALSE;
  179. { first search for extended opcodes }
  180. for j:=0 to _count_asmspecialops do
  181. Begin
  182. if s = _specialops[j] then
  183. Begin
  184. is_asmopcode:=TRUE;
  185. exit;
  186. end;
  187. end;
  188. for i:=firstop to lastop do
  189. Begin
  190. if s=iasmops^[i] then
  191. begin
  192. is_asmopcode := TRUE;
  193. exit
  194. end;
  195. end;
  196. { not found yet ... }
  197. { search for all possible suffixes }
  198. for hs:=S_WL downto S_B do
  199. if copy(s,length(s)-length(att_opsize2str[hs])+1,
  200. length(att_opsize2str[hs]))=upper(att_opsize2str[hs]) then
  201. begin
  202. { here we search the entire table... }
  203. hid:=copy(s,1,length(s)-length(att_opsize2str[hs]));
  204. for i:=firstop to lastop do
  205. if (length(hid) > 0) and (hid=iasmops^[i]) then
  206. begin
  207. is_asmopcode := TRUE;
  208. exit;
  209. end;
  210. end;
  211. end;
  212. Procedure is_asmdirective(const s: string; var token: tinteltoken);
  213. {*********************************************************************}
  214. { FUNCTION is_asmdirective(s: string; var token: tinteltoken):Boolean }
  215. { Description: Determines if the s string is a valid directive }
  216. { (an operator can occur in operand fields, while a directive cannot) }
  217. { if so returns the directive token, otherwise does not change token.}
  218. {*********************************************************************}
  219. var
  220. i:byte;
  221. Begin
  222. for i:=0 to _count_asmdirectives do
  223. begin
  224. if s=_asmdirectives[i] then
  225. begin
  226. token := tinteltoken(longint(firstdirective)+i);
  227. exit;
  228. end;
  229. end;
  230. end;
  231. Procedure is_register(const s: string; var token: tinteltoken);
  232. {*********************************************************************}
  233. { PROCEDURE is_register(s: string; var token: tinteltoken); }
  234. { Description: Determines if the s string is a valid register, if }
  235. { so return token equal to A_REGISTER, otherwise does not change token}
  236. {*********************************************************************}
  237. Var
  238. i: tregister;
  239. Begin
  240. for i:=firstreg to lastreg do
  241. begin
  242. if s=iasmregs[i] then
  243. begin
  244. token := AS_REGISTER;
  245. exit;
  246. end;
  247. end;
  248. end;
  249. Function GetToken: tinteltoken;
  250. {*********************************************************************}
  251. { FUNCTION GetToken: tinteltoken; }
  252. { Description: This routine returns intel assembler tokens and }
  253. { does some minor syntax error checking. }
  254. {*********************************************************************}
  255. var
  256. token: tinteltoken;
  257. forcelabel: boolean;
  258. errorflag : boolean;
  259. temp: string;
  260. code: integer;
  261. value: byte;
  262. begin
  263. errorflag := FALSE;
  264. forcelabel := FALSE;
  265. actasmpattern :='';
  266. {* INIT TOKEN TO NOTHING *}
  267. token := AS_NONE;
  268. { while space and tab , continue scan... }
  269. while c in [' ',#9] do
  270. c:=current_scanner^.asmgetchar;
  271. { Possiblities for first token in a statement: }
  272. { Local Label, Label, Directive, Prefix or Opcode.... }
  273. if not (c in [newline,#13,'{',';']) then
  274. current_scanner^.gettokenpos;
  275. if firsttoken and not (c in [newline,#13,'{',';']) then
  276. begin
  277. firsttoken := FALSE;
  278. { directive or local labe }
  279. if c = '.' then
  280. begin
  281. actasmpattern := c;
  282. { Let us point to the next character }
  283. c := current_scanner^.asmgetchar;
  284. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  285. begin
  286. actasmpattern := actasmpattern + c;
  287. c := current_scanner^.asmgetchar;
  288. end;
  289. { this is a local label... }
  290. if (actasmpattern[2] = 'L') and (c = ':') then
  291. Begin
  292. { local variables are case sensitive }
  293. gettoken := AS_LLABEL;
  294. { delete .L }
  295. delete(actasmpattern,1,2);
  296. { point to next character ... }
  297. c := current_scanner^.asmgetchar;
  298. exit;
  299. end
  300. { must be a directive }
  301. else
  302. Begin
  303. { directives are case sensitive!! }
  304. is_asmdirective(actasmpattern, token);
  305. if (token <> AS_NONE) then
  306. Begin
  307. gettoken := token;
  308. exit;
  309. end
  310. else
  311. Message1(assem_e_not_directive_or_local_symbol,actasmpattern);
  312. end;
  313. end; { endif }
  314. { only opcodes and global labels are allowed now. }
  315. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  316. begin
  317. actasmpattern := actasmpattern + c;
  318. c := current_scanner^.asmgetchar;
  319. end;
  320. if c = ':' then
  321. begin
  322. { uppervar(actasmpattern);
  323. Carl, you cannot change the label to upper
  324. if you want to be able to read in system unit
  325. don't forget that ATT syntax is case sensitive
  326. for labels !! (PM) }
  327. token := AS_LABEL;
  328. { let us point to the next character }
  329. c := current_scanner^.asmgetchar;
  330. gettoken := token;
  331. exit;
  332. end;
  333. If is_asmopcode(upper(actasmpattern)) then
  334. Begin
  335. uppervar(actasmpattern);
  336. gettoken := AS_OPCODE;
  337. exit;
  338. end
  339. else
  340. if upper(actasmpattern) = 'END' then
  341. begin
  342. gettoken := AS_END;
  343. exit;
  344. end
  345. else
  346. begin
  347. gettoken := AS_NONE;
  348. Message(assem_e_invalid_operand);
  349. end;
  350. end
  351. else { else firsttoken }
  352. { Here we must handle all possible cases }
  353. begin
  354. case c of
  355. '.': { possiblities : - local label reference , such as in jmp @local1 }
  356. { - field of object/record }
  357. { - directive. }
  358. begin
  359. if previous_was_id then
  360. begin
  361. c:=current_scanner^.asmgetchar;
  362. gettoken:=AS_DOT;
  363. exit;
  364. end;
  365. actasmpattern := c;
  366. c:= current_scanner^.asmgetchar;
  367. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  368. begin
  369. actasmpattern := actasmpattern + c;
  370. c := current_scanner^.asmgetchar;
  371. end;
  372. is_asmdirective(actasmpattern,token);
  373. { if directive }
  374. if (token <> AS_NONE) then
  375. begin
  376. gettoken := token;
  377. exit;
  378. end;
  379. { local label references and directives }
  380. { are case sensitive }
  381. gettoken := AS_ID;
  382. exit;
  383. end;
  384. { identifier, register, opcode, prefix or directive }
  385. '_','A'..'Z','a'..'z': begin
  386. actasmpattern := c;
  387. c:= current_scanner^.asmgetchar;
  388. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  389. begin
  390. actasmpattern := actasmpattern + c;
  391. c := current_scanner^.asmgetchar;
  392. end;
  393. { pascal is not case sensitive! }
  394. { therefore variables which are }
  395. { outside the scope of the asm }
  396. { block, should not be made case }
  397. { sensitive... !!!!! }
  398. uppervar(actasmpattern);
  399. If is_asmopcode(actasmpattern) then
  400. Begin
  401. gettoken := AS_OPCODE;
  402. exit;
  403. end;
  404. { we handle this directive separately from }
  405. { others. }
  406. if actasmpattern = 'END' then
  407. Begin
  408. gettoken := AS_END;
  409. exit;
  410. end;
  411. { if found }
  412. if (token <> AS_NONE) then
  413. begin
  414. gettoken := token;
  415. exit;
  416. end
  417. { this is surely an identifier }
  418. else
  419. token := AS_ID;
  420. gettoken := token;
  421. exit;
  422. end;
  423. '&': begin
  424. c:=current_scanner^.asmgetchar;
  425. gettoken := AS_AND;
  426. end;
  427. { character }
  428. '''' : begin
  429. c:=current_scanner^.asmgetchar;
  430. if c = '\' then
  431. Begin
  432. { escape sequence }
  433. c:=current_scanner^.asmgetchar;
  434. case c of
  435. newline: Message(scan_f_string_exceeds_line);
  436. 't': actasmpattern:=#09;
  437. 'b': actasmpattern:=#08;
  438. '\': actasmpattern:='\';
  439. 'f': actasmpattern:=#12;
  440. 'n': actasmpattern:=#10;
  441. 'r': actasmpattern:=#13;
  442. '"': actasmpattern:='"';
  443. { octal number }
  444. '0'..'7':
  445. begin
  446. temp:=c;
  447. temp:=temp+current_scanner^.asmgetchar;
  448. temp:=temp+current_scanner^.asmgetchar;
  449. val(octaltodec(temp),value,code);
  450. if (code <> 0) then
  451. Message1(assem_e_error_in_octal_const,temp);
  452. actasmpattern:=chr(value);
  453. end;
  454. { hexadecimal number }
  455. 'x':
  456. begin
  457. temp:=current_scanner^.asmgetchar;
  458. temp:=temp+current_scanner^.asmgetchar;
  459. val(hextodec(temp),value,code);
  460. if (code <> 0) then
  461. Message1(assem_e_error_in_hex_const,temp);
  462. actasmpattern:=chr(value);
  463. end;
  464. else
  465. Begin
  466. Message(assem_e_escape_seq_ignored);
  467. actasmpattern:=c;
  468. end
  469. end; { end case }
  470. end
  471. else
  472. actasmpattern:=c;
  473. gettoken := AS_STRING;
  474. c:=current_scanner^.asmgetchar;
  475. exit;
  476. end;
  477. { string }
  478. '"' :
  479. begin
  480. actasmpattern:='';
  481. while true do
  482. Begin
  483. c:=current_scanner^.asmgetchar;
  484. case c of
  485. '\': Begin
  486. { escape sequences }
  487. c:=current_scanner^.asmgetchar;
  488. case c of
  489. newline: Message(scan_f_string_exceeds_line);
  490. 't': actasmpattern:=actasmpattern+#09;
  491. 'b': actasmpattern:=actasmpattern+#08;
  492. '\': actasmpattern:=actasmpattern+'\';
  493. 'f': actasmpattern:=actasmpattern+#12;
  494. 'n': actasmpattern:=actasmpattern+#10;
  495. 'r': actasmpattern:=actasmpattern+#13;
  496. '"': actasmpattern:=actasmpattern+'"';
  497. { octal number }
  498. '0'..'7':
  499. begin
  500. temp:=c;
  501. temp:=temp+current_scanner^.asmgetchar;
  502. temp:=temp+current_scanner^.asmgetchar;
  503. val(octaltodec(temp),value,code);
  504. if (code <> 0) then
  505. Message1(assem_e_error_in_octal_const,temp);
  506. actasmpattern:=actasmpattern+chr(value);
  507. end;
  508. { hexadecimal number }
  509. 'x':
  510. begin
  511. temp:=current_scanner^.asmgetchar;
  512. temp:=temp+current_scanner^.asmgetchar;
  513. val(hextodec(temp),value,code);
  514. if (code <> 0) then
  515. Message1(assem_e_error_in_hex_const,temp);
  516. actasmpattern:=actasmpattern+chr(value);
  517. end;
  518. else
  519. Begin
  520. Message(assem_e_escape_seq_ignored);
  521. actasmpattern:=actasmpattern+c;
  522. end
  523. end; { end case }
  524. end;
  525. '"': begin
  526. c:=current_scanner^.asmgetchar;
  527. break;
  528. end;
  529. newline: Message(scan_f_string_exceeds_line);
  530. else
  531. actasmpattern:=actasmpattern+c;
  532. end;
  533. end; { end case }
  534. token := AS_STRING;
  535. gettoken := token;
  536. exit;
  537. end;
  538. '$' : begin
  539. gettoken := AS_DOLLAR;
  540. c:=current_scanner^.asmgetchar;
  541. exit;
  542. end;
  543. ',' : begin
  544. gettoken := AS_COMMA;
  545. c:=current_scanner^.asmgetchar;
  546. exit;
  547. end;
  548. '<' : begin
  549. gettoken := AS_SHL;
  550. c := current_scanner^.asmgetchar;
  551. if c = '<' then
  552. c := current_scanner^.asmgetchar;
  553. exit;
  554. end;
  555. '>' : begin
  556. gettoken := AS_SHL;
  557. c := current_scanner^.asmgetchar;
  558. if c = '>' then
  559. c := current_scanner^.asmgetchar;
  560. exit;
  561. end;
  562. '|' : begin
  563. gettoken := AS_OR;
  564. c := current_scanner^.asmgetchar;
  565. exit;
  566. end;
  567. '^' : begin
  568. gettoken := AS_XOR;
  569. c := current_scanner^.asmgetchar;
  570. exit;
  571. end;
  572. '!' : begin
  573. Message(assem_e_nor_not_supported);
  574. c := current_scanner^.asmgetchar;
  575. gettoken := AS_NONE;
  576. exit;
  577. end;
  578. '(' : begin
  579. gettoken := AS_LPAREN;
  580. c:=current_scanner^.asmgetchar;
  581. exit;
  582. end;
  583. ')' : begin
  584. gettoken := AS_RPAREN;
  585. c:=current_scanner^.asmgetchar;
  586. exit;
  587. end;
  588. ':' : begin
  589. gettoken := AS_COLON;
  590. c:=current_scanner^.asmgetchar;
  591. exit;
  592. end;
  593. '+' : begin
  594. gettoken := AS_PLUS;
  595. c:=current_scanner^.asmgetchar;
  596. exit;
  597. end;
  598. '-' : begin
  599. gettoken := AS_MINUS;
  600. c:=current_scanner^.asmgetchar;
  601. exit;
  602. end;
  603. '*' : begin
  604. gettoken := AS_STAR;
  605. c:=current_scanner^.asmgetchar;
  606. exit;
  607. end;
  608. '/' : begin
  609. c:=current_scanner^.asmgetchar;
  610. { att styled comment }
  611. if c='/' then
  612. begin
  613. repeat
  614. c:=current_scanner^.asmgetchar;
  615. until c=newline;
  616. firsttoken := TRUE;
  617. gettoken:=AS_SEPARATOR;
  618. c:=current_scanner^.asmgetchar;
  619. exit;
  620. end
  621. else
  622. begin
  623. gettoken := AS_SLASH;
  624. c:=current_scanner^.asmgetchar;
  625. exit;
  626. end;
  627. end;
  628. { register or modulo }
  629. { only register supported }
  630. { for the moment. }
  631. '%' : begin
  632. actasmpattern := c;
  633. c:=current_scanner^.asmgetchar;
  634. while c in ['a'..'z','A'..'Z','0'..'9'] do
  635. Begin
  636. actasmpattern := actasmpattern + c;
  637. c:=current_scanner^.asmgetchar;
  638. end;
  639. token := AS_NONE;
  640. uppervar(actasmpattern);
  641. if (actasmpattern = '%ST') and (c='(') then
  642. Begin
  643. actasmpattern:=actasmpattern+c;
  644. c:=current_scanner^.asmgetchar;
  645. if c in ['0'..'9'] then
  646. actasmpattern := actasmpattern + c
  647. else
  648. Message(assem_e_invalid_fpu_register);
  649. c:=current_scanner^.asmgetchar;
  650. if c <> ')' then
  651. Message(assem_e_invalid_fpu_register)
  652. else
  653. Begin
  654. actasmpattern := actasmpattern + c;
  655. c:=current_scanner^.asmgetchar; { let us point to next character. }
  656. end;
  657. end;
  658. is_register(actasmpattern, token);
  659. { if found }
  660. if (token <> AS_NONE) then
  661. begin
  662. gettoken := token;
  663. exit;
  664. end
  665. else
  666. begin
  667. Message(assem_w_modulo_not_supported);
  668. gettoken := AS_NONE;
  669. end;
  670. end;
  671. { integer number }
  672. '1'..'9': begin
  673. actasmpattern := c;
  674. c := current_scanner^.asmgetchar;
  675. while c in ['0'..'9'] do
  676. Begin
  677. actasmpattern := actasmpattern + c;
  678. c:= current_scanner^.asmgetchar;
  679. end;
  680. gettoken := AS_INTNUM;
  681. exit;
  682. end;
  683. '0': begin
  684. { octal,hexa,real or binary number. }
  685. actasmpattern := c;
  686. c:=current_scanner^.asmgetchar;
  687. case upcase(c) of
  688. { binary }
  689. 'B': Begin
  690. c:=current_scanner^.asmgetchar;
  691. while c in ['0','1'] do
  692. Begin
  693. actasmpattern := actasmpattern + c;
  694. c := current_scanner^.asmgetchar;
  695. end;
  696. gettoken := AS_BINNUM;
  697. exit;
  698. end;
  699. { real }
  700. 'D': Begin
  701. c:=current_scanner^.asmgetchar;
  702. { get ridd of the 0d }
  703. if (c='+') or (c='-') then
  704. begin
  705. actasmpattern:=c;
  706. c:=current_scanner^.asmgetchar;
  707. end
  708. else
  709. actasmpattern:='';
  710. while c in ['0'..'9'] do
  711. Begin
  712. actasmpattern := actasmpattern + c;
  713. c:= current_scanner^.asmgetchar;
  714. end;
  715. if c='.' then
  716. begin
  717. actasmpattern := actasmpattern + c;
  718. c:=current_scanner^.asmgetchar;
  719. while c in ['0'..'9'] do
  720. Begin
  721. actasmpattern := actasmpattern + c;
  722. c:= current_scanner^.asmgetchar;
  723. end;
  724. if upcase(c) = 'E' then
  725. begin
  726. actasmpattern := actasmpattern + c;
  727. c:=current_scanner^.asmgetchar;
  728. if (c = '+') or (c = '-') then
  729. begin
  730. actasmpattern := actasmpattern + c;
  731. c:=current_scanner^.asmgetchar;
  732. end;
  733. while c in ['0'..'9'] do
  734. Begin
  735. actasmpattern := actasmpattern + c;
  736. c:= current_scanner^.asmgetchar;
  737. end;
  738. end;
  739. gettoken := AS_REALNUM;
  740. exit;
  741. end
  742. else
  743. begin
  744. Message1(assem_e_invalid_float_const,actasmpattern+c);
  745. gettoken:=AS_NONE;
  746. end;
  747. end;
  748. { hexadecimal }
  749. 'X': Begin
  750. c:=current_scanner^.asmgetchar;
  751. while c in ['0'..'9','a'..'f','A'..'F'] do
  752. Begin
  753. actasmpattern := actasmpattern + c;
  754. c := current_scanner^.asmgetchar;
  755. end;
  756. gettoken := AS_HEXNUM;
  757. exit;
  758. end;
  759. { octal }
  760. '1'..'7': begin
  761. actasmpattern := actasmpattern + c;
  762. while c in ['0'..'7'] do
  763. Begin
  764. actasmpattern := actasmpattern + c;
  765. c := current_scanner^.asmgetchar;
  766. end;
  767. gettoken := AS_OCTALNUM;
  768. exit;
  769. end;
  770. else { octal number zero value...}
  771. Begin
  772. gettoken := AS_OCTALNUM;
  773. exit;
  774. end;
  775. end; { end case }
  776. end;
  777. '{',#13,newline,';' : begin
  778. { the comment is read by asmgetchar }
  779. c:=current_scanner^.asmgetchar;
  780. firsttoken := TRUE;
  781. gettoken:=AS_SEPARATOR;
  782. end;
  783. else
  784. Begin
  785. Message(scan_f_illegal_char);
  786. end;
  787. end; { end case }
  788. end; { end else if }
  789. end;
  790. {---------------------------------------------------------------------}
  791. { Routines for the output }
  792. {---------------------------------------------------------------------}
  793. { looks for internal names of variables and routines }
  794. Function SearchDirectVar(var Instr: TInstruction; const hs:string;operandnum:byte): Boolean;
  795. var
  796. p : pai_external;
  797. Begin
  798. SearchDirectVar:=false;
  799. { search in the list of internals }
  800. p:=search_assembler_symbol(internals,hs,EXT_ANY);
  801. if p=nil then
  802. p:=search_assembler_symbol(externals,hs,EXT_ANY);
  803. if p<>nil then
  804. begin
  805. { get symbol name }
  806. { free the memory before changing the symbol name. }
  807. instr.operands[operandnum].ref.symbol:=newasmsymbol(p^.sym^.name);
  808. case p^.exttyp of
  809. EXT_BYTE : instr.operands[operandnum].size := S_B;
  810. EXT_WORD : instr.operands[operandnum].size := S_W;
  811. EXT_NEAR,EXT_FAR,EXT_PROC,EXT_DWORD,EXT_CODEPTR,EXT_DATAPTR:
  812. instr.operands[operandnum].size := S_L;
  813. EXT_QWORD : instr.operands[operandnum].size := S_FL;
  814. EXT_TBYTE : instr.operands[operandnum].size := S_FX;
  815. else
  816. { this is in the case where the instruction is LEA }
  817. { or something like that, in that case size is not }
  818. { important. }
  819. instr.operands[operandnum].size := S_NO;
  820. end;
  821. SearchDirectVar := TRUE;
  822. Exit;
  823. end;
  824. end;
  825. { returns an appropriate ao_xxxx flag indicating the type }
  826. { of operand. }
  827. function findtype(Var Opr: TOperand): longint;
  828. Begin
  829. With Opr do
  830. Begin
  831. case operandtype of
  832. OPR_REFERENCE: Begin
  833. if assigned(ref.symbol) then
  834. { check if in local label list }
  835. { if so then it is considered }
  836. { as a displacement. }
  837. Begin
  838. if labellist.search(ref.symbol^.name) <> nil then
  839. findtype := ao_disp
  840. else
  841. findtype := ao_mem; { probably a mem ref. }
  842. end
  843. else
  844. findtype := ao_mem;
  845. end;
  846. OPR_CONSTANT: Begin
  847. { check if there is not already a default size }
  848. if opr.size <> S_NO then
  849. Begin
  850. findtype := const_2_type[opr.size];
  851. exit;
  852. end;
  853. if val < $ff then
  854. Begin
  855. findtype := ao_imm8;
  856. opr.size := S_B;
  857. end
  858. else if val < $ffff then
  859. Begin
  860. findtype := ao_imm16;
  861. opr.size := S_W;
  862. end
  863. else
  864. Begin
  865. findtype := ao_imm32;
  866. opr.size := S_L;
  867. end
  868. end;
  869. OPR_REGISTER: Begin
  870. findtype := reg_2_type[reg];
  871. exit;
  872. end;
  873. OPR_SYMBOL: Begin
  874. findtype := ao_jumpabsolute;
  875. end;
  876. OPR_NONE: Begin
  877. findtype := 0;
  878. end;
  879. else
  880. Begin
  881. Message(assem_f_internal_error_in_findtype);
  882. end;
  883. end;
  884. end;
  885. end;
  886. Procedure HandleExtend(var instr: TInstruction);
  887. { Handles MOVZX, MOVSX ... }
  888. var
  889. instruc: tasmop;
  890. opsize : topsize;
  891. Begin
  892. instruc:=instr.getinstruction;
  893. { if we have A_MOVZX/A_MOVSX here, there is a big problem }
  894. { it should never happen, because it is already replaced }
  895. { by ConcatOpcode! }
  896. Case instruc of
  897. A_MOVZX,A_MOVSX: Message(assem_f_internal_error_in_handleextend);
  898. A_MOVSB, A_MOVSBL:
  899. Begin
  900. instruc := A_MOVSX;
  901. { Indirect memory reference with register }
  902. { no size determined, therefore we determine it }
  903. { with the suffix. }
  904. if instr.operands[1].size = S_NO then
  905. instr.operands[1].size := S_B;
  906. end;
  907. A_MOVSBW:
  908. Begin
  909. instruc := A_MOVSX;
  910. { Indirect memory reference with register }
  911. { no size determined, therefore we determine it }
  912. { with the suffix. }
  913. if instr.operands[1].size = S_NO then
  914. instr.operands[1].size := S_B;
  915. end;
  916. A_MOVSWL:
  917. Begin
  918. instruc := A_MOVSX;
  919. { Indirect memory reference with register }
  920. { no size determined, therefore we determine it }
  921. { with the suffix. }
  922. if instr.operands[1].size = S_NO then
  923. instr.operands[1].size := S_W;
  924. end;
  925. A_MOVZB:
  926. Begin
  927. instruc := A_MOVZX;
  928. { Indirect memory reference with register }
  929. { no size determined, therefore we determine it }
  930. { with the suffix. }
  931. if instr.operands[1].size = S_NO then
  932. instr.operands[1].size := S_B;
  933. end;
  934. A_MOVZWL:
  935. Begin
  936. instruc := A_MOVZX;
  937. { Indirect memory reference with register }
  938. { no size determined, therefore we determine it }
  939. { with the suffix. }
  940. if instr.operands[1].size = S_NO then
  941. instr.operands[1].size := S_W;
  942. end;
  943. End;
  944. With instr do
  945. Begin
  946. if operands[1].size = S_B then
  947. Begin
  948. if operands[2].size = S_L then
  949. opsize := S_BL
  950. else
  951. if operands[2].size = S_W then
  952. opsize := S_BW
  953. else
  954. begin
  955. Message(assem_e_invalid_size_movzx);
  956. exit;
  957. end;
  958. end
  959. else
  960. if operands[1].size = S_W then
  961. Begin
  962. if operands[2].size = S_L then
  963. opsize := S_WL
  964. else
  965. begin
  966. Message(assem_e_invalid_size_movzx);
  967. exit;
  968. end;
  969. end
  970. else
  971. begin
  972. Message(assem_e_invalid_size_movzx);
  973. exit;
  974. end;
  975. if operands[1].operandtype = OPR_REGISTER then
  976. Begin
  977. if operands[2].operandtype <> OPR_REGISTER then
  978. Message(assem_e_invalid_opcode) { exit...}
  979. else
  980. p^.concat(new(pai386,op_reg_reg(instruc,opsize,
  981. operands[1].reg,operands[2].reg)));
  982. end
  983. else
  984. if operands[1].operandtype = OPR_REFERENCE then
  985. Begin
  986. if operands[2].operandtype <> OPR_REGISTER then
  987. Message(assem_e_invalid_opcode) {exit...}
  988. else
  989. p^.concat(new(pai386,op_ref_reg(instruc,opsize,
  990. newreference(operands[1].ref),operands[2].reg)));
  991. end
  992. end; { end with }
  993. end;
  994. Function CheckAndSetOpsize(var op : toperand;size : topsize) : boolean;
  995. Begin
  996. CheckAndSetOpsize:=true;
  997. { operandtype for constant get a default value for size }
  998. { only claim for op.size in S_B, S_W or S_L :
  999. if the var is a float it will be accessed as 2 longs !! }
  1000. if (op.operandtype<>OPR_CONSTANT) and (op.size in [S_B,S_W,S_L]) and
  1001. (op.size<>size) then
  1002. begin
  1003. if (cs_compilesystem in aktmoduleswitches) or
  1004. not (cs_check_range in aktlocalswitches) then
  1005. Message(assem_w_size_suffix_and_dest_dont_match)
  1006. else
  1007. Begin
  1008. Message(assem_e_size_suffix_and_dest_dont_match);
  1009. CheckAndSetOpsize:=false;
  1010. End;
  1011. end;
  1012. op.size:=size;
  1013. End;
  1014. Procedure ConcatOpCode(var instr: TInstruction);
  1015. {*********************************************************************}
  1016. { First Pass: }
  1017. { - If this is a three operand opcode: }
  1018. { imul,shld,and shrd -> check them manually. }
  1019. {*********************************************************************}
  1020. var
  1021. fits : boolean;
  1022. i: longint;
  1023. opsize: topsize;
  1024. optyp1, optyp2, optyp3: longint;
  1025. instruc: tasmop;
  1026. href : treference;
  1027. Begin
  1028. fits := FALSE;
  1029. for i:=1 to instr.numops do
  1030. Begin
  1031. case instr.operands[i].operandtype of
  1032. OPR_REGISTER: instr.operands[i].size :=
  1033. reg_2_size[instr.operands[i].reg];
  1034. end; { end case }
  1035. end; { endif }
  1036. { setup specific instructions for first pass }
  1037. instruc := instr.getinstruction;
  1038. Case instruc Of
  1039. A_LEA,A_LDS,A_LSS,A_LES,A_LFS,A_LGS:
  1040. Begin
  1041. if instr.operands[2].size <> S_L then
  1042. Begin
  1043. Message(assem_e_16bit_base_in_32bit_segment);
  1044. exit;
  1045. end; { endif }
  1046. end;
  1047. End;
  1048. With instr do
  1049. Begin
  1050. for i:=1 to numops do
  1051. Begin
  1052. With operands[i] do
  1053. Begin
  1054. { check for 16-bit bases/indexes and emit an error. }
  1055. { we cannot only emit a warning since gas does not }
  1056. { accept 16-bit indexes and bases. }
  1057. if (operandtype = OPR_REFERENCE) and
  1058. ((ref.base <> R_NO) or
  1059. (ref.index <> R_NO)) then
  1060. Begin
  1061. { index or base defined. }
  1062. if (ref.base <> R_NO) then
  1063. Begin
  1064. if not (ref.base in
  1065. [R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESI,R_EDI,R_ESP]) then
  1066. Message(assem_e_16bit_base_in_32bit_segment);
  1067. end;
  1068. { index or base defined. }
  1069. if (ref.index <> R_NO) then
  1070. Begin
  1071. if not (ref.index in
  1072. [R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESI,R_EDI,R_ESP]) then
  1073. Message(assem_e_16bit_index_in_32bit_segment);
  1074. end;
  1075. end;
  1076. { Check for constants without bases/indexes in memory }
  1077. { references. }
  1078. { Update: allow constant references under Go32v2, to }
  1079. { access data in the bios data segmement (JM) }
  1080. if (target_info.target<>target_i386_Go32v2) and
  1081. ((operandtype = OPR_REFERENCE) and
  1082. (ref.base = R_NO) and
  1083. (ref.index = R_NO) and
  1084. (ref.symbol = nil) and
  1085. (ref.offset <> 0)) then
  1086. Begin
  1087. ref.is_immediate := TRUE;
  1088. Message(assem_e_const_ref_not_allowed);
  1089. end;
  1090. opinfo := findtype(operands[i]);
  1091. end; { end with }
  1092. end; {endfor}
  1093. { TAKE CARE OF SPECIAL OPCODES, TAKE CARE OF THEM INDIVUALLY. }
  1094. { ALL THE REST ARE TAKEN CARE BY OPCODE TABLE AND THIRD PASS. }
  1095. { is this right for ratti386 ? (PM) }
  1096. { almost... here we check for the size of registers and references }
  1097. { to determine the correct gas opcode to use, because if the token }
  1098. { is A_MOVSX or A_MOVZX then that means that the person forgot to }
  1099. { specify the size.. }
  1100. { if memory size is not specified, will of course give out an error}
  1101. Case instruc Of
  1102. A_MOVSX:
  1103. Begin
  1104. if numops = 2 then
  1105. begin
  1106. if stropsize = S_BL then
  1107. begin
  1108. CheckAndSetOpsize(operands[1],S_B);
  1109. stropsize := S_NO;
  1110. CheckAndSetOpsize(operands[2],S_L);
  1111. addinstr(A_MOVSBL)
  1112. end
  1113. else
  1114. if stropsize = S_WL then
  1115. begin
  1116. CheckAndSetOpsize(operands[1],S_W);
  1117. stropsize := S_NO;
  1118. CheckAndSetOpsize(operands[2],S_L);
  1119. addinstr(A_MOVSWL)
  1120. end
  1121. else
  1122. if stropsize = S_BW then
  1123. begin
  1124. CheckAndSetOpsize(operands[1],S_B);
  1125. stropsize := S_NO;
  1126. CheckAndSetOpsize(operands[2],S_W);
  1127. addinstr(A_MOVSBW)
  1128. end
  1129. else
  1130. if (operands[1].size = S_B) and
  1131. (operands[2].size = S_W) then
  1132. addinstr(A_MOVSBW)
  1133. else
  1134. if (operands[1].size = S_B) and
  1135. (operands[2].size = S_L) then
  1136. addinstr(A_MOVSBL)
  1137. else
  1138. if (operands[1].size = S_W) and
  1139. (operands[2].size = S_L) then
  1140. addinstr(A_MOVSWL)
  1141. else
  1142. begin
  1143. Message(assem_e_invalid_size_movzx);
  1144. exit;
  1145. end;
  1146. instruc := getinstruction; { reload instruction }
  1147. end
  1148. else
  1149. begin
  1150. Message(assem_e_too_many_operands);
  1151. exit;
  1152. end;
  1153. end;
  1154. A_MOVZX:
  1155. Begin
  1156. if numops = 2 then
  1157. Begin
  1158. if stropsize = S_BW then
  1159. begin
  1160. CheckAndSetOpsize(operands[1],S_B);
  1161. stropsize := S_NO;
  1162. CheckAndSetOpsize(operands[2],S_W);
  1163. addinstr(A_MOVZB)
  1164. end
  1165. else
  1166. if stropsize = S_BL then
  1167. begin
  1168. CheckAndSetOpsize(operands[1],S_B);
  1169. stropsize := S_NO;
  1170. CheckAndSetOpsize(operands[2],S_L);
  1171. addinstr(A_MOVZB)
  1172. end
  1173. else
  1174. if stropsize = S_WL then
  1175. begin
  1176. CheckAndSetOpsize(operands[1],S_W);
  1177. stropsize := S_NO;
  1178. CheckAndSetOpsize(operands[2],S_L);
  1179. addinstr(A_MOVZWL)
  1180. end
  1181. else
  1182. { change the instruction to conform to GAS }
  1183. if (operands[1].size = S_B) and
  1184. (operands[2].size in [S_W,S_L]) then
  1185. addinstr(A_MOVZB)
  1186. else
  1187. if (operands[1].size = S_W) and
  1188. (operands[2].size = S_L) then
  1189. addinstr(A_MOVZWL)
  1190. else
  1191. begin
  1192. Message(assem_e_invalid_size_movzx);
  1193. exit;
  1194. end;
  1195. instruc := getinstruction; { reload instruction }
  1196. end
  1197. else
  1198. Begin
  1199. Message(assem_e_too_many_operands);
  1200. exit;
  1201. end;
  1202. end;
  1203. A_FWAIT: FWaitWarning;
  1204. A_BT,A_BTC,A_BTR,A_BTS:
  1205. Begin
  1206. if numops = 2 then
  1207. Begin
  1208. if (operands[1].operandtype = OPR_CONSTANT)
  1209. and (operands[1].val <= $ff) then
  1210. Begin
  1211. operands[1].opinfo := ao_imm8;
  1212. { no operand size if using constant. }
  1213. operands[1].size := S_NO;
  1214. fits := TRUE;
  1215. end
  1216. end
  1217. else
  1218. Begin
  1219. Message(assem_e_invalid_opcode_and_operand);
  1220. exit;
  1221. end;
  1222. end;
  1223. A_ENTER:
  1224. Begin
  1225. if numops =2 then
  1226. Begin
  1227. if (operands[1].operandtype = OPR_CONSTANT) and
  1228. (operands[1].val <= $ffff) then
  1229. Begin
  1230. operands[1].opinfo := ao_imm16;
  1231. end { endif }
  1232. end { endif }
  1233. else
  1234. Begin
  1235. Message(assem_e_invalid_opcode_and_operand);
  1236. exit;
  1237. end
  1238. end;
  1239. { Handle special opcodes for the opcode }
  1240. { table. Set them up correctly. }
  1241. A_INS,A_IN:
  1242. Begin
  1243. if numops =2 then
  1244. Begin
  1245. if (operands[1].operandtype = OPR_REGISTER) and
  1246. (operands[1].reg = R_DX) then
  1247. Begin
  1248. operands[1].opinfo := ao_inoutportreg;
  1249. if (operands[2].operandtype = OPR_REGISTER) and
  1250. (operands[2].reg in [R_EAX,R_AX,R_AL]) and
  1251. (instruc = A_IN) then
  1252. Begin
  1253. operands[2].opinfo := ao_acc;
  1254. case operands[2].reg of
  1255. R_EAX: operands[2].size := S_L;
  1256. R_AX: operands[2].size := S_W;
  1257. R_AL: operands[2].size := S_B;
  1258. end;
  1259. end
  1260. end
  1261. else
  1262. if (operands[1].operandtype = OPR_CONSTANT) and
  1263. (operands[1].val <= $ff) And
  1264. (instruc = A_IN) then
  1265. Begin
  1266. operands[1].opinfo := ao_imm8;
  1267. operands[1].size := S_B;
  1268. if (operands[2].operandtype = OPR_REGISTER) and
  1269. (operands[2].reg in [R_EAX,R_AX,R_AL]) and
  1270. (instruc = A_IN) then
  1271. Begin
  1272. operands[2].opinfo := ao_acc;
  1273. end
  1274. end;
  1275. end
  1276. else
  1277. Begin
  1278. Message(assem_e_invalid_opcode_and_operand);
  1279. exit;
  1280. end;
  1281. end;
  1282. A_OUTS,A_OUT:
  1283. Begin
  1284. if numops =2 then
  1285. Begin
  1286. if (operands[2].operandtype = OPR_REGISTER) and
  1287. (operands[2].reg = R_DX) then
  1288. Begin
  1289. operands[2].opinfo := ao_inoutportreg;
  1290. if (operands[1].operandtype = OPR_REGISTER) and
  1291. (operands[1].reg in [R_EAX,R_AX,R_AL]) and
  1292. (instruc = A_OUT) then
  1293. Begin
  1294. operands[1].opinfo := ao_acc;
  1295. fits := TRUE;
  1296. end
  1297. end
  1298. else
  1299. if (operands[2].operandtype = OPR_CONSTANT) and
  1300. (operands[2].val <= $ff) and
  1301. (instruc = A_OUT) then
  1302. Begin
  1303. operands[2].opinfo := ao_imm8;
  1304. operands[2].size := S_B;
  1305. if (operands[1].operandtype = OPR_REGISTER) and
  1306. (operands[1].reg in [R_EAX,R_AX,R_AL]) and
  1307. (instruc = A_OUT) then
  1308. Begin
  1309. operands[1].opinfo := ao_acc;
  1310. fits := TRUE;
  1311. end
  1312. end;
  1313. end
  1314. else
  1315. Begin
  1316. Message(assem_e_invalid_opcode_and_operand);
  1317. exit;
  1318. end;
  1319. end;
  1320. A_RCL,A_RCR,A_ROL,A_ROR,A_SAL,A_SAR,A_SHL,A_SHR:
  1321. Begin
  1322. if numops =2 then
  1323. Begin
  1324. if (operands[1].operandtype = OPR_REGISTER) and
  1325. (operands[1].reg = R_CL) then
  1326. Begin
  1327. operands[1].opinfo := ao_shiftcount
  1328. end
  1329. else
  1330. if (operands[1].operandtype = OPR_CONSTANT) and
  1331. (operands[1].val <= $ff) then
  1332. Begin
  1333. operands[1].opinfo := ao_imm8;
  1334. operands[1].size := S_B;
  1335. end;
  1336. end
  1337. else { if numops = 2 }
  1338. Begin
  1339. Message(assem_e_invalid_opcode_and_operand);
  1340. exit;
  1341. end;
  1342. end;
  1343. { this did not work (PM) }
  1344. A_DIV, A_IDIV:
  1345. Begin
  1346. if (operands[2].operandtype = OPR_REGISTER) and
  1347. (operands[2].reg in [R_AL,R_AX,R_EAX]) then
  1348. operands[2].opinfo := ao_acc;
  1349. end;
  1350. A_FNSTSW, A_FSTSW:
  1351. Begin
  1352. { %ax can be omitted in ATT syntax }
  1353. if numops = 0 then
  1354. Begin
  1355. numops:=1;
  1356. operands[1].operandtype:=OPR_REGISTER;
  1357. operands[1].reg:=R_AX;
  1358. operands[1].opinfo := ao_acc;
  1359. end
  1360. else
  1361. if numops = 1 then
  1362. Begin
  1363. if (operands[1].operandtype = OPR_REGISTER) and
  1364. (operands[1].reg = R_AX) then
  1365. operands[1].opinfo := ao_acc;
  1366. end
  1367. else
  1368. Begin
  1369. Message(assem_e_invalid_opcode_and_operand);
  1370. exit;
  1371. end;
  1372. end;
  1373. A_SHLD,A_SHRD:
  1374. { these instruction are fully parsed individually on pass three }
  1375. { so we just do a summary checking here. }
  1376. Begin
  1377. if numops = 3 then
  1378. Begin
  1379. if (operands[1].operandtype = OPR_CONSTANT) and
  1380. (operands[1].val <= $ff) then
  1381. Begin
  1382. operands[1].opinfo := ao_imm8;
  1383. operands[1].size := S_B;
  1384. end
  1385. else if (operands[1].operandtype = OPR_REGISTER) and
  1386. (operands[1].reg = R_CL) then
  1387. Begin
  1388. operands[1].opinfo := ao_shiftcount;
  1389. operands[1].size := S_B;
  1390. end;
  1391. end
  1392. else
  1393. Begin
  1394. Message(assem_e_invalid_opcode_and_operand);
  1395. exit;
  1396. end;
  1397. end;
  1398. A_IMUL:
  1399. { these instruction are fully parsed individually on pass three }
  1400. { so we just do a summary checking here. }
  1401. Begin
  1402. if numops = 3 then
  1403. Begin
  1404. if (operands[1].operandtype = OPR_CONSTANT) and
  1405. (operands[1].val <= 127) and (operands[1].val >=-128) then
  1406. Begin
  1407. operands[1].opinfo := ao_imm8s;
  1408. operands[1].size := S_B;
  1409. end
  1410. else
  1411. Begin
  1412. { should depend on size of other operands !! }
  1413. operands[1].opinfo := ao_imm32;
  1414. operands[1].size := S_L;
  1415. End
  1416. end;
  1417. end;
  1418. A_INT:
  1419. Begin
  1420. if numops = 1 then
  1421. Begin
  1422. if (operands[1].operandtype = OPR_CONSTANT) and
  1423. (operands[1].val <= $ff) then
  1424. operands[1].opinfo := ao_imm8;
  1425. end
  1426. end;
  1427. A_RET:
  1428. Begin
  1429. if numops =1 then
  1430. Begin
  1431. if (operands[1].operandtype = OPR_CONSTANT) and
  1432. (operands[1].val <= $ffff) then
  1433. operands[1].opinfo := ao_imm16;
  1434. end
  1435. end;
  1436. { all string instructions have default memory }
  1437. { location which are ignored. Take care of }
  1438. { those. }
  1439. { Here could be added the code for segment }
  1440. { overrides. }
  1441. A_SCAS,A_CMPS,A_STOS,A_LODS:
  1442. Begin
  1443. if numops =1 then
  1444. Begin
  1445. operands[1].operandtype := OPR_NONE;
  1446. numops := 0;
  1447. end;
  1448. end;
  1449. A_XLAT:
  1450. Begin
  1451. { handle special TP syntax case for XLAT }
  1452. { here we accept XLAT, XLATB and XLAT m8 }
  1453. if (numops = 1) or (numops = 0) then
  1454. Begin
  1455. operands[1].operandtype := OPR_NONE;
  1456. numops := 0;
  1457. { always a byte for XLAT }
  1458. instr.stropsize := S_B;
  1459. end;
  1460. end;
  1461. { ------------------------------------------------------------------- }
  1462. { ------------------------- SIZE CHECK ------------------------------ }
  1463. { ------------- presently done only for most used opcodes ---------- }
  1464. { Checks if the suffix concords with the destination size , if }
  1465. { not gives out an error. (This check is stricter then gas but is }
  1466. { REQUIRED for intasmi3) }
  1467. A_MOV,A_ADD,A_SUB,A_ADC,A_SBB,A_CMP,A_AND,A_OR,A_TEST,A_XOR:
  1468. begin
  1469. if (instr.stropsize <> S_NO) then
  1470. Begin
  1471. if not CheckAndSetOpsize(operands[1],instr.stropsize) or
  1472. not CheckAndSetOpsize(operands[2],instr.stropsize) then
  1473. End;
  1474. end;
  1475. A_DEC,A_INC,A_NOT,A_NEG:
  1476. begin
  1477. if (instr.stropsize <> S_NO) and
  1478. (instr.operands[1].size <> S_NO) then
  1479. if (instr.stropsize <> instr.operands[1].size) then
  1480. begin
  1481. Message(assem_e_size_suffix_and_dest_reg_dont_match);
  1482. exit;
  1483. end;
  1484. end;
  1485. A_PUSH:
  1486. Begin
  1487. if (instr.operands[1].operandtype = OPR_CONSTANT) and
  1488. (instr.stropsize = S_NO) then
  1489. Message(assem_e_size_suffix_and_dest_reg_dont_match);
  1490. end;
  1491. End; { case }
  1492. {we have to start a new case because A_INS etc are already handled before
  1493. as well... (JM)}
  1494. Case instruc of
  1495. A_INS,A_MOVS,A_OUTS:
  1496. Begin
  1497. if numops =2 then
  1498. Begin
  1499. operands[2].operandtype := OPR_NONE;
  1500. operands[1].operandtype := OPR_NONE;
  1501. numops := 0;
  1502. end;
  1503. end;
  1504. { handle parameter for segment overrides }
  1505. end; { case }
  1506. { ------------------------------------------------------------------- }
  1507. { copy them to local variables }
  1508. { for faster access }
  1509. optyp1:=operands[1].opinfo;
  1510. optyp2:=operands[2].opinfo;
  1511. optyp3:=operands[3].opinfo;
  1512. end; { end with }
  1513. { after reading the operands }
  1514. { search the instruction }
  1515. { setup startvalue from cache }
  1516. if itcache^[instruc]<>-1 then
  1517. i:=itcache^[instruc]
  1518. else
  1519. i:=0;
  1520. { I think this is too dangerous for me therefore i decided that for }
  1521. { the att version only if the processor > i386 or we are compiling }
  1522. { the system unit then this will be allowed... }
  1523. if (instruc > lastop_ittable) then
  1524. begin
  1525. Message1(assem_w_opcode_not_in_table,upper(att_op2str[instruc]));
  1526. fits:=true;
  1527. end
  1528. else while not(fits) do
  1529. begin
  1530. { set the instruction cache, if the instruction }
  1531. { occurs the first time }
  1532. if (it[i].i=instruc) and (itcache^[instruc]=-1) then
  1533. itcache^[instruc]:=i;
  1534. if (it[i].i=instruc) and (instr.numops=it[i].ops) then
  1535. begin
  1536. { first fit }
  1537. case instr.numops of
  1538. 0 : begin
  1539. fits:=true;
  1540. break;
  1541. end;
  1542. 1 :
  1543. Begin
  1544. if (optyp1 and it[i].o1)<>0 then
  1545. Begin
  1546. fits:=true;
  1547. break;
  1548. end;
  1549. { I consider sign-extended 8bit value to }
  1550. { be equal to immediate 8bit therefore }
  1551. { convert... }
  1552. if (optyp1 = ao_imm8) then
  1553. Begin
  1554. { check if this is a simple sign extend. }
  1555. if (it[i].o1<>ao_imm8s) then
  1556. Begin
  1557. fits:=true;
  1558. break;
  1559. end;
  1560. end;
  1561. end;
  1562. 2 : if ((optyp1 and it[i].o1)<>0) and
  1563. ((optyp2 and it[i].o2)<>0) then
  1564. Begin
  1565. fits:=true;
  1566. break;
  1567. end
  1568. { if the operands can be swaped }
  1569. { then swap them }
  1570. else if ((it[i].m and af_d)<>0) and
  1571. ((optyp1 and it[i].o2)<>0) and
  1572. ((optyp2 and it[i].o1)<>0) then
  1573. begin
  1574. fits:=true;
  1575. break;
  1576. end;
  1577. 3 : if ((optyp1 and it[i].o1)<>0) and
  1578. ((optyp2 and it[i].o2)<>0) and
  1579. ((optyp3 and it[i].o3)<>0) then
  1580. Begin
  1581. fits:=true;
  1582. break;
  1583. end;
  1584. end; { end case }
  1585. end; { endif }
  1586. if it[i].i=A_NONE then
  1587. begin
  1588. { NO MATCH! }
  1589. Message(assem_e_invalid_opcode_and_operand);
  1590. exit;
  1591. end;
  1592. inc(i);
  1593. end; { end while }
  1594. { We add the opcode to the opcode linked list }
  1595. if fits then
  1596. Begin
  1597. if instr.getprefix <> A_NONE then
  1598. Begin
  1599. p^.concat(new(pai386,op_none(instr.getprefix,S_NO)));
  1600. end;
  1601. { change from AT&T styled floating point to }
  1602. { intel styled floating point with valid size }
  1603. { we use these instructions so it does not }
  1604. { mess up intasmi3 }
  1605. if (instruc >= A_FILDQ) and (instruc <= A_FIDIVRS) then
  1606. Begin
  1607. instr.stropsize := _fpusizes[instruc];
  1608. instr.addinstr(_fpuopcodes[instruc]);
  1609. instruc := instr.getinstruction;
  1610. end;
  1611. case instr.numops of
  1612. 0:
  1613. if instr.stropsize <> S_NO then
  1614. { is this a string operation opcode or xlat then check }
  1615. { the size of the operation. }
  1616. p^.concat(new(pai386,op_none(instruc,instr.stropsize)))
  1617. else
  1618. p^.concat(new(pai386,op_none(instruc,S_NO)));
  1619. 1: Begin
  1620. case instr.operands[1].operandtype of
  1621. { GAS requires that the size be set in the case of a push }
  1622. { constant value, }
  1623. OPR_CONSTANT:
  1624. Begin
  1625. if instruc = A_PUSH then
  1626. Begin
  1627. if instr.stropsize <> S_NO then
  1628. p^.concat(new(pai386,op_const(instruc,
  1629. instr.stropsize, instr.operands[1].val)))
  1630. else
  1631. Message(assem_e_invalid_opcode_and_operand);
  1632. end
  1633. else
  1634. p^.concat(new(pai386,op_const(instruc,
  1635. S_NO, instr.operands[1].val)));
  1636. end;
  1637. OPR_REGISTER:
  1638. Begin
  1639. if (instr.stropsize<>S_NO) then
  1640. opsize:=instr.stropsize
  1641. else
  1642. opsize:=instr.operands[1].size;
  1643. p^.concat(new(pai386,op_reg(instruc,opsize,instr.operands[1].reg)));
  1644. End;
  1645. OPR_SYMBOL:
  1646. Begin
  1647. p^.concat(new(pai386,op_sym(instruc,
  1648. instr.stropsize,instr.operands[1].symbol)));
  1649. End;
  1650. OPR_REFERENCE:
  1651. { now first check suffix ... }
  1652. if instr.stropsize <> S_NO then
  1653. Begin
  1654. p^.concat(new(pai386,op_ref(instruc,
  1655. instr.stropsize,newreference(instr.operands[1].ref))));
  1656. end
  1657. { no suffix... therefore resort using intel styled checking .. }
  1658. else if (instr.operands[1].size <> S_NO) and
  1659. Not ((instruc = A_CALL) or (instruc = A_JMP)) then
  1660. Begin
  1661. p^.concat(new(pai386,op_ref(instruc,
  1662. instr.operands[1].size,newreference(instr.operands[1].ref))));
  1663. end
  1664. else
  1665. Begin
  1666. { special jmp and call case with }
  1667. { symbolic references. }
  1668. Case instruc of
  1669. A_CALL,A_JMP,A_FNSTCW,A_FSTCW,A_FLDCW,A_FNSTSW,
  1670. A_FSTSW,A_FLDENV,A_FSTENV,A_FNSAVE,A_FSAVE:
  1671. Begin
  1672. p^.concat(new(pai386,op_ref(instruc,
  1673. S_NO,newreference(instr.operands[1].ref))));
  1674. end
  1675. else
  1676. Message(assem_e_invalid_opcode_and_operand);
  1677. End
  1678. end;
  1679. { This either crashed the compiler or the symbol would always be nil! }
  1680. { The problem is here is I didn't see any way of adding the labeled }
  1681. { symbol in the internal list, since i think from what i see in aasm }
  1682. { that these will automatically be declared as external ?? }
  1683. { note: has to be changed to a case if ever activated again to avoid }
  1684. { range problems (JM) }
  1685. { if (instruc in [A_JO,A_JNO,A_JB,A_JC,A_JNAE,
  1686. A_JNB,A_JNC,A_JAE,A_JE,A_JZ,A_JNE,A_JNZ,A_JBE,A_JNA,A_JNBE,
  1687. A_JA,A_JS,A_JNS,A_JP,A_JPE,A_JNP,A_JPO,A_JL,A_JNGE,A_JNL,A_JGE,
  1688. A_JLE,A_JNG,A_JNLE,A_JG,A_JCXZ,A_JECXZ,A_LOOP,A_LOOPZ,A_LOOPE,
  1689. A_LOOPNZ,A_LOOPNE,A_JMP,A_CALL]) then
  1690. Begin
  1691. if assigned(instr.operands[1].ref.symbol) then
  1692. p^.concat(new(pai386,op_csymbol(instruc,
  1693. S_NO,newcsymbol(instr.operands[1].ref.symbol^,instr.operands[1].ref.offset))))
  1694. else
  1695. Message(assem_e_invalid_opcode_and_operand);
  1696. end
  1697. else
  1698. else
  1699. Message(assem_e_invalid_opcode_and_operand);
  1700. end;}
  1701. OPR_NONE: Begin
  1702. Message(assem_f_internal_error_in_concatopcode);
  1703. end;
  1704. else
  1705. Begin
  1706. Message(assem_f_internal_error_in_concatopcode);
  1707. end;
  1708. end;
  1709. end;
  1710. 2:
  1711. Begin
  1712. Case instruc Of
  1713. A_MOVSX,A_MOVZX,A_MOVSB,A_MOVSBL,A_MOVSBW,A_MOVSWL,
  1714. A_MOVZB,A_MOVZWL:
  1715. { movzx and movsx }
  1716. HandleExtend(instr);
  1717. else
  1718. { other instructions }
  1719. Begin
  1720. With instr do
  1721. Begin
  1722. { source }
  1723. opsize := operands[1].size;
  1724. case operands[1].operandtype of
  1725. { reg,reg }
  1726. { reg,ref }
  1727. { const,reg -- IN/OUT }
  1728. OPR_REGISTER:
  1729. Begin
  1730. case operands[2].operandtype of
  1731. OPR_REGISTER:
  1732. { correction: according to the DJGPP FAQ, gas }
  1733. { doesn't even check correctly the size of }
  1734. { operands, therefore let us specify a size! }
  1735. { as in the GAS docs... destination tells us }
  1736. { the size! This might give out invalid output }
  1737. { in some very rare cases (because the size }
  1738. { checking is still not perfect). }
  1739. if (opsize = operands[2].size) then
  1740. begin
  1741. p^.concat(new(pai386,op_reg_reg(instruc,
  1742. opsize,operands[1].reg,operands[2].reg)));
  1743. end
  1744. else
  1745. Case instruc of
  1746. A_IN:
  1747. p^.concat(new(pai386,op_reg_reg(instruc,
  1748. operands[2].size,operands[1].reg,operands[2].reg)));
  1749. A_OUT:
  1750. p^.concat(new(pai386,op_reg_reg(instruc,
  1751. operands[1].size,operands[1].reg,operands[2].reg)));
  1752. { these do not require any size specification. }
  1753. A_SAL,A_SAR,A_SHL,A_SHR,A_ROL,A_ROR,A_RCR,
  1754. A_RCL:
  1755. { outs and ins are already taken care by }
  1756. { the first pass. }
  1757. p^.concat(new(pai386,op_reg_reg(instruc,
  1758. S_NO,operands[1].reg,operands[2].reg)))
  1759. else
  1760. if stropsize <> S_NO then
  1761. Begin
  1762. p^.concat(new(pai386,op_reg_reg(instruc,
  1763. stropsize,operands[1].reg,operands[2].reg)))
  1764. end
  1765. else
  1766. Begin
  1767. Message(assem_e_invalid_opcode_and_operand);
  1768. end;
  1769. end; {case instruc of}
  1770. OPR_REFERENCE:
  1771. { variable name. }
  1772. { here we must check the instruction type }
  1773. { before deciding if to use and compare }
  1774. { any sizes. }
  1775. if assigned(operands[2].ref.symbol) then
  1776. Begin
  1777. if stropsize <> S_NO then
  1778. Begin
  1779. p^.concat(new(pai386,op_reg_ref(instruc,
  1780. stropsize,operands[1].reg,newreference(operands[2].ref))))
  1781. end
  1782. else
  1783. if (opsize = operands[2].size) Then
  1784. p^.concat(new(pai386,op_reg_ref(instruc,
  1785. opsize,operands[1].reg,newreference(operands[2].ref))))
  1786. else
  1787. Case instruc of
  1788. A_RCL,A_RCR,A_ROL,A_ROR,A_SAL,A_SAR,
  1789. A_SHR,A_SHL:
  1790. p^.concat(new(pai386,op_reg_ref(instruc,
  1791. opsize,operands[1].reg,newreference(operands[2].ref))))
  1792. else
  1793. Message(assem_e_invalid_size_in_ref);
  1794. end
  1795. end
  1796. else
  1797. Begin
  1798. { register reference }
  1799. if stropsize <> S_NO then
  1800. Begin
  1801. p^.concat(new(pai386,op_reg_ref(instruc,
  1802. stropsize,operands[1].reg,newreference(operands[2].ref))))
  1803. end
  1804. else
  1805. if (opsize = operands[2].size) or
  1806. (operands[2].size = S_NO) then
  1807. p^.concat(new(pai386,op_reg_ref(instruc,
  1808. opsize,operands[1].reg,newreference(operands[2].ref))))
  1809. else
  1810. Message(assem_e_invalid_size_in_ref);
  1811. end;
  1812. OPR_CONSTANT: { OUT }
  1813. begin
  1814. { determine first with suffix }
  1815. if instruc = A_OUT then
  1816. begin
  1817. if instr.stropsize <> S_NO then
  1818. p^.concat(new(pai386,op_reg_const(instruc,stropsize,
  1819. instr.operands[1].reg, instr.operands[2].val)))
  1820. else
  1821. p^.concat(new(pai386,op_reg_const(instruc,S_NO,
  1822. instr.operands[1].reg, instr.operands[2].val)));
  1823. end
  1824. else
  1825. Message(assem_e_invalid_opcode);
  1826. end;
  1827. else { else case }
  1828. Begin
  1829. Message(assem_f_internal_error_in_concatopcode);
  1830. end;
  1831. end; { end inner case }
  1832. end;
  1833. { const,reg }
  1834. { const,const }
  1835. { const,ref }
  1836. OPR_CONSTANT:
  1837. case instr.operands[2].operandtype of
  1838. { constant, constant does not have a specific size. }
  1839. OPR_CONSTANT:
  1840. p^.concat(new(pai386,op_const_const(instruc,
  1841. S_NO,operands[1].val,operands[2].val)));
  1842. OPR_REFERENCE:
  1843. Begin
  1844. { check for suffix first ... }
  1845. if (instr.stropsize <> S_NO) then
  1846. Begin
  1847. p^.concat(new(pai386,op_const_ref(instruc,
  1848. stropsize,operands[1].val,
  1849. newreference(operands[2].ref))))
  1850. end
  1851. else
  1852. { resort to intel styled checking ... }
  1853. if (operands[1].val <= $ff) and
  1854. (operands[2].size in [S_B,S_W,S_L]) then
  1855. p^.concat(new(pai386,op_const_ref(instruc,
  1856. operands[2].size,operands[1].val,
  1857. newreference(operands[2].ref))))
  1858. else
  1859. if (operands[1].val <= $ffff) and
  1860. (operands[2].size in [S_W,S_L]) then
  1861. p^.concat(new(pai386,op_const_ref(instruc,
  1862. operands[2].size,operands[1].val,
  1863. newreference(operands[2].ref))))
  1864. else
  1865. if (operands[1].val <= $7fffffff) and
  1866. (operands[2].size in [S_L]) then
  1867. p^.concat(new(pai386,op_const_ref(instruc,
  1868. operands[2].size,operands[1].val,
  1869. newreference(operands[2].ref))))
  1870. else
  1871. Message(assem_e_invalid_size_in_ref);
  1872. end;
  1873. OPR_REGISTER:
  1874. Begin
  1875. { size of opcode determined by register }
  1876. if (operands[1].val <= $ff) and
  1877. (operands[2].size in [S_B,S_W,S_L]) then
  1878. p^.concat(new(pai386,op_const_reg(instruc,
  1879. operands[2].size,operands[1].val,
  1880. operands[2].reg)))
  1881. else
  1882. if (operands[1].val <= $ffff) and
  1883. (operands[2].size in [S_W,S_L]) then
  1884. p^.concat(new(pai386,op_const_reg(instruc,
  1885. operands[2].size,operands[1].val,
  1886. operands[2].reg)))
  1887. else
  1888. if (operands[1].val <= $7fffffff) and
  1889. (operands[2].size in [S_L]) then
  1890. p^.concat(new(pai386,op_const_reg(instruc,
  1891. operands[2].size,operands[1].val,
  1892. operands[2].reg)))
  1893. else
  1894. Message(assem_e_invalid_opcode_size);
  1895. end;
  1896. else
  1897. Begin
  1898. Message(assem_f_internal_error_in_concatopcode);
  1899. end;
  1900. end; { end case }
  1901. { sym,reg }
  1902. OPR_SYMBOL:
  1903. case instr.operands[2].operandtype of
  1904. OPR_REGISTER:
  1905. Begin
  1906. Case instruc of
  1907. A_LDS,A_LES,A_LFS,A_LGS,A_LSS:
  1908. opsize:=S_NO
  1909. else
  1910. if stropsize<>S_NO then
  1911. opsize:=stropsize
  1912. else
  1913. if (opsize<>operands[2].size) then
  1914. Message(assem_e_invalid_opcode_and_operand);
  1915. { create an temporary reference }
  1916. End; {case}
  1917. reset_reference(href);
  1918. href.symbol:=instr.operands[1].symbol;
  1919. p^.concat(new(pai386,op_ref_reg(instruc,opsize,
  1920. newreference(href),operands[2].reg)));
  1921. clear_reference(href);
  1922. end;
  1923. else
  1924. Begin
  1925. Message(assem_f_internal_error_in_concatopcode);
  1926. end;
  1927. end; { end inner case }
  1928. { ref,reg }
  1929. { ref,ref }
  1930. OPR_REFERENCE:
  1931. case instr.operands[2].operandtype of
  1932. OPR_REGISTER:
  1933. if assigned(operands[1].ref.symbol) then
  1934. { global variable }
  1935. Begin
  1936. Case instruc Of
  1937. A_LDS,A_LES,A_LFS,A_LGS,A_LSS:
  1938. opsize:=S_NO
  1939. else
  1940. if stropsize<>S_NO then
  1941. opsize:=stropsize
  1942. else
  1943. if (opsize<>operands[2].size) then
  1944. Message(assem_e_invalid_opcode_and_operand);
  1945. end; { case }
  1946. p^.concat(new(pai386,op_ref_reg(instruc,
  1947. opsize,newreference(operands[1].ref),operands[2].reg)));
  1948. end
  1949. else
  1950. Begin
  1951. { register reference }
  1952. { possiblities:1) local variable which }
  1953. { has been replaced by bp and offset }
  1954. { in this case size should be valid }
  1955. { 2) Indirect register }
  1956. { adressing, 2nd operand determines }
  1957. { size. }
  1958. if (stropsize <> S_NO) then
  1959. Begin
  1960. p^.concat(new(pai386,op_ref_reg(instruc,
  1961. stropsize,newreference(operands[1].ref),
  1962. operands[2].reg)))
  1963. end
  1964. else
  1965. if (opsize = operands[2].size) or
  1966. (opsize = S_NO) then
  1967. Begin
  1968. p^.concat(new(pai386,op_ref_reg(instruc,
  1969. operands[2].size,newreference(operands[1].ref),
  1970. operands[2].reg)));
  1971. end
  1972. else
  1973. Message(assem_e_invalid_size_in_ref);
  1974. end;
  1975. OPR_REFERENCE: { special opcodes }
  1976. p^.concat(new(pai386,op_ref_ref(instruc,
  1977. opsize,newreference(operands[1].ref),
  1978. newreference(operands[2].ref))));
  1979. else
  1980. Begin
  1981. Message(assem_f_internal_error_in_concatopcode);
  1982. end;
  1983. end; { end inner case }
  1984. end; { end case }
  1985. end; { end with }
  1986. end; {end else of case instruc of movsx ... }
  1987. end; { end case movsx ...}
  1988. end;
  1989. 3: Begin
  1990. { only imul, shld and shrd }
  1991. { middle must be a register }
  1992. if ((instruc = A_SHLD) or (instruc = A_SHRD)) and (instr.operands[2].operandtype =
  1993. OPR_REGISTER) then
  1994. Begin
  1995. case instr.operands[2].size of
  1996. S_W:
  1997. if instr.operands[1].operandtype = OPR_CONSTANT then
  1998. Begin
  1999. if instr.operands[1].val <= $ff then
  2000. Begin
  2001. if instr.operands[3].size in [S_NO,S_W] then
  2002. Begin
  2003. case instr.operands[3].operandtype of
  2004. OPR_REFERENCE:
  2005. {$IfNDef NO_OP3}
  2006. p^.concat(new(pai386,
  2007. op_const_reg_ref(instruc, S_W,
  2008. instr.operands[1].val, instr.operands[2].reg,
  2009. newreference(instr.operands[3].ref))));
  2010. {$else NO_OP3}
  2011. Message(assem_e_unsupported_opcode_and_operand)
  2012. { MISSING !!!! } ;
  2013. {$endif NO_OP3}
  2014. OPR_REGISTER:
  2015. p^.concat(new(pai386,
  2016. op_const_reg_reg(instruc, S_W,
  2017. instr.operands[1].val, instr.operands[2].reg,
  2018. instr.operands[3].reg)));
  2019. else
  2020. Message(assem_e_invalid_opcode_and_operand);
  2021. end;
  2022. end
  2023. else
  2024. Message(assem_e_invalid_opcode_and_operand);
  2025. end;
  2026. end
  2027. else if instr.operands[1].operandtype = OPR_REGISTER then
  2028. {$ifdef NO_OP3}
  2029. Message(assem_e_unsupported_opcode_and_operand)
  2030. {$else NO_OP3}
  2031. begin
  2032. case instr.operands[3].operandtype of
  2033. OPR_REFERENCE:
  2034. p^.concat(new(pai386,
  2035. op_reg_reg_ref(instruc, S_W,
  2036. instr.operands[1].reg, instr.operands[2].reg,
  2037. newreference(instr.operands[3].ref))));
  2038. OPR_REGISTER:
  2039. p^.concat(new(pai386,
  2040. op_reg_reg_reg(instruc, S_W,
  2041. instr.operands[1].reg, instr.operands[2].reg,
  2042. instr.operands[3].reg)));
  2043. else
  2044. Message(assem_e_invalid_opcode_and_operand);
  2045. end;
  2046. end
  2047. {$endif NO_OP3}
  2048. else
  2049. Message(assem_e_invalid_opcode_and_operand);
  2050. S_L:
  2051. if instr.operands[1].operandtype = OPR_CONSTANT then
  2052. Begin
  2053. if instr.operands[1].val <= $ff then
  2054. Begin
  2055. if instr.operands[3].size in [S_NO,S_L] then
  2056. Begin
  2057. case instr.operands[3].operandtype of
  2058. OPR_REFERENCE:
  2059. {$IfNDef NO_OP3}
  2060. p^.concat(new(pai386,
  2061. op_const_reg_ref(instruc, S_L,
  2062. instr.operands[1].val, instr.operands[2].reg,
  2063. newreference(instr.operands[3].ref))));
  2064. {$else NO_OP3}
  2065. Message(assem_e_unsupported_opcode_and_operand)
  2066. { MISSING !!!! } ;
  2067. {$endif NO_OP3}
  2068. OPR_REGISTER:
  2069. p^.concat(new(pai386,
  2070. op_const_reg_reg(instruc, S_L,
  2071. instr.operands[1].val, instr.operands[2].reg,
  2072. instr.operands[3].reg)));
  2073. else
  2074. Message(assem_e_invalid_opcode_and_operand);
  2075. end;
  2076. end
  2077. else
  2078. Message(assem_e_invalid_opcode_and_operand);
  2079. end;
  2080. end
  2081. else if instr.operands[1].operandtype = OPR_REGISTER then
  2082. {$ifdef NO_OP3}
  2083. Message(assem_e_unsupported_opcode_and_operand)
  2084. {$else NO_OP3}
  2085. begin
  2086. case instr.operands[3].operandtype of
  2087. OPR_REFERENCE:
  2088. p^.concat(new(pai386,
  2089. op_reg_reg_ref(instruc, S_L,
  2090. instr.operands[1].reg, instr.operands[2].reg,
  2091. newreference(instr.operands[3].ref))));
  2092. OPR_REGISTER:
  2093. p^.concat(new(pai386,
  2094. op_reg_reg_reg(instruc, S_L,
  2095. instr.operands[1].reg, instr.operands[2].reg,
  2096. instr.operands[3].reg)));
  2097. else
  2098. Message(assem_e_invalid_opcode_and_operand);
  2099. end;
  2100. end
  2101. {$endif NO_OP3}
  2102. else
  2103. Message(assem_e_invalid_opcode_and_operand);
  2104. { else of case instr.operands[2].size of }
  2105. else
  2106. Message(assem_e_invalid_opcode_and_operand);
  2107. end; { end case }
  2108. end
  2109. else
  2110. if (instruc = A_IMUL) and
  2111. (instr.operands[3].operandtype = OPR_REGISTER) then
  2112. Begin
  2113. case instr.operands[3].size of
  2114. S_W: if instr.operands[1].operandtype = OPR_CONSTANT then
  2115. Begin
  2116. if instr.operands[1].val <= $ffff then
  2117. Begin
  2118. if instr.operands[2].size in [S_NO,S_W] then
  2119. Begin
  2120. case instr.operands[2].operandtype of
  2121. OPR_REFERENCE:
  2122. {$IfNDef NO_OP3}
  2123. p^.concat(new(pai386,
  2124. op_const_ref_reg(instruc, S_W,
  2125. instr.operands[1].val, newreference(instr.operands[2].ref),
  2126. instr.operands[3].reg)));
  2127. {$else NO_OP3}
  2128. Message(assem_e_unsupported_opcode_and_operand)
  2129. { MISSING !!!! } ;
  2130. {$endif NO_OP3}
  2131. OPR_REGISTER: p^.concat(new(pai386,
  2132. op_const_reg_reg(instruc, S_W,
  2133. instr.operands[1].val, instr.operands[2].reg,
  2134. instr.operands[3].reg)));
  2135. else
  2136. Message(assem_e_invalid_opcode_and_operand);
  2137. end; { end case }
  2138. end
  2139. else
  2140. Message(assem_e_invalid_opcode_and_operand);
  2141. end;
  2142. end
  2143. else
  2144. Message(assem_e_invalid_opcode_and_operand);
  2145. S_L: if instr.operands[1].operandtype = OPR_CONSTANT then
  2146. Begin
  2147. if instr.operands[1].val <= $7fffffff then
  2148. Begin
  2149. if instr.operands[2].size in [S_NO,S_L] then
  2150. Begin
  2151. case instr.operands[2].operandtype of
  2152. OPR_REFERENCE:
  2153. {$IfNDef NO_OP3}
  2154. p^.concat(new(pai386,
  2155. op_const_ref_reg(instruc, S_L,
  2156. instr.operands[1].val, newreference(instr.operands[2].ref),
  2157. instr.operands[3].reg)));
  2158. {$else NO_OP3}
  2159. Message(assem_e_unsupported_opcode_and_operand)
  2160. { MISSING !!!! } ;
  2161. {$endif NO_OP3}
  2162. OPR_REGISTER: p^.concat(new(pai386,
  2163. op_const_reg_reg(instruc, S_L,
  2164. instr.operands[1].val, instr.operands[2].reg,
  2165. instr.operands[3].reg)));
  2166. else
  2167. Message(assem_e_invalid_opcode_and_operand);
  2168. end; { end case }
  2169. end
  2170. else
  2171. Message(assem_e_invalid_opcode_and_operand);
  2172. end;
  2173. end
  2174. else
  2175. Message(assem_e_invalid_opcode_and_operand);
  2176. else
  2177. Message(assem_e_invalid_middle_sized_operand);
  2178. end; { end case }
  2179. end { endif }
  2180. else
  2181. Message(assem_e_invalid_three_operand_opcode);
  2182. end;
  2183. (* Old code not yet removed because I did not check exactly that there
  2184. was no difference between ra386int and ra386att code
  2185. The code above is just a copy of the code from ra386int ! (PM)
  2186. 3: Begin
  2187. { only imul, shld and shrd }
  2188. { middle must be a register }
  2189. if (instruc = A_SHLD) Or (instruc = A_SHRD) and
  2190. (instr.operands[2].operandtype = OPR_REGISTER) then
  2191. Begin
  2192. case instr.operands[2].size of
  2193. S_W: if instr.operands[1].operandtype = OPR_CONSTANT then
  2194. Begin
  2195. if instr.operands[1].val <= $ff then
  2196. Begin
  2197. if instr.operands[3].size in [S_W] then
  2198. Begin
  2199. case instr.operands[3].operandtype of
  2200. OPR_REFERENCE:
  2201. Message(assem_e_unsupported_opcode_and_operand)
  2202. { MISSING !!!! } ;
  2203. OPR_REGISTER: p^.concat(new(pai386,
  2204. op_const_reg_reg(instruc, S_W,
  2205. instr.operands[1].val, instr.operands[2].reg,
  2206. instr.operands[3].reg)));
  2207. else
  2208. Message(assem_e_invalid_opcode_and_operand);
  2209. end;
  2210. end
  2211. else
  2212. Message(assem_e_invalid_opcode_and_operand);
  2213. end;
  2214. end
  2215. else
  2216. Message(assem_e_invalid_opcode_and_operand);
  2217. S_L: if instr.operands[1].operandtype = OPR_CONSTANT then
  2218. Begin
  2219. if instr.operands[1].val <= $ff then
  2220. Begin
  2221. if instr.operands[3].size in [S_L] then
  2222. Begin
  2223. case instr.operands[3].operandtype of
  2224. OPR_REFERENCE:
  2225. Message(assem_e_unsupported_opcode_and_operand)
  2226. { MISSING !!!! } ;
  2227. OPR_REGISTER: p^.concat(new(pai386,
  2228. op_const_reg_reg(instruc, S_L,
  2229. instr.operands[1].val, instr.operands[2].reg,
  2230. instr.operands[3].reg)));
  2231. else
  2232. Message(assem_e_invalid_opcode_and_operand);
  2233. end;
  2234. end
  2235. else
  2236. Message(assem_e_invalid_opcode_and_operand);
  2237. end;
  2238. end
  2239. else
  2240. Message(assem_e_invalid_opcode_and_operand);
  2241. else
  2242. Message(assem_e_invalid_opcode_and_operand);
  2243. end; { end case }
  2244. end
  2245. else
  2246. if (instruc = A_IMUL) and (instr.operands[3].operandtype
  2247. = OPR_REGISTER) then
  2248. Begin
  2249. case instr.operands[3].size of
  2250. S_W: if instr.operands[1].operandtype = OPR_CONSTANT then
  2251. Begin
  2252. if instr.operands[1].val <= $ffff then
  2253. Begin
  2254. if instr.operands[2].size in [S_W] then
  2255. Begin
  2256. case instr.operands[2].operandtype of
  2257. OPR_REFERENCE:
  2258. Message(assem_e_unsupported_opcode_and_operand)
  2259. { MISSING !!!! } ;
  2260. OPR_REGISTER: p^.concat(new(pai386,
  2261. op_const_reg_reg(instruc, S_W,
  2262. instr.operands[1].val, instr.operands[2].reg,
  2263. instr.operands[3].reg)));
  2264. else
  2265. Message(assem_e_invalid_opcode_and_operand);
  2266. end; { end case }
  2267. end
  2268. else
  2269. Message(assem_e_invalid_opcode_and_operand);
  2270. end;
  2271. end
  2272. else
  2273. Message(assem_e_invalid_opcode_and_operand);
  2274. S_L: if instr.operands[1].operandtype = OPR_CONSTANT then
  2275. Begin
  2276. if instr.operands[1].val <= $7fffffff then
  2277. Begin
  2278. if instr.operands[2].size in [S_L] then
  2279. Begin
  2280. case instr.operands[2].operandtype of
  2281. OPR_REFERENCE:
  2282. Message(assem_e_unsupported_opcode_and_operand)
  2283. { MISSING !!!! } ;
  2284. OPR_REGISTER: p^.concat(new(pai386,
  2285. op_const_reg_reg(instruc, S_L,
  2286. instr.operands[1].val, instr.operands[2].reg,
  2287. instr.operands[3].reg)));
  2288. else
  2289. Message(assem_e_invalid_opcode_and_operand);
  2290. end; { end case }
  2291. end
  2292. else
  2293. Message(assem_e_invalid_opcode_and_operand);
  2294. end;
  2295. end
  2296. else
  2297. Message(assem_e_invalid_opcode_and_operand);
  2298. else
  2299. Message(assem_e_invalid_middle_sized_operand);
  2300. end; { end case }
  2301. end { endif }
  2302. else
  2303. Message(assem_e_invalid_three_operand_opcode);
  2304. end; *)
  2305. end; { end case }
  2306. end;
  2307. end;
  2308. Procedure ConcatLabeledInstr(var instr: TInstruction);
  2309. Var instruct : tasmop;
  2310. i : longint;
  2311. Begin
  2312. instruct:=instr.getinstruction;
  2313. Case instruct Of
  2314. A_JO,A_JNO,A_JB,A_JC,A_JNAE,
  2315. A_JNB,A_JNC,A_JAE,A_JE,A_JZ,A_JNE,A_JNZ,A_JBE,A_JNA,A_JNBE,
  2316. A_JA,A_JS,A_JNS,A_JP,A_JPE,A_JNP,A_JPO,A_JL,A_JNGE,A_JNL,A_JGE,
  2317. A_JLE,A_JNG,A_JNLE,A_JG,A_JCXZ,A_JECXZ,A_LOOP,A_LOOPZ,A_LOOPE,
  2318. A_LOOPNZ,A_LOOPNE,A_JMP,A_CALL:
  2319. Begin
  2320. if (instr.numops <> 1) then
  2321. Message(assem_e_invalid_labeled_opcode)
  2322. else if instr.operands[1].operandtype <> OPR_LABINSTR then
  2323. Message(assem_e_invalid_labeled_opcode)
  2324. else if assigned(instr.operands[1].hl) then
  2325. p^.concat(new(pai386_labeled,op_lab(instruct, instr.operands[1].hl)))
  2326. else
  2327. Begin
  2328. Message(assem_f_internal_error_in_concatlabeledinstr);
  2329. end;
  2330. end;
  2331. A_MOV: { move from/to rel8 };
  2332. else
  2333. if (cs_compilesystem in aktmoduleswitches) then
  2334. begin
  2335. for i:=1 to instr.numops do
  2336. if instr.operands[i].operandtype=OPR_LABINSTR then
  2337. begin
  2338. instr.operands[i].operandtype:=OPR_REFERENCE;
  2339. instr.operands[i].ref.symbol:=newpasstr(lab2str(instr.operands[i].hl) );
  2340. instr.operands[i].opinfo:=ao_mem;
  2341. instr.operands[i].ref.base:=R_NO;
  2342. instr.operands[i].ref.index:=R_NO;
  2343. instr.operands[i].ref.segment:=R_DEFAULT_SEG;
  2344. instr.operands[i].ref.offset:=0;
  2345. end;
  2346. { handle now as an ordinary opcode }
  2347. concatopcode(instr);
  2348. end
  2349. else
  2350. Message1(assem_e_invalid_operand,'');
  2351. end; { case }
  2352. end;
  2353. {---------------------------------------------------------------------}
  2354. { Routines for the parsing }
  2355. {---------------------------------------------------------------------}
  2356. procedure consume(t : tinteltoken);
  2357. begin
  2358. if t<>actasmtoken then
  2359. Message(assem_e_syntax_error);
  2360. actasmtoken:=gettoken;
  2361. { set the previous id flag }
  2362. previous_was_id:=(actasmtoken=AS_ID);
  2363. { if the token must be ignored, then }
  2364. { get another token to parse. }
  2365. if actasmtoken = AS_NONE then
  2366. actasmtoken := gettoken;
  2367. end;
  2368. function findregister(const s : string): tregister;
  2369. {*********************************************************************}
  2370. { FUNCTION findregister(s: string):tasmop; }
  2371. { Description: Determines if the s string is a valid register, }
  2372. { if so returns correct tregister token, or R_NO if not found. }
  2373. {*********************************************************************}
  2374. var
  2375. i: tregister;
  2376. begin
  2377. findregister := R_NO;
  2378. for i:=firstreg to lastreg do
  2379. if s = iasmregs[i] then
  2380. Begin
  2381. findregister := i;
  2382. exit;
  2383. end;
  2384. end;
  2385. function findprefix(const s: string; var token: tasmop): boolean;
  2386. var i: byte;
  2387. Begin
  2388. findprefix := FALSE;
  2389. for i:=0 to _count_asmprefixes do
  2390. Begin
  2391. if s = _asmprefixes[i] then
  2392. begin
  2393. token := _prefixtokens[i];
  2394. findprefix := TRUE;
  2395. exit;
  2396. end;
  2397. end;
  2398. end;
  2399. function findsegment(const s:string): tregister;
  2400. {*********************************************************************}
  2401. { FUNCTION findsegment(s: string):tasmop; }
  2402. { Description: Determines if the s string is a valid segment register}
  2403. { if so returns correct tregister token, or R_NO if not found. }
  2404. {*********************************************************************}
  2405. var
  2406. i: tregister;
  2407. Begin
  2408. findsegment := R_DEFAULT_SEG;
  2409. for i:=firstsreg to lastsreg do
  2410. if s = iasmregs[i] then
  2411. Begin
  2412. findsegment := i;
  2413. exit;
  2414. end;
  2415. end;
  2416. function findopcode(const s: string): tasmop;
  2417. {*********************************************************************}
  2418. { FUNCTION findopcode(s: string): tasmop; }
  2419. { Description: Determines if the s string is a valid opcode }
  2420. { if so returns correct tasmop token. }
  2421. {*********************************************************************}
  2422. var
  2423. i: tasmop;
  2424. j: byte;
  2425. hs: topsize;
  2426. hid: string;
  2427. Begin
  2428. findopcode := A_NONE;
  2429. { first search for extended opcodes }
  2430. { now, in this case, we must use the suffix }
  2431. { to determine the size of the instruction }
  2432. for j:=0 to _count_asmspecialops do
  2433. Begin
  2434. if s = _specialops[j] then
  2435. Begin
  2436. findopcode := _specialopstokens[j];
  2437. { set the size }
  2438. case s[length(s)] of
  2439. 'B': instr.stropsize := S_B;
  2440. 'L': instr.stropsize := S_L;
  2441. 'W': instr.stropsize := S_W;
  2442. end;
  2443. exit;
  2444. end;
  2445. end;
  2446. for i:=firstop to lastop do
  2447. Begin
  2448. if s=iasmops^[i] then
  2449. begin
  2450. findopcode := i;
  2451. instr.stropsize := S_NO;
  2452. exit;
  2453. end;
  2454. end;
  2455. { not found yet ... }
  2456. { search for all possible suffixes }
  2457. for hs:=S_WL downto S_B do
  2458. if copy(s,length(s)-length(att_opsize2str[hs])+1,
  2459. length(att_opsize2str[hs]))=upper(att_opsize2str[hs]) then
  2460. begin
  2461. hid:=copy(s,1,length(s)-length(att_opsize2str[hs]));
  2462. for i:=firstop to lastop do
  2463. if (length(hid) > 0) and (hid=iasmops^[i]) then
  2464. begin
  2465. findopcode := i;
  2466. instr.stropsize := hs;
  2467. exit;
  2468. end;
  2469. end;
  2470. end;
  2471. Function CheckPrefix(prefix: tasmop; opcode:tasmop): Boolean;
  2472. { Checks if the prefix is valid with the following instruction }
  2473. { return false if not, otherwise true }
  2474. Begin
  2475. CheckPrefix := TRUE;
  2476. Case prefix of
  2477. A_REP,A_REPNE,A_REPE:
  2478. Case opcode Of
  2479. A_SCAS,A_INS,A_OUTS,A_MOVS,
  2480. A_CMPS,A_LODS,A_STOS:;
  2481. Else
  2482. Begin
  2483. CheckPrefix := FALSE;
  2484. exit;
  2485. end;
  2486. End;
  2487. A_LOCK:
  2488. Case opcode Of
  2489. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,
  2490. A_ADC,A_SBB,A_AND,A_SUB,A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  2491. Else
  2492. Begin
  2493. CheckPrefix := FALSE;
  2494. Exit;
  2495. end;
  2496. End;
  2497. A_NONE: exit; { no prefix here }
  2498. else
  2499. CheckPrefix := FALSE;
  2500. end; { end case }
  2501. end;
  2502. Procedure InitAsmRef(var instr: TInstruction);
  2503. {*********************************************************************}
  2504. { Description: This routine first check if the instruction is of }
  2505. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  2506. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  2507. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  2508. { to point to the default segment. }
  2509. {*********************************************************************}
  2510. Begin
  2511. With instr do
  2512. Begin
  2513. case operands[operandnum].operandtype of
  2514. OPR_REFERENCE: exit;
  2515. OPR_NONE: ;
  2516. else
  2517. Message(assem_e_invalid_operand_type);
  2518. end;
  2519. operands[operandnum].operandtype := OPR_REFERENCE;
  2520. operands[operandnum].ref.segment := R_DEFAULT_SEG;
  2521. end;
  2522. end;
  2523. Function CheckOverride(segreg: tregister; var instr: TInstruction): Boolean;
  2524. { Check if the override is valid, and if so then }
  2525. { update the instr variable accordingly. }
  2526. Begin
  2527. CheckOverride := FALSE;
  2528. Case instr.getinstruction of
  2529. A_MOVS,A_XLAT,A_CMPS:
  2530. Begin
  2531. CheckOverride := TRUE;
  2532. Message(assem_e_segment_override_not_supported);
  2533. end
  2534. end
  2535. end;
  2536. Procedure GetRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  2537. {*********************************************************************}
  2538. { PROCEDURE GetRecordOffsetSize }
  2539. { Description: This routine builds up a record offset after a AS_DOT }
  2540. { token is encountered. }
  2541. { On entry actasmtoken should be equal to AS_DOT }
  2542. {*********************************************************************}
  2543. { EXIT CONDITION: On exit the routine should point to either the }
  2544. { ERROR RECOVER: read until AS_COMMA or AS_SEPARATOR token. }
  2545. { Warning: This is called recursively. }
  2546. {*********************************************************************}
  2547. var
  2548. toffset,tsize : longint;
  2549. Begin
  2550. offset:=0;
  2551. size:=0;
  2552. Consume(AS_DOT);
  2553. if actasmtoken = AS_ID then
  2554. Begin
  2555. if not GetTypeOffsetSize(expr,actasmpattern,toffset,tsize) and
  2556. not GetVarOffsetSize(expr,actasmpattern,toffset,tsize) then
  2557. begin
  2558. Message(assem_e_syntax_error);
  2559. toffset:=0;
  2560. tsize:=0;
  2561. end;
  2562. inc(offset,toffset);
  2563. size:=tsize;
  2564. Consume(AS_ID);
  2565. if actasmtoken=AS_DOT then
  2566. begin
  2567. GetRecordOffsetSize(expr,toffset,tsize);
  2568. inc(offset,toffset);
  2569. size:=tsize;
  2570. end;
  2571. end
  2572. else
  2573. Begin
  2574. Message(assem_e_syntax_error);
  2575. repeat
  2576. consume(actasmtoken)
  2577. until (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA);
  2578. end;
  2579. end;
  2580. Function BuildConstExpression(allowref,betweenbracket:boolean): longint;
  2581. {*********************************************************************}
  2582. { FUNCTION BuildConstExpression: longint }
  2583. { Description: This routine calculates a constant expression to }
  2584. { a given value. The return value is the value calculated from }
  2585. { the expression. }
  2586. { The following tokens (not strings) are recognized: }
  2587. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  2588. {*********************************************************************}
  2589. { ENTRY: On entry the token should be any valid expression token. }
  2590. { EXIT: On Exit the token points to any token after the closing }
  2591. { RBRACKET }
  2592. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  2593. { invalid tokens. }
  2594. {*********************************************************************}
  2595. var
  2596. tempstr,expr : string;
  2597. parenlevel,l,k : longint;
  2598. errorflag : boolean;
  2599. prevtoken : tinteltoken;
  2600. Begin
  2601. errorflag := FALSE;
  2602. tempstr := '';
  2603. expr := '';
  2604. inexpression := TRUE;
  2605. prevtoken := AS_NONE;
  2606. parenlevel := 0;
  2607. Repeat
  2608. if actasmtoken<>AS_LPAREN then
  2609. prevtoken:=actasmtoken;
  2610. Case actasmtoken of
  2611. AS_LPAREN:
  2612. Begin
  2613. { Exit if ref? }
  2614. if allowref and (prevtoken in [AS_INTNUM,AS_BINNUM,AS_OCTALNUM,AS_ID,AS_HEXNUM]) then
  2615. break;
  2616. Consume(AS_LPAREN);
  2617. expr := expr + '(';
  2618. inc(parenlevel);
  2619. end;
  2620. AS_RPAREN:
  2621. Begin
  2622. { end of ref ? }
  2623. if (parenlevel=0) and betweenbracket then
  2624. break;
  2625. Consume(AS_RPAREN);
  2626. expr := expr + ')';
  2627. dec(parenlevel);
  2628. end;
  2629. AS_SHL:
  2630. Begin
  2631. Consume(AS_SHL);
  2632. expr := expr + '<';
  2633. end;
  2634. AS_SHR:
  2635. Begin
  2636. Consume(AS_SHR);
  2637. expr := expr + '>';
  2638. end;
  2639. AS_SLASH:
  2640. Begin
  2641. Consume(AS_SLASH);
  2642. expr := expr + '/';
  2643. end;
  2644. AS_MOD:
  2645. Begin
  2646. Consume(AS_MOD);
  2647. expr := expr + '%';
  2648. end;
  2649. AS_STAR:
  2650. Begin
  2651. Consume(AS_STAR);
  2652. expr := expr + '*';
  2653. end;
  2654. AS_PLUS:
  2655. Begin
  2656. Consume(AS_PLUS);
  2657. expr := expr + '+';
  2658. end;
  2659. AS_MINUS:
  2660. Begin
  2661. Consume(AS_MINUS);
  2662. expr := expr + '-';
  2663. end;
  2664. AS_AND:
  2665. Begin
  2666. Consume(AS_AND);
  2667. expr := expr + '&';
  2668. end;
  2669. AS_NOT:
  2670. Begin
  2671. Consume(AS_NOT);
  2672. expr := expr + '~';
  2673. end;
  2674. AS_XOR:
  2675. Begin
  2676. Consume(AS_XOR);
  2677. expr := expr + '^';
  2678. end;
  2679. AS_OR:
  2680. Begin
  2681. Consume(AS_OR);
  2682. expr := expr + '|';
  2683. end;
  2684. AS_INTNUM:
  2685. Begin
  2686. expr := expr + actasmpattern;
  2687. Consume(AS_INTNUM);
  2688. end;
  2689. AS_BINNUM:
  2690. Begin
  2691. expr:=expr+BinaryToDec(actasmpattern);
  2692. Consume(AS_BINNUM);
  2693. end;
  2694. AS_HEXNUM:
  2695. Begin
  2696. expr:=expr+HexToDec(actasmpattern);
  2697. Consume(AS_HEXNUM);
  2698. end;
  2699. AS_OCTALNUM:
  2700. Begin
  2701. expr:=expr+OctalToDec(actasmpattern);
  2702. Consume(AS_OCTALNUM);
  2703. end;
  2704. AS_ID:
  2705. Begin
  2706. tempstr:=actasmpattern;
  2707. consume(AS_ID);
  2708. if actasmtoken=AS_DOT then
  2709. begin
  2710. GetRecordOffsetSize(tempstr,l,k);
  2711. str(l, tempstr);
  2712. expr := expr + tempstr;
  2713. end
  2714. else
  2715. begin
  2716. if SearchIConstant(tempstr,l) then
  2717. begin
  2718. str(l, tempstr);
  2719. expr := expr + tempstr;
  2720. end
  2721. else
  2722. Message1(assem_e_invalid_const_symbol,tempstr);
  2723. end;
  2724. end;
  2725. AS_SEPARATOR,
  2726. AS_COMMA:
  2727. Begin
  2728. break;
  2729. end;
  2730. else
  2731. Begin
  2732. { write error only once. }
  2733. if not errorflag then
  2734. Message(assem_e_invalid_constant_expression);
  2735. BuildConstExpression := 0;
  2736. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then
  2737. exit;
  2738. { consume tokens until we find COMMA or SEPARATOR }
  2739. Consume(actasmtoken);
  2740. errorflag := TRUE;
  2741. end;
  2742. end;
  2743. Until false;
  2744. { calculate expression }
  2745. if not ErrorFlag then
  2746. BuildConstExpression := CalculateExpression(expr)
  2747. else
  2748. BuildConstExpression := 0;
  2749. { no longer in an expression }
  2750. inexpression := FALSE;
  2751. end;
  2752. Procedure BuildRealConstant(typ : tfloattype);
  2753. {*********************************************************************}
  2754. { PROCEDURE BuilRealConst }
  2755. { Description: This routine calculates a constant expression to }
  2756. { a given value. The return value is the value calculated from }
  2757. { the expression. }
  2758. { The following tokens (not strings) are recognized: }
  2759. { +/-,numbers and real numbers }
  2760. {*********************************************************************}
  2761. { ENTRY: On entry the token should be any valid expression token. }
  2762. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  2763. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  2764. { invalid tokens. }
  2765. {*********************************************************************}
  2766. var expr: string;
  2767. tempstr: string;
  2768. r : bestreal;
  2769. code : word;
  2770. negativ : boolean;
  2771. errorflag: boolean;
  2772. Begin
  2773. errorflag := FALSE;
  2774. Repeat
  2775. negativ:=false;
  2776. expr := '';
  2777. tempstr := '';
  2778. if actasmtoken=AS_PLUS then Consume(AS_PLUS)
  2779. else if actasmtoken=AS_MINUS then
  2780. begin
  2781. negativ:=true;
  2782. consume(AS_MINUS);
  2783. end;
  2784. Case actasmtoken of
  2785. AS_INTNUM: Begin
  2786. expr := actasmpattern;
  2787. Consume(AS_INTNUM);
  2788. end;
  2789. AS_REALNUM: Begin
  2790. expr := actasmpattern;
  2791. { in ATT syntax you have 0d in front of the real }
  2792. { should this be forced ? yes i think so, as to }
  2793. { conform to gas as much as possible. }
  2794. if (expr[1]='0') and (upper(expr[2])='D') then
  2795. expr:=copy(expr,3,255);
  2796. Consume(AS_REALNUM);
  2797. end;
  2798. AS_BINNUM: Begin
  2799. { checking for real constants with this should use }
  2800. { real DECODING otherwise the compiler will crash! }
  2801. Message(assem_w_float_bin_ignored);
  2802. Consume(AS_BINNUM);
  2803. end;
  2804. AS_HEXNUM: Begin
  2805. { checking for real constants with this should use }
  2806. { real DECODING otherwise the compiler will crash! }
  2807. Message(assem_w_float_hex_ignored);
  2808. Consume(AS_HEXNUM);
  2809. end;
  2810. AS_OCTALNUM: Begin
  2811. { checking for real constants with this should use }
  2812. { real DECODING otherwise the compiler will crash! }
  2813. { xxxToDec using reals could be a solution, but the }
  2814. { problem is that these will crash the m68k compiler }
  2815. { when compiling -- because of lack of good fpu }
  2816. { support. }
  2817. Message(assem_w_float_octal_ignored);
  2818. Consume(AS_OCTALNUM);
  2819. end;
  2820. else
  2821. Begin
  2822. { only write error once. }
  2823. if not errorflag then
  2824. Message(assem_e_invalid_real_const);
  2825. { consume tokens until we find COMMA or SEPARATOR }
  2826. Consume(actasmtoken);
  2827. errorflag := TRUE;
  2828. End;
  2829. end;
  2830. { go to next term }
  2831. if (actasmtoken=AS_COMMA) or (actasmtoken=AS_SEPARATOR) then
  2832. Begin
  2833. if negativ then expr:='-'+expr;
  2834. val(expr,r,code);
  2835. if code<>0 then
  2836. Begin
  2837. r:=0;
  2838. Message(assem_e_invalid_real_const);
  2839. ConcatRealConstant(p,r,typ);
  2840. End
  2841. else
  2842. Begin
  2843. ConcatRealConstant(p,r,typ);
  2844. End;
  2845. end
  2846. else
  2847. Message(assem_e_invalid_real_const);
  2848. Until actasmtoken=AS_SEPARATOR;
  2849. end;
  2850. Procedure BuildReference(var Instr: TInstruction);
  2851. {*********************************************************************}
  2852. { PROCEDURE BuildReference }
  2853. { Description: This routine builds up an expression after a LPAREN }
  2854. { token is encountered. }
  2855. { On entry actasmtoken should be equal to AS_LPAREN }
  2856. {*********************************************************************}
  2857. { EXIT CONDITION: On exit the routine should point to either the }
  2858. { AS_COMMA or AS_SEPARATOR token. }
  2859. {*********************************************************************}
  2860. procedure Consume_RParen;
  2861. begin
  2862. if actasmtoken <> AS_RPAREN then
  2863. Begin
  2864. Message(assem_e_invalid_reference);
  2865. while actasmtoken <> AS_SEPARATOR do
  2866. Consume(actasmtoken);
  2867. end
  2868. else
  2869. begin
  2870. Consume(AS_RPAREN);
  2871. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  2872. Begin
  2873. Message(assem_e_invalid_reference);
  2874. { error recovery ... }
  2875. while actasmtoken <> AS_SEPARATOR do
  2876. Consume(actasmtoken);
  2877. end;
  2878. end;
  2879. end;
  2880. var
  2881. l:longint;
  2882. Begin
  2883. Consume(AS_LPAREN);
  2884. initAsmRef(instr);
  2885. Case actasmtoken of
  2886. AS_HEXNUM,
  2887. AS_INTNUM,
  2888. AS_BINNUM,
  2889. AS_OCTALNUM,
  2890. AS_MINUS,
  2891. AS_PLUS: { absolute offset, such as fs:(0x046c) }
  2892. Begin
  2893. { offset(offset) is invalid }
  2894. If Instr.Operands[OperandNum].Ref.Offset <> 0 Then
  2895. Begin
  2896. Message(assem_e_invalid_reference);
  2897. while actasmtoken <> AS_SEPARATOR do
  2898. Consume(actasmtoken);
  2899. End
  2900. Else
  2901. Begin
  2902. Instr.Operands[OperandNum].Ref.Offset := BuildConstExpression(false,true);
  2903. Consume_RParen;
  2904. end;
  2905. exit;
  2906. End;
  2907. AS_REGISTER: { (reg ... }
  2908. Begin
  2909. { Check if there is already a base (mostly ebp,esp) than this is
  2910. not allowed,becuase it will give crashing code }
  2911. if instr.operands[operandnum].ref.base<>R_NO then
  2912. Message(assem_e_cannot_index_relative_var);
  2913. instr.operands[operandnum].ref.base := findregister(actasmpattern);
  2914. Consume(AS_REGISTER);
  2915. { can either be a register or a right parenthesis }
  2916. { (reg) }
  2917. if actasmtoken=AS_RPAREN then
  2918. Begin
  2919. Consume_RParen;
  2920. exit;
  2921. end;
  2922. { (reg,reg .. }
  2923. Consume(AS_COMMA);
  2924. if actasmtoken=AS_REGISTER then
  2925. Begin
  2926. instr.operands[operandnum].ref.index := findregister(actasmpattern);
  2927. Consume(AS_REGISTER);
  2928. { check for scaling ... }
  2929. case actasmtoken of
  2930. AS_RPAREN:
  2931. Begin
  2932. Consume_RParen;
  2933. exit;
  2934. end;
  2935. AS_COMMA:
  2936. Begin
  2937. Consume(AS_COMMA);
  2938. { we have to process the scaling }
  2939. l:=BuildConstExpression(false,true);
  2940. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) then
  2941. instr.operands[operandnum].ref.scalefactor := l
  2942. else
  2943. Begin
  2944. Message(assem_e_invalid_scaling_value);
  2945. instr.operands[operandnum].ref.scalefactor := 0;
  2946. end;
  2947. Consume_RParen;
  2948. end;
  2949. else
  2950. Begin
  2951. Message(assem_e_invalid_reference_syntax);
  2952. while (actasmtoken <> AS_SEPARATOR) do
  2953. Consume(actasmtoken);
  2954. end;
  2955. end; { end case }
  2956. end
  2957. else
  2958. Begin
  2959. Message(assem_e_invalid_reference_syntax);
  2960. while (actasmtoken <> AS_SEPARATOR) do
  2961. Consume(actasmtoken);
  2962. end;
  2963. end; {end case }
  2964. AS_COMMA: { (, ... can either be scaling, or index }
  2965. Begin
  2966. Consume(AS_COMMA);
  2967. { Index }
  2968. if (actasmtoken=AS_REGISTER) then
  2969. Begin
  2970. instr.operands[operandnum].ref.index := findregister(actasmpattern);
  2971. Consume(AS_REGISTER);
  2972. { check for scaling ... }
  2973. case actasmtoken of
  2974. AS_RPAREN:
  2975. Begin
  2976. Consume_RParen;
  2977. exit;
  2978. end;
  2979. AS_COMMA:
  2980. Begin
  2981. Consume(AS_COMMA);
  2982. { we have to process the scaling }
  2983. l:=BuildConstExpression(false,true);
  2984. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) then
  2985. instr.operands[operandnum].ref.scalefactor := l
  2986. else
  2987. Begin
  2988. Message(assem_e_invalid_scaling_value);
  2989. instr.operands[operandnum].ref.scalefactor := 0;
  2990. end;
  2991. Consume_RParen;
  2992. end;
  2993. else
  2994. Begin
  2995. Message(assem_e_invalid_reference_syntax);
  2996. while (actasmtoken <> AS_SEPARATOR) do
  2997. Consume(actasmtoken);
  2998. end;
  2999. end; {end case }
  3000. end
  3001. { Scaling }
  3002. else
  3003. Begin
  3004. l:=BuildConstExpression(false,true);
  3005. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) then
  3006. instr.operands[operandnum].ref.scalefactor := l
  3007. else
  3008. Begin
  3009. Message(assem_e_invalid_scaling_value);
  3010. instr.operands[operandnum].ref.scalefactor := 0;
  3011. end;
  3012. Consume_RParen;
  3013. exit;
  3014. end;
  3015. end;
  3016. else
  3017. Begin
  3018. Message(assem_e_invalid_reference_syntax);
  3019. while (actasmtoken <> AS_SEPARATOR) do
  3020. Consume(actasmtoken);
  3021. end;
  3022. end; { end case }
  3023. end;
  3024. Procedure BuildOperand(var instr: TInstruction);
  3025. {*********************************************************************}
  3026. { EXIT CONDITION: On exit the routine should point to either the }
  3027. { AS_COMMA or AS_SEPARATOR token. }
  3028. {*********************************************************************}
  3029. var
  3030. tempstr: string;
  3031. expr: string;
  3032. lab: Pasmlabel;
  3033. hl: plabel;
  3034. tsize,l,
  3035. toffset : longint;
  3036. Begin
  3037. tempstr := '';
  3038. expr := '';
  3039. case actasmtoken of
  3040. AS_LPAREN: { Memory reference or constant expression }
  3041. Begin
  3042. initAsmRef(instr);
  3043. BuildReference(instr);
  3044. end;
  3045. AS_DOLLAR: { Constant expression }
  3046. Begin
  3047. Consume(AS_DOLLAR);
  3048. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  3049. Message(assem_e_invalid_operand_type);
  3050. { identifiers are handled by BuildExpression }
  3051. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  3052. instr.operands[operandnum].val :=BuildConstExpression(False,False);
  3053. end;
  3054. AS_HEXNUM,AS_INTNUM,AS_MINUS,
  3055. AS_BINNUM,AS_OCTALNUM,AS_PLUS:
  3056. Begin
  3057. { Constant memory offset }
  3058. { This must absolutely be followed by ( }
  3059. InitAsmRef(instr);
  3060. instr.operands[operandnum].ref.offset:=BuildConstExpression(True,False);
  3061. if actasmtoken<>AS_LPAREN then
  3062. Message(assem_e_invalid_reference_syntax)
  3063. else
  3064. BuildReference(instr);
  3065. end;
  3066. AS_STAR: { Call from memory address }
  3067. Begin
  3068. Consume(AS_STAR);
  3069. InitAsmRef(instr);
  3070. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  3071. Message(assem_e_syn_opcode_operand);
  3072. end;
  3073. AS_ID: { A constant expression, or a Variable ref. }
  3074. Begin
  3075. { Local label ? }
  3076. if (actasmpattern[1] ='.') and (actasmpattern[2] = 'L') then
  3077. Begin
  3078. delete(actasmpattern,1,2);
  3079. if actasmpattern = '' then
  3080. Message(assem_e_null_label_ref_not_allowed);
  3081. lab := labellist.search(actasmpattern);
  3082. { check if the label is already defined }
  3083. { if so, we then check if the plabel is }
  3084. { non-nil, if so we add it to instruction }
  3085. if assigned(lab) then
  3086. Begin
  3087. if assigned(lab^.lab) then
  3088. Begin
  3089. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  3090. instr.operands[operandnum].hl := lab^.lab;
  3091. instr.labeled := TRUE;
  3092. end;
  3093. end
  3094. else
  3095. Begin
  3096. { the label does not exist, create it }
  3097. { emit the opcode, but set that the }
  3098. { label has not been emitted }
  3099. getlabel(hl);
  3100. labellist.insert(actasmpattern,hl,FALSE);
  3101. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  3102. instr.operands[operandnum].hl := hl;
  3103. instr.labeled := TRUE;
  3104. end;
  3105. Consume(AS_ID);
  3106. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  3107. Begin
  3108. Message(assem_e_syntax_error);
  3109. end;
  3110. end
  3111. else
  3112. { probably a variable or normal expression }
  3113. { or a procedure (such as in CALL ID) }
  3114. Begin
  3115. { check if this is a label, if so then }
  3116. { emit it as a label. }
  3117. if SearchLabel(actasmpattern,hl) then
  3118. Begin
  3119. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  3120. instr.operands[operandnum].hl := hl;
  3121. instr.labeled := TRUE;
  3122. Consume(AS_ID);
  3123. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  3124. Message(assem_e_syntax_error);
  3125. end
  3126. else
  3127. { is it a normal variable ? }
  3128. Begin
  3129. { context for scanner }
  3130. initAsmRef(instr);
  3131. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  3132. Begin
  3133. { look for special symbols ... }
  3134. if actasmpattern = '__RESULT' then
  3135. SetUpResult(instr,operandnum)
  3136. else
  3137. if actasmpattern = '__SELF' then
  3138. Begin
  3139. if assigned(procinfo._class) then
  3140. Begin
  3141. instr.operands[operandnum].operandtype := OPR_REFERENCE;
  3142. instr.operands[operandnum].ref.offset := procinfo.ESI_offset;
  3143. instr.operands[operandnum].ref.base := procinfo.framepointer;
  3144. end
  3145. else
  3146. Message(assem_e_cannot_use___SELF_outside_methode);
  3147. end
  3148. else
  3149. if actasmpattern = '__OLDEBP' then
  3150. Begin
  3151. if lexlevel>normal_function_level then
  3152. Begin
  3153. instr.operands[operandnum].operandtype := OPR_REFERENCE;
  3154. instr.operands[operandnum].ref.offset := procinfo.framepointer_offset;
  3155. instr.operands[operandnum].ref.base := procinfo.framepointer;
  3156. end
  3157. else
  3158. Message(assem_e_cannot_use___OLDEBP_outside_nested_procedure);
  3159. end
  3160. else
  3161. { check for direct symbolic names }
  3162. { only if compiling the system unit }
  3163. if (cs_compilesystem in aktmoduleswitches) then
  3164. begin
  3165. if not SearchDirectVar(instr,actasmpattern,operandnum) then
  3166. Begin
  3167. { not found, finally ... add it anyways ... }
  3168. Message1(assem_w_id_supposed_external,actasmpattern);
  3169. instr.operands[operandnum].ref.symbol := newpasstr(actasmpattern);
  3170. end;
  3171. end
  3172. else
  3173. Message1(assem_e_unknown_id,actasmpattern);
  3174. end;
  3175. { constant expression? }
  3176. if (instr.operands[operandnum].operandtype=OPR_CONSTANT) then
  3177. begin
  3178. l:=BuildConstExpression(true,false);
  3179. { indexing? }
  3180. if actasmtoken=AS_LPAREN then
  3181. begin
  3182. instr.operands[operandnum].operandtype:=OPR_REFERENCE;
  3183. reset_reference(Instr.Operands[OperandNum].Ref);
  3184. Instr.Operands[OperandNum].Ref.Offset:=l;
  3185. BuildReference(instr);
  3186. end
  3187. else
  3188. Instr.Operands[OperandNum].Val:=l;
  3189. end
  3190. else
  3191. begin
  3192. expr := actasmpattern;
  3193. Consume(AS_ID);
  3194. if actasmtoken=AS_DOT then
  3195. begin
  3196. GetRecordOffsetSize(expr,toffset,tsize);
  3197. inc(instr.operands[operandnum].ref.offset,toffset);
  3198. SetOperandSize(instr,operandnum,tsize);
  3199. end;
  3200. if actasmtoken=AS_LPAREN then
  3201. BuildReference(instr);
  3202. end;
  3203. end; { end if }
  3204. end; { end if }
  3205. end;
  3206. AS_REGISTER: { Register, a variable reference or a constant reference }
  3207. Begin
  3208. { save the type of register used. }
  3209. tempstr := actasmpattern;
  3210. Consume(AS_REGISTER);
  3211. if actasmtoken = AS_COLON then
  3212. Begin
  3213. Consume(AS_COLON);
  3214. initAsmRef(instr);
  3215. instr.operands[operandnum].ref.segment := findsegment(tempstr);
  3216. { here we can have either an identifier }
  3217. { or a constant, where either can be }
  3218. { followed by a parenthesis... }
  3219. { Constant memory offset . }
  3220. { This must absolutely be followed by ( }
  3221. case actasmtoken of
  3222. AS_HEXNUM,AS_INTNUM,AS_MINUS,
  3223. AS_BINNUM,AS_OCTALNUM,AS_PLUS:
  3224. Begin
  3225. instr.operands[operandnum].ref.offset:=BuildConstExpression(True,False);
  3226. if actasmtoken<>AS_LPAREN then
  3227. Message(assem_e_invalid_reference_syntax)
  3228. else
  3229. BuildReference(instr);
  3230. end;
  3231. AS_LPAREN:
  3232. BuildReference(instr);
  3233. AS_ID: { only a variable is allowed ... }
  3234. Begin
  3235. { is it a normal variable ? }
  3236. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  3237. begin
  3238. { check for direct symbolic names }
  3239. { only if compiling the system unit }
  3240. if (cs_compilesystem in aktmoduleswitches) then
  3241. begin
  3242. if not SearchDirectVar(instr,actasmpattern,operandnum) then
  3243. Message(assem_e_invalid_seg_override);
  3244. end
  3245. else
  3246. Message(assem_e_invalid_seg_override);
  3247. end;
  3248. Consume(actasmtoken);
  3249. case actasmtoken of
  3250. AS_SEPARATOR,
  3251. AS_COMMA: ;
  3252. AS_LPAREN: BuildReference(instr);
  3253. else
  3254. Begin
  3255. Message(assem_e_invalid_seg_override);
  3256. Consume(actasmtoken);
  3257. end;
  3258. end; {end case }
  3259. end;
  3260. else
  3261. Begin
  3262. Message(assem_e_invalid_seg_override);
  3263. Consume(actasmtoken);
  3264. end;
  3265. end; { end case }
  3266. end
  3267. { Simple register }
  3268. else if (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  3269. Begin
  3270. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_REGISTER]) then
  3271. Message(assem_e_invalid_operand_type);
  3272. instr.operands[operandnum].operandtype := OPR_REGISTER;
  3273. instr.operands[operandnum].reg := findregister(tempstr);
  3274. end
  3275. else
  3276. Message1(assem_e_syn_register,tempstr);
  3277. end;
  3278. AS_SEPARATOR,
  3279. AS_COMMA: ;
  3280. else
  3281. Begin
  3282. Message(assem_e_syn_opcode_operand);
  3283. Consume(actasmtoken);
  3284. end;
  3285. end; { end case }
  3286. end;
  3287. Procedure BuildConstant(maxvalue: longint);
  3288. {*********************************************************************}
  3289. { PROCEDURE BuildConstant }
  3290. { Description: This routine takes care of parsing a DB,DD,or DW }
  3291. { line and adding those to the assembler node. Expressions, range- }
  3292. { checking are fullly taken care of. }
  3293. { maxvalue: $ff -> indicates that this is a DB node. }
  3294. { $ffff -> indicates that this is a DW node. }
  3295. { $ffffffff -> indicates that this is a DD node. }
  3296. {*********************************************************************}
  3297. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  3298. {*********************************************************************}
  3299. var
  3300. strlength: byte;
  3301. expr: string;
  3302. value : longint;
  3303. Begin
  3304. Repeat
  3305. Case actasmtoken of
  3306. AS_STRING:
  3307. Begin
  3308. if maxvalue = $ff then
  3309. strlength := 1
  3310. else
  3311. Message(assem_e_string_not_allowed_as_const);
  3312. expr := actasmpattern;
  3313. if length(expr) > 1 then
  3314. Message(assem_e_string_not_allowed_as_const);
  3315. Consume(AS_STRING);
  3316. Case actasmtoken of
  3317. AS_COMMA: Consume(AS_COMMA);
  3318. AS_SEPARATOR: ;
  3319. else
  3320. Message(assem_e_invalid_string_expression);
  3321. end; { end case }
  3322. ConcatString(p,expr);
  3323. end;
  3324. AS_INTNUM,
  3325. AS_BINNUM,
  3326. AS_OCTALNUM,
  3327. AS_HEXNUM,
  3328. AS_PLUS,
  3329. AS_MINUS,
  3330. AS_LPAREN,
  3331. AS_NOT:
  3332. Begin
  3333. value:=BuildConstExpression(false,false);
  3334. ConcatConstant(p,value,maxvalue);
  3335. end;
  3336. AS_COMMA:
  3337. Begin
  3338. Consume(AS_COMMA);
  3339. end;
  3340. AS_SEPARATOR:
  3341. begin
  3342. break;
  3343. end;
  3344. else
  3345. Begin
  3346. Message(assem_f_internal_error_in_buildconstant);
  3347. end;
  3348. end; { end case }
  3349. Until false;
  3350. end;
  3351. Procedure BuildStringConstant(asciiz: boolean);
  3352. {*********************************************************************}
  3353. { PROCEDURE BuildStringConstant }
  3354. { Description: Takes care of a ASCII, or ASCIIZ directive. }
  3355. { asciiz: boolean -> if true then string will be null terminated. }
  3356. {*********************************************************************}
  3357. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  3358. { On ENTRY: Token should point to AS_STRING }
  3359. {*********************************************************************}
  3360. var
  3361. expr: string;
  3362. errorflag : boolean;
  3363. Begin
  3364. errorflag := FALSE;
  3365. Repeat
  3366. Case actasmtoken of
  3367. AS_STRING:
  3368. Begin
  3369. expr:=actasmpattern;
  3370. if asciiz then
  3371. expr:=expr+#0;
  3372. ConcatPasString(p,expr);
  3373. Consume(AS_STRING);
  3374. end;
  3375. AS_COMMA:
  3376. begin
  3377. Consume(AS_COMMA);
  3378. end;
  3379. AS_SEPARATOR:
  3380. begin
  3381. break;
  3382. end;
  3383. else
  3384. Begin
  3385. Consume(actasmtoken);
  3386. if not errorflag then
  3387. Message(assem_e_invalid_string_expression);
  3388. errorflag := TRUE;
  3389. end;
  3390. end;
  3391. Until false;
  3392. end;
  3393. Procedure BuildOpCode;
  3394. {*********************************************************************}
  3395. { PROCEDURE BuildOpcode; }
  3396. { Description: Parses the intel opcode and operands, and writes it }
  3397. { in the TInstruction object. }
  3398. {*********************************************************************}
  3399. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  3400. { On ENTRY: Token should point to AS_OPCODE }
  3401. {*********************************************************************}
  3402. var
  3403. asmtok: tasmop;
  3404. op: tasmop;
  3405. expr: string;
  3406. segreg: tregister;
  3407. Begin
  3408. expr := '';
  3409. asmtok := A_NONE; { assmume no prefix }
  3410. segreg := R_NO; { assume no segment override }
  3411. { prefix seg opcode }
  3412. { prefix opcode }
  3413. if findprefix(actasmpattern,asmtok) then
  3414. Begin
  3415. { standard opcode prefix }
  3416. if asmtok <> A_NONE then
  3417. instr.addprefix(asmtok);
  3418. Consume(AS_OPCODE);
  3419. end;
  3420. { allow for newline as in gas styled syntax }
  3421. { under DOS you get two AS_SEPARATOR !! }
  3422. while actasmtoken=AS_SEPARATOR do
  3423. Consume(AS_SEPARATOR);
  3424. if (actasmtoken <> AS_OPCODE) then
  3425. Begin
  3426. Message(assem_e_invalid_or_missing_opcode);
  3427. { error recovery }
  3428. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA,AS_END]) do
  3429. Consume(actasmtoken);
  3430. exit;
  3431. end
  3432. else
  3433. Begin
  3434. op := findopcode(actasmpattern);
  3435. instr.addinstr(op);
  3436. { Valid combination of prefix and instruction ? }
  3437. if (asmtok <> A_NONE) and (NOT CheckPrefix(asmtok,op)) then
  3438. Message1(assem_e_invalid_prefix_and_opcode,actasmpattern);
  3439. Consume(AS_OPCODE);
  3440. { Zero operand opcode ? }
  3441. if actasmtoken in [AS_SEPARATOR,AS_END] then
  3442. exit
  3443. else
  3444. operandnum := 1;
  3445. end;
  3446. repeat
  3447. case actasmtoken of
  3448. AS_COMMA: { Operand delimiter }
  3449. Begin
  3450. if operandnum > MaxOperands then
  3451. Message(assem_e_too_many_operands)
  3452. else
  3453. Inc(operandnum);
  3454. Consume(AS_COMMA);
  3455. end;
  3456. AS_SEPARATOR,
  3457. AS_END : { End of asm operands for this opcode }
  3458. begin
  3459. break;
  3460. end;
  3461. else
  3462. BuildOperand(instr);
  3463. end; { end case }
  3464. until false;
  3465. end;
  3466. Function Assemble: Ptree;
  3467. {*********************************************************************}
  3468. { PROCEDURE Assemble; }
  3469. { Description: Parses the att assembler syntax, parsing is done }
  3470. { according to GAs rules. }
  3471. {*********************************************************************}
  3472. Var
  3473. hl: plabel;
  3474. labelptr,nextlabel : pasmlabel;
  3475. commname : string;
  3476. store_p : paasmoutput;
  3477. Begin
  3478. Message(assem_d_start_att);
  3479. firsttoken := TRUE;
  3480. operandnum := 0;
  3481. previous_was_id := FALSE;
  3482. if assigned(procinfo.retdef) and
  3483. (is_fpu(procinfo.retdef) or
  3484. ret_in_acc(procinfo.retdef)) then
  3485. procinfo.funcret_is_valid:=true;
  3486. { sets up all opcode and register tables in uppercase }
  3487. if not _asmsorted then
  3488. Begin
  3489. SetupTables;
  3490. _asmsorted := TRUE;
  3491. end;
  3492. p:=new(paasmoutput,init);
  3493. { save pointer code section }
  3494. store_p:=p;
  3495. { setup label linked list }
  3496. labellist.init;
  3497. c:=current_scanner^.asmgetchar;
  3498. actasmtoken:=gettoken;
  3499. repeat
  3500. case actasmtoken of
  3501. AS_LLABEL:
  3502. Begin
  3503. labelptr := labellist.search(actasmpattern);
  3504. if not assigned(labelptr) then
  3505. Begin
  3506. getlabel(hl);
  3507. labellist.insert(actasmpattern,hl,TRUE);
  3508. ConcatLabel(p,hl);
  3509. end
  3510. else
  3511. Begin
  3512. { the label has already been inserted into the }
  3513. { label list, either as an instruction label (in}
  3514. { this case it has not been emitted), or as a }
  3515. { duplicate local symbol (in this case it has }
  3516. { already been emitted). }
  3517. if labelptr^.emitted then
  3518. Message1(assem_e_dup_local_sym,'.L'+labelptr^.name^)
  3519. else
  3520. Begin
  3521. if assigned(labelptr^.lab) then
  3522. ConcatLabel(p,labelptr^.lab);
  3523. labelptr^.emitted := TRUE;
  3524. end;
  3525. end;
  3526. Consume(AS_LLABEL);
  3527. end;
  3528. AS_LABEL:
  3529. Begin
  3530. { when looking for Pascal labels, these must }
  3531. { be in uppercase. }
  3532. if SearchLabel(upper(actasmpattern),hl) then
  3533. ConcatLabel(p,hl)
  3534. else
  3535. Begin
  3536. if (cs_compilesystem in aktmoduleswitches) then
  3537. begin
  3538. Message1(assem_e_unknown_label_identifer,actasmpattern);
  3539. { once again we don't know what it represents }
  3540. { so we simply concatenate it }
  3541. ConcatLocal(p,actasmpattern);
  3542. end
  3543. else
  3544. Message1(assem_e_unknown_label_identifer,actasmpattern);
  3545. end;
  3546. Consume(AS_LABEL);
  3547. end;
  3548. AS_DW:
  3549. Begin
  3550. Consume(AS_DW);
  3551. BuildConstant($ffff);
  3552. end;
  3553. AS_DATA:
  3554. Begin
  3555. { -- this should only be allowed for system development -- }
  3556. { i think this should be fixed in the dos unit, and }
  3557. { not here. }
  3558. if (cs_compilesystem in aktmoduleswitches) then
  3559. p:=datasegment
  3560. else
  3561. Message(assem_e_switching_sections_not_allowed);
  3562. Consume(AS_DATA);
  3563. end;
  3564. AS_TEXT:
  3565. Begin
  3566. { -- this should only be allowed for system development -- }
  3567. { i think this should be fixed in the dos unit, and }
  3568. { not here. }
  3569. if (cs_compilesystem in aktmoduleswitches) then
  3570. p:=store_p
  3571. else
  3572. Message(assem_e_switching_sections_not_allowed);
  3573. Consume(AS_TEXT);
  3574. end;
  3575. AS_DB:
  3576. Begin
  3577. Consume(AS_DB);
  3578. BuildConstant($ff);
  3579. end;
  3580. AS_DD:
  3581. Begin
  3582. Consume(AS_DD);
  3583. BuildConstant($ffffffff);
  3584. end;
  3585. AS_DQ:
  3586. Begin
  3587. Consume(AS_DQ);
  3588. BuildRealConstant(s64bit);
  3589. end;
  3590. AS_SINGLE:
  3591. Begin
  3592. Consume(AS_SINGLE);
  3593. BuildRealConstant(s32real);
  3594. end;
  3595. AS_DOUBLE:
  3596. Begin
  3597. Consume(AS_DOUBLE);
  3598. BuildRealConstant(s64real);
  3599. end;
  3600. AS_EXTENDED:
  3601. Begin
  3602. Consume(AS_EXTENDED);
  3603. BuildRealConstant(s80real);
  3604. end;
  3605. AS_GLOBAL:
  3606. Begin
  3607. { normal units should not be able to declare }
  3608. { direct label names like this... anyhow }
  3609. { procedural calls in asm blocks are }
  3610. { supposedely replaced automatically }
  3611. if (cs_compilesystem in aktmoduleswitches) then
  3612. begin
  3613. Consume(AS_GLOBAL);
  3614. if actasmtoken <> AS_ID then
  3615. Message(assem_e_invalid_global_def)
  3616. else
  3617. ConcatPublic(p,actasmpattern);
  3618. Consume(actasmtoken);
  3619. if actasmtoken <> AS_SEPARATOR then
  3620. Begin
  3621. Message(assem_e_line_separator_expected);
  3622. while actasmtoken <> AS_SEPARATOR do
  3623. Consume(actasmtoken);
  3624. end;
  3625. end
  3626. else
  3627. begin
  3628. Message(assem_w_globl_not_supported);
  3629. while actasmtoken <> AS_SEPARATOR do
  3630. Consume(actasmtoken);
  3631. end;
  3632. end;
  3633. AS_ALIGN:
  3634. Begin
  3635. Message(assem_w_align_not_supported);
  3636. while actasmtoken <> AS_SEPARATOR do
  3637. Consume(actasmtoken);
  3638. end;
  3639. AS_ASCIIZ:
  3640. Begin
  3641. Consume(AS_ASCIIZ);
  3642. BuildStringConstant(TRUE);
  3643. end;
  3644. AS_ASCII:
  3645. Begin
  3646. Consume(AS_ASCII);
  3647. BuildStringConstant(FALSE);
  3648. end;
  3649. AS_LCOMM:
  3650. Begin
  3651. { -- this should only be allowed for system development -- }
  3652. { -- otherwise may mess up future enhancements we might -- }
  3653. { -- add. -- }
  3654. if (cs_compilesystem in aktmoduleswitches) then
  3655. begin
  3656. Consume(AS_LCOMM);
  3657. if actasmtoken <> AS_ID then
  3658. begin
  3659. Message(assem_e_invalid_lcomm_def);
  3660. { error recovery }
  3661. while actasmtoken <> AS_SEPARATOR do
  3662. Consume(actasmtoken);
  3663. end
  3664. else
  3665. begin
  3666. commname:=actasmpattern;
  3667. Consume(AS_COMMA);
  3668. ConcatLocalBss(actasmpattern,BuildConstExpression(false,false));
  3669. if actasmtoken <> AS_SEPARATOR then
  3670. Begin
  3671. Message(assem_e_line_separator_expected);
  3672. while actasmtoken <> AS_SEPARATOR do
  3673. Consume(actasmtoken);
  3674. end;
  3675. end;
  3676. end
  3677. else
  3678. begin
  3679. Message(assem_w_lcomm_not_supported);
  3680. while actasmtoken <> AS_SEPARATOR do
  3681. Consume(actasmtoken);
  3682. end;
  3683. end;
  3684. AS_COMM:
  3685. Begin
  3686. { -- this should only be allowed for system development -- }
  3687. { -- otherwise may mess up future enhancements we might -- }
  3688. { -- add. -- }
  3689. if (cs_compilesystem in aktmoduleswitches) then
  3690. begin
  3691. Consume(AS_COMM);
  3692. if actasmtoken <> AS_ID then
  3693. begin
  3694. Message(assem_e_invalid_comm_def);
  3695. { error recovery }
  3696. while actasmtoken <> AS_SEPARATOR do
  3697. Consume(actasmtoken);
  3698. end
  3699. else
  3700. begin
  3701. commname:=actasmpattern;
  3702. Consume(AS_COMMA);
  3703. ConcatGlobalBss(actasmpattern,BuildConstExpression(false,false));
  3704. if actasmtoken <> AS_SEPARATOR then
  3705. Begin
  3706. Message(assem_e_line_separator_expected);
  3707. while actasmtoken <> AS_SEPARATOR do
  3708. Consume(actasmtoken);
  3709. end;
  3710. end;
  3711. end
  3712. else
  3713. begin
  3714. Message(assem_w_comm_not_supported);
  3715. while actasmtoken <> AS_SEPARATOR do
  3716. Consume(actasmtoken);
  3717. end;
  3718. end;
  3719. AS_OPCODE:
  3720. Begin
  3721. instr.init;
  3722. BuildOpcode;
  3723. instr.numops := operandnum;
  3724. if instr.labeled then
  3725. ConcatLabeledInstr(instr)
  3726. else
  3727. ConcatOpCode(instr);
  3728. instr.done;
  3729. end;
  3730. AS_SEPARATOR:
  3731. Begin
  3732. Consume(AS_SEPARATOR);
  3733. { let us go back to the first operand }
  3734. operandnum := 0;
  3735. end;
  3736. AS_END:
  3737. begin
  3738. break; { end assembly block }
  3739. end;
  3740. else
  3741. Begin
  3742. Message(assem_e_assemble_node_syntax_error);
  3743. { error recovery }
  3744. Consume(actasmtoken);
  3745. end;
  3746. end; { end case }
  3747. until false;
  3748. { check if there were undefined symbols. }
  3749. { if so, then list each of those undefined }
  3750. { labels. }
  3751. if assigned(labellist.First) then
  3752. Begin
  3753. labelptr := labellist.First;
  3754. While labelptr <> nil do
  3755. Begin
  3756. nextlabel:=labelptr^.next;
  3757. if not labelptr^.emitted then
  3758. Message1(assem_e_local_sym_not_found_in_asm_statement,'.L'+labelptr^.name^);
  3759. labelptr:=nextlabel;
  3760. end;
  3761. end;
  3762. if p<>store_p then
  3763. begin
  3764. Message(assem_e_assembler_code_not_returned_to_text);
  3765. p:=store_p;
  3766. end;
  3767. assemble := genasmnode(p);
  3768. labellist.done;
  3769. Message(assem_d_finish_att);
  3770. end;
  3771. var
  3772. old_exit: pointer;
  3773. procedure ra386att_exit;{$ifndef FPC}far;{$endif}
  3774. begin
  3775. if assigned(iasmops) then
  3776. dispose(iasmops);
  3777. exitproc:=old_exit;
  3778. end;
  3779. Begin
  3780. old_exit := exitproc;
  3781. exitproc := @ra386att_exit;
  3782. end.
  3783. {
  3784. $Log$
  3785. Revision 1.36 1999-04-18 00:32:22 pierre
  3786. * fix for bug0124 and better error position info
  3787. Revision 1.35 1999/04/17 22:16:55 pierre
  3788. * ifdef USE_OP3 released (changed into ifndef NO_OP3)
  3789. * SHRD and SHLD first operand (ATT syntax) can only be CL reg or immediate const
  3790. Revision 1.34 1999/04/16 10:00:58 pierre
  3791. + ifdef USE_OP3 code :
  3792. added all missing op_... constructors for tai386 needed
  3793. for SHRD,SHLD and IMUL code in assembler readers
  3794. (check in tests/tbs0123.pp)
  3795. Revision 1.33 1999/02/25 21:02:47 peter
  3796. * ag386bin updates
  3797. + coff writer
  3798. Revision 1.32 1999/02/22 02:15:34 peter
  3799. * updates for ag386bin
  3800. Revision 1.31 1999/01/29 11:24:02 pierre
  3801. * incompatible size warning or error suppressed for sizes > 4
  3802. Revision 1.30 1999/01/28 14:12:59 pierre
  3803. * bug0175 solved give error on $R+ and warning otherwise
  3804. Revision 1.29 1999/01/10 15:37:54 peter
  3805. * moved some tables from ra386*.pas -> i386.pas
  3806. + start of coff writer
  3807. * renamed asmutils unit to rautils
  3808. Revision 1.28 1998/12/28 15:47:09 peter
  3809. * general constant solution. Constant expressions are now almost
  3810. everywhere allowed and correctly parsed
  3811. Revision 1.27 1998/12/23 22:55:56 peter
  3812. + rec.field(%esi) support
  3813. + [esi+rec.field] support
  3814. Revision 1.26 1998/12/11 00:03:42 peter
  3815. + globtype,tokens,version unit splitted from globals
  3816. Revision 1.25 1998/12/09 13:23:40 jonas
  3817. * replaced two "in [A_...]" constructs I had missed before
  3818. * for shrd/shld: "if operands[1].operandtype = OPR_CONSTANT" instead of
  3819. "if operands[3].operandtype ..."
  3820. Revision 1.24 1998/12/08 23:03:48 jonas
  3821. * allow constant offsets for go32v2 in assembler blocks
  3822. Revision 1.23 1998/12/02 16:23:33 jonas
  3823. * changed "if longintvar in set" to case or "if () or () .." statements
  3824. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  3825. Revision 1.22 1998/11/29 12:47:21 peter
  3826. * fixes for 'asm sti end;'
  3827. Revision 1.21 1998/11/16 15:38:54 peter
  3828. * fixed instruct not in table msg
  3829. Revision 1.20 1998/11/13 15:40:27 pierre
  3830. + added -Se in Makefile cvstest target
  3831. + lexlevel cleanup
  3832. normal_function_level main_program_level and unit_init_level defined
  3833. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  3834. (test added in code !)
  3835. * -Un option was wrong
  3836. * _FAIL and _SELF only keyword inside
  3837. constructors and methods respectively
  3838. Revision 1.19 1998/11/13 10:12:16 peter
  3839. * constant fixes
  3840. Revision 1.18 1998/11/05 23:48:26 peter
  3841. * recordtype.field support in constant expressions
  3842. * fixed imul for oa_imm8 which was not allowed
  3843. * fixed reading of local typed constants
  3844. * fixed comment reading which is not any longer a separator
  3845. Revision 1.17 1998/10/28 21:34:39 peter
  3846. * fixed some opsize
  3847. Revision 1.16 1998/10/28 00:08:48 peter
  3848. + leal procsym,eax is now allowed
  3849. + constants are now handled also when starting an expression
  3850. + call *pointer is now allowed
  3851. Revision 1.15 1998/10/13 16:50:16 pierre
  3852. * undid some changes of Peter that made the compiler wrong
  3853. for m68k (I had to reinsert some ifdefs)
  3854. * removed several memory leaks under m68k
  3855. * removed the meory leaks for assembler readers
  3856. * cross compiling shoud work again better
  3857. ( crosscompiling sysamiga works
  3858. but as68k still complain about some code !)
  3859. Revision 1.14 1998/10/07 04:28:52 carl
  3860. * bugfix of in/out with gas (ins/outs still left though)
  3861. * bugfix of wrong error output with concatlabel
  3862. * gettoken always returns something valid now
  3863. Revision 1.13 1998/09/24 17:52:31 carl
  3864. * bugfix from fix branch
  3865. Revision 1.12.2.1 1998/09/24 17:47:16 carl
  3866. * bugfix with objects/records access
  3867. Revision 1.12 1998/09/03 17:08:46 pierre
  3868. * better lines for stabs
  3869. (no scroll back to if before else part
  3870. no return to case line at jump outside case)
  3871. + source lines also if not in order
  3872. Revision 1.11 1998/09/02 15:16:44 carl
  3873. * indirect register refs with movsx/movzx fixed
  3874. Revision 1.10 1998/09/02 01:24:09 carl
  3875. * bugfix of PUSH opcode with constants
  3876. Revision 1.9 1998/08/21 08:45:49 pierre
  3877. * better line info for asm statements
  3878. Revision 1.8 1998/08/19 16:07:54 jonas
  3879. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  3880. Revision 1.7 1998/08/10 23:57:23 peter
  3881. * use bestreal instead of extended
  3882. Revision 1.6 1998/08/10 14:50:21 peter
  3883. + localswitches, moduleswitches, globalswitches splitting
  3884. Revision 1.5 1998/07/14 14:46:58 peter
  3885. * released NEWINPUT
  3886. Revision 1.4 1998/07/08 15:06:41 daniel
  3887. + $N+,E+ added for TP.
  3888. Revision 1.3 1998/07/07 11:20:07 peter
  3889. + NEWINPUT for a better inputfile and scanner object
  3890. Revision 1.2 1998/06/24 14:06:36 peter
  3891. * fixed the name changes
  3892. Revision 1.1 1998/06/23 14:00:17 peter
  3893. * renamed RA* units
  3894. Revision 1.13 1998/06/16 08:56:29 peter
  3895. + targetcpu
  3896. * cleaner pmodules for newppu
  3897. Revision 1.12 1998/06/12 10:32:35 pierre
  3898. * column problem hopefully solved
  3899. + C vars declaration changed
  3900. Revision 1.11 1998/05/31 14:13:35 peter
  3901. * fixed call bugs with assembler readers
  3902. + OPR_SYMBOL to hold a symbol in the asm parser
  3903. * fixed staticsymtable vars which were acessed through %ebp instead of
  3904. name
  3905. Revision 1.10 1998/05/30 14:31:08 peter
  3906. + $ASMMODE
  3907. Revision 1.9 1998/05/29 09:58:16 pierre
  3908. * OPR_REGISTER for 1 arg was missing in ratti386.pas
  3909. (probably a merging problem)
  3910. * errors at start of line were lost
  3911. Revision 1.8 1998/05/28 16:34:36 carl
  3912. * call bugfix
  3913. * operand with regs bugfix (manual patch in both cases)
  3914. Revision 1.7 1998/05/23 01:21:27 peter
  3915. + aktasmmode, aktoptprocessor, aktoutputformat
  3916. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  3917. + $LIBNAME to set the library name where the unit will be put in
  3918. * splitted cgi386 a bit (codeseg to large for bp7)
  3919. * nasm, tasm works again. nasm moved to ag386nsm.pas
  3920. Revision 1.6 1998/05/20 09:42:37 pierre
  3921. + UseTokenInfo now default
  3922. * unit in interface uses and implementation uses gives error now
  3923. * only one error for unknown symbol (uses lastsymknown boolean)
  3924. the problem came from the label code !
  3925. + first inlined procedures and function work
  3926. (warning there might be allowed cases were the result is still wrong !!)
  3927. * UseBrower updated gives a global list of all position of all used symbols
  3928. with switch -gb
  3929. Revision 1.5 1998/04/29 13:52:23 peter
  3930. * small optimize fix
  3931. Revision 1.4 1998/04/29 10:34:04 pierre
  3932. + added some code for ansistring (not complete nor working yet)
  3933. * corrected operator overloading
  3934. * corrected nasm output
  3935. + started inline procedures
  3936. + added starstarn : use ** for exponentiation (^ gave problems)
  3937. + started UseTokenInfo cond to get accurate positions
  3938. Revision 1.3 1998/04/08 16:58:07 pierre
  3939. * several bugfixes
  3940. ADD ADC and AND are also sign extended
  3941. nasm output OK (program still crashes at end
  3942. and creates wrong assembler files !!)
  3943. procsym types sym in tdef removed !!
  3944. }