ra386att.pas 158 KB

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