ra386int.pas 135 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436
  1. {
  2. $Id$
  3. Copyright (c) 1997-98 by Carl Eric Codere
  4. Does the parsing process for the intel 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 Ra386int;
  19. {**********************************************************************}
  20. { WARNING }
  21. {**********************************************************************}
  22. { Any modification in the order or removal of terms in the tables }
  23. { in i386.pas and intasmi3.pas will BREAK the code in this unit, }
  24. { unless the appropriate changes are made to this unit. Addition }
  25. { of terms though, will not change the code herein. }
  26. {**********************************************************************}
  27. {--------------------------------------------------------------------}
  28. { LEFT TO DO: }
  29. {--------------------------------------------------------------------}
  30. { o Add support for floating point opcodes. }
  31. { o Handle module overrides also... such as crt.white or }
  32. { crt.delay and local typed constants. }
  33. { o Handle label references }
  34. { o Add support for TP styled segment overrides, when the opcode }
  35. { table will be completed. }
  36. { o Add imul,shld and shrd support with references and CL }
  37. { i386.pas requires to be updated to do this. }
  38. { o Support for (* *) tp styled comments, this support should be }
  39. { added in asmgetchar in scanner.pas (it cannot be implemented }
  40. { here without causing errors such as in : }
  41. { (* "openbrace" AComment *) }
  42. { (presently an infinite loop will be created if a (* styled }
  43. { comment is found). }
  44. { o Bugfix of ao_imm8s for IMUL. (Currently the 3 operand imul will }
  45. { be considered as invalid because I use ao_imm8 and the table }
  46. { uses ao_imm8s). }
  47. {--------------------------------------------------------------------}
  48. Interface
  49. uses
  50. tree,i386;
  51. function assemble: ptree;
  52. const
  53. { this variable is TRUE if the lookup tables have already been setup }
  54. { for fast access. On the first call to assemble the tables are setup }
  55. { and stay set up. }
  56. _asmsorted: boolean = FALSE;
  57. firstreg = R_EAX;
  58. lastreg = R_ST7;
  59. type
  60. tiasmops = array[firstop..lastop] of string[7];
  61. piasmops = ^tiasmops;
  62. var
  63. { sorted tables of opcodes }
  64. iasmops: piasmops;
  65. { uppercased tables of registers }
  66. iasmregs: array[firstreg..lastreg] of string[6];
  67. Implementation
  68. Uses
  69. systems,files,aasm,globals,AsmUtils,strings,hcodegen,scanner,
  70. cobjects,verbose,types;
  71. type
  72. tinteltoken = (
  73. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_HEXNUM,AS_OCTALNUM,
  74. AS_BINNUM,AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  75. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,AS_INTNUM,
  76. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,
  77. {------------------ Assembler directives --------------------}
  78. AS_DB,AS_DW,AS_DD,AS_END,
  79. {------------------ Assembler Operators --------------------}
  80. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_NEAR,AS_FAR,
  81. AS_HIGH,AS_LOW,AS_OFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  82. AS_AND,AS_OR,AS_XOR);
  83. tasmkeyword = string[6];
  84. const
  85. { These tokens should be modified accordingly to the modifications }
  86. { in the different enumerations. }
  87. firstdirective = AS_DB;
  88. lastdirective = AS_END;
  89. firstoperator = AS_BYTE;
  90. lastoperator = AS_XOR;
  91. firstsreg = R_CS;
  92. lastsreg = R_SS;
  93. { this is a hack to accept all opcodes }
  94. { in the opcode table. }
  95. { check is done until A_POPFD }
  96. { otherwise no check. }
  97. lastop_in_table = A_POPFD;
  98. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  99. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  100. _count_asmprefixes = 5;
  101. _count_asmspecialops = 25;
  102. _count_asmoverrides = 3;
  103. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  104. ('DB','DW','DD','END');
  105. { problems with shl,shr,not,and,or and xor, they are }
  106. { context sensitive. }
  107. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  108. 'BYTE','WORD','DWORD','QWORD','TBYTE','NEAR','FAR','HIGH',
  109. 'LOW','OFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  110. 'OR','XOR');
  111. {------------------ Missing opcodes from std list ----------------}
  112. _asmprefixes: array[0.._count_asmprefixes] of tasmkeyword = (
  113. 'REPNE','REPE','REP','REPZ','REPNZ','LOCK');
  114. _asmoverrides: array[0.._count_asmoverrides] of tasmkeyword =
  115. ('SEGCS','SEGDS','SEGES','SEGSS');
  116. _overridetokens: array[0.._count_asmoverrides] of tregister =
  117. (R_CS,R_DS,R_ES,R_SS);
  118. _prefixtokens: array[0.._count_asmprefixes] of tasmop = (
  119. A_REPNE,A_REPE,A_REP,A_REPE,A_REPNE,A_LOCK);
  120. _specialops: array[0.._count_asmspecialops] of tasmkeyword = (
  121. 'CMPSB','CMPSW','CMPSD','INSB','INSW','INSD','OUTSB','OUTSW','OUTSD',
  122. 'SCASB','SCASW','SCASD','STOSB','STOSW','STOSD','MOVSB','MOVSW','MOVSD',
  123. 'LODSB','LODSW','LODSD','LOCK','SEGCS','SEGDS','SEGES','SEGSS');
  124. _specialopstokens: array[0.._count_asmspecialops] of tasmop = (
  125. A_CMPS,A_CMPS,A_CMPS,A_INS,A_INS,A_INS,A_OUTS,A_OUTS,A_OUTS,
  126. A_SCAS,A_SCAS,A_SCAS,A_STOS,A_STOS,A_STOS,A_MOVS,A_MOVS,A_MOVS,
  127. A_LODS,A_LODS,A_LODS,A_LOCK,A_NONE,A_NONE,A_NONE,A_NONE);
  128. {------------------------------------------------------------------}
  129. { register type definition table for easier searching }
  130. _regtypes:array[firstreg..lastreg] of longint =
  131. (ao_reg32,ao_reg32,ao_reg32,ao_reg32,ao_reg32,ao_reg32,ao_reg32,ao_reg32,
  132. ao_reg16,ao_reg16,ao_reg16,ao_reg16,ao_reg16,ao_reg16,ao_reg16,ao_reg16,
  133. ao_reg8,ao_reg8,ao_reg8,ao_reg8,ao_reg8,ao_reg8,ao_reg8,ao_reg8,
  134. ao_none,ao_sreg2,ao_sreg2,ao_sreg2,ao_sreg3,ao_sreg3,ao_sreg2,
  135. ao_floatacc,ao_floatacc,ao_floatreg,ao_floatreg,ao_floatreg,ao_floatreg,
  136. ao_floatreg,ao_floatreg,ao_floatreg);
  137. _regsizes: array[firstreg..lastreg] of topsize =
  138. (S_L,S_L,S_L,S_L,S_L,S_L,S_L,S_L,
  139. S_W,S_W,S_W,S_W,S_W,S_W,S_W,S_W,
  140. S_B,S_B,S_B,S_B,S_B,S_B,S_B,S_B,
  141. { segment register }
  142. S_W,S_W,S_W,S_W,S_W,S_W,S_W,
  143. { can also be S_S or S_T - must be checked at run-time }
  144. S_FL,S_FL,S_FL,S_FL,S_FL,S_FL,S_FL,S_FL,S_FL);
  145. {topsize = (S_NO,S_B,S_W,S_L,S_BW,S_BL,S_WL,
  146. S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX,S_D);}
  147. _constsizes: array[S_NO..S_FS] of longint =
  148. (0,ao_imm8,ao_imm16,ao_imm32,0,0,0,ao_imm16,ao_imm32,0,ao_imm32);
  149. const
  150. newline = #10;
  151. firsttoken : boolean = TRUE;
  152. operandnum : byte = 0;
  153. var
  154. { context for SHL,SHR,AND,NOT,OR,XOR operators }
  155. { if set to true GetToken will return these }
  156. { as operators, otherwise will return these as }
  157. { opcodes. }
  158. inexpression: boolean;
  159. p : paasmoutput;
  160. actasmtoken: tinteltoken;
  161. actasmpattern: string;
  162. c: char;
  163. Instr: TInstruction;
  164. labellist: TAsmLabelList;
  165. old_exit : pointer;
  166. Procedure SetupTables;
  167. { creates uppercased symbol tables for speed access }
  168. var
  169. i: tasmop;
  170. j: tregister;
  171. Begin
  172. Message(assem_d_creating_lookup_tables);
  173. { opcodes }
  174. new(iasmops);
  175. for i:=firstop to lastop do
  176. iasmops^[i] := upper(int_op2str[i]);
  177. { opcodes }
  178. for j:=firstreg to lastreg do
  179. iasmregs[j] := upper(int_reg2str[j]);
  180. end;
  181. {---------------------------------------------------------------------}
  182. { Routines for the tokenizing }
  183. {---------------------------------------------------------------------}
  184. function is_asmopcode(const s: string):Boolean;
  185. {*********************************************************************}
  186. { FUNCTION is_asmopcode(s: string):Boolean }
  187. { Description: Determines if the s string is a valid opcode }
  188. { if so returns TRUE otherwise returns FALSE. }
  189. {*********************************************************************}
  190. var
  191. i: tasmop;
  192. j: byte;
  193. Begin
  194. is_asmopcode := FALSE;
  195. for i:=firstop to lastop do
  196. begin
  197. if s = iasmops^[i] then
  198. begin
  199. is_asmopcode:=TRUE;
  200. exit;
  201. end;
  202. end;
  203. { not found yet, 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. end;
  213. Procedure is_asmdirective(const s: string; var token: tinteltoken);
  214. {*********************************************************************}
  215. { FUNCTION is_asmdirective(s: string; var token: tinteltoken):Boolean }
  216. { Description: Determines if the s string is a valid directive }
  217. { (an operator can occur in operand fields, while a directive cannot) }
  218. { if so returns the directive token, otherwise does not change token.}
  219. {*********************************************************************}
  220. var
  221. i:byte;
  222. Begin
  223. for i:=0 to _count_asmdirectives do
  224. begin
  225. if s=_asmdirectives[i] then
  226. begin
  227. token := tinteltoken(longint(firstdirective)+i);
  228. exit;
  229. end;
  230. end;
  231. end;
  232. Procedure is_asmoperator(const s: string; var token: tinteltoken);
  233. {*********************************************************************}
  234. { FUNCTION is_asmoperator(s: string; var token: tinteltoken): Boolean}
  235. { Description: Determines if the s string is a valid operator }
  236. { (an operator can occur in operand fields, while a directive cannot) }
  237. { if so returns the operator token, otherwise does not change token. }
  238. {*********************************************************************}
  239. var
  240. i:longint;
  241. Begin
  242. for i:=0 to _count_asmoperators do
  243. begin
  244. if s=_asmoperators[i] then
  245. begin
  246. token := tinteltoken(longint(firstoperator)+i);
  247. exit;
  248. end;
  249. end;
  250. end;
  251. Procedure is_register(const s: string; var token: tinteltoken);
  252. {*********************************************************************}
  253. { PROCEDURE is_register(s: string; var token: tinteltoken); }
  254. { Description: Determines if the s string is a valid register, if }
  255. { so return token equal to A_REGISTER, otherwise does not change token}
  256. {*********************************************************************}
  257. Var
  258. i: tregister;
  259. Begin
  260. for i:=firstreg to lastreg do
  261. begin
  262. if s=iasmregs[i] then
  263. begin
  264. token := AS_REGISTER;
  265. exit;
  266. end;
  267. end;
  268. end;
  269. Function GetToken: tinteltoken;
  270. {*********************************************************************}
  271. { FUNCTION GetToken: tinteltoken; }
  272. { Description: This routine returns intel assembler tokens and }
  273. { does some minor syntax error checking. }
  274. {*********************************************************************}
  275. var
  276. j: integer;
  277. token: tinteltoken;
  278. forcelabel: boolean;
  279. errorflag : boolean;
  280. begin
  281. errorflag := FALSE;
  282. forcelabel := FALSE;
  283. actasmpattern :='';
  284. {* INIT TOKEN TO NOTHING *}
  285. token := AS_NONE;
  286. { while space and tab , continue scan... }
  287. while (c in [' ',#9]) do
  288. c := asmgetchar;
  289. { Possiblities for first token in a statement: }
  290. { Local Label, Label, Directive, Prefix or Opcode.... }
  291. tokenpos.line:=current_module^.current_inputfile^.line_no;
  292. tokenpos.column:=get_file_col;
  293. tokenpos.fileindex:=current_module^.current_index;
  294. if firsttoken and not (c in [newline,#13,'{',';']) then
  295. begin
  296. firsttoken := FALSE;
  297. if c = '@' then
  298. begin
  299. token := AS_LLABEL; { this is a local label }
  300. { Let us point to the next character }
  301. c := asmgetchar;
  302. end;
  303. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  304. begin
  305. { if there is an at_sign, then this must absolutely be a label }
  306. if c = '@' then forcelabel:=TRUE;
  307. actasmpattern := actasmpattern + c;
  308. c := asmgetchar;
  309. end;
  310. uppervar(actasmpattern);
  311. if c = ':' then
  312. begin
  313. case token of
  314. AS_NONE: token := AS_LABEL;
  315. AS_LLABEL: ; { do nothing }
  316. end; { end case }
  317. { let us point to the next character }
  318. c := asmgetchar;
  319. gettoken := token;
  320. exit;
  321. end;
  322. { Are we trying to create an identifier with }
  323. { an at-sign...? }
  324. if forcelabel then
  325. Message(assem_e_none_label_contain_at);
  326. If is_asmopcode(actasmpattern) then
  327. Begin
  328. gettoken := AS_OPCODE;
  329. { check if we are in an expression }
  330. { then continue with asm directives }
  331. if not inexpression then
  332. exit;
  333. end;
  334. is_asmdirective(actasmpattern, token);
  335. if (token <> AS_NONE) then
  336. Begin
  337. gettoken := token;
  338. exit
  339. end
  340. else
  341. begin
  342. gettoken := AS_NONE;
  343. Message1(assem_e_invalid_operand,actasmpattern);
  344. end;
  345. end
  346. else { else firsttoken }
  347. { Here we must handle all possible cases }
  348. begin
  349. case c of
  350. '@': { possiblities : - local label reference , such as in jmp @local1 }
  351. { - @Result, @Code or @Data special variables. }
  352. begin
  353. actasmpattern := c;
  354. c:= asmgetchar;
  355. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  356. begin
  357. actasmpattern := actasmpattern + c;
  358. c := asmgetchar;
  359. end;
  360. uppervar(actasmpattern);
  361. gettoken := AS_ID;
  362. exit;
  363. end;
  364. { identifier, register, opcode, prefix or directive }
  365. 'A'..'Z','a'..'z','_': begin
  366. actasmpattern := c;
  367. c:= asmgetchar;
  368. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  369. begin
  370. actasmpattern := actasmpattern + c;
  371. c := asmgetchar;
  372. end;
  373. uppervar(actasmpattern);
  374. If is_asmopcode(actasmpattern) then
  375. Begin
  376. gettoken := AS_OPCODE;
  377. { if we are not in a constant }
  378. { expression than this is an }
  379. { opcode. }
  380. if not inexpression then
  381. exit;
  382. end;
  383. is_register(actasmpattern, token);
  384. is_asmoperator(actasmpattern,token);
  385. is_asmdirective(actasmpattern,token);
  386. { if found }
  387. if (token <> AS_NONE) then
  388. begin
  389. gettoken := token;
  390. exit;
  391. end
  392. { this is surely an identifier }
  393. else
  394. token := AS_ID;
  395. gettoken := token;
  396. exit;
  397. end;
  398. { override operator... not supported }
  399. '&': begin
  400. Message(assem_w_override_op_not_supported);
  401. c:=asmgetchar;
  402. gettoken := AS_NONE;
  403. end;
  404. { string or character }
  405. '''' :
  406. begin
  407. actasmpattern:='';
  408. while true do
  409. begin
  410. if c = '''' then
  411. begin
  412. c:=asmgetchar;
  413. if c=newline then
  414. begin
  415. Message(scan_f_string_exceeds_line);
  416. break;
  417. end;
  418. repeat
  419. if c=''''then
  420. begin
  421. c:=asmgetchar;
  422. if c='''' then
  423. begin
  424. actasmpattern:=actasmpattern+'''';
  425. c:=asmgetchar;
  426. if c=newline then
  427. begin
  428. Message(scan_f_string_exceeds_line);
  429. break;
  430. end;
  431. end
  432. else break;
  433. end
  434. else
  435. begin
  436. actasmpattern:=actasmpattern+c;
  437. c:=asmgetchar;
  438. if c=newline then
  439. begin
  440. Message(scan_f_string_exceeds_line);
  441. break
  442. end;
  443. end;
  444. until false; { end repeat }
  445. end
  446. else break; { end if }
  447. end; { end while }
  448. token:=AS_STRING;
  449. gettoken := token;
  450. exit;
  451. end;
  452. { string or character }
  453. '"' :
  454. begin
  455. actasmpattern:='';
  456. while true do
  457. begin
  458. if c = '"' then
  459. begin
  460. c:=asmgetchar;
  461. if c=newline then
  462. begin
  463. Message(scan_f_string_exceeds_line);
  464. break;
  465. end;
  466. repeat
  467. if c='"'then
  468. begin
  469. c:=asmgetchar;
  470. if c='"' then
  471. begin
  472. actasmpattern:=actasmpattern+'"';
  473. c:=asmgetchar;
  474. if c=newline then
  475. begin
  476. Message(scan_f_string_exceeds_line);
  477. break;
  478. end;
  479. end
  480. else break;
  481. end
  482. else
  483. begin
  484. actasmpattern:=actasmpattern+c;
  485. c:=asmgetchar;
  486. if c=newline then
  487. begin
  488. Message(scan_f_string_exceeds_line);
  489. break
  490. end;
  491. end;
  492. until false; { end repeat }
  493. end
  494. else break; { end if }
  495. end; { end while }
  496. token := AS_STRING;
  497. gettoken := token;
  498. exit;
  499. end;
  500. '$' : begin
  501. c:=asmgetchar;
  502. while c in ['0'..'9','A'..'F','a'..'f'] do
  503. begin
  504. actasmpattern := actasmpattern + c;
  505. c := asmgetchar;
  506. end;
  507. gettoken := AS_HEXNUM;
  508. exit;
  509. end;
  510. ',' : begin
  511. gettoken := AS_COMMA;
  512. c:=asmgetchar;
  513. exit;
  514. end;
  515. '[' : begin
  516. gettoken := AS_LBRACKET;
  517. c:=asmgetchar;
  518. exit;
  519. end;
  520. ']' : begin
  521. gettoken := AS_RBRACKET;
  522. c:=asmgetchar;
  523. exit;
  524. end;
  525. '(' : begin
  526. gettoken := AS_LPAREN;
  527. c:=asmgetchar;
  528. exit;
  529. end;
  530. ')' : begin
  531. gettoken := AS_RPAREN;
  532. c:=asmgetchar;
  533. exit;
  534. end;
  535. ':' : begin
  536. gettoken := AS_COLON;
  537. c:=asmgetchar;
  538. exit;
  539. end;
  540. '.' : begin
  541. gettoken := AS_DOT;
  542. c:=asmgetchar;
  543. exit;
  544. end;
  545. '+' : begin
  546. gettoken := AS_PLUS;
  547. c:=asmgetchar;
  548. exit;
  549. end;
  550. '-' : begin
  551. gettoken := AS_MINUS;
  552. c:=asmgetchar;
  553. exit;
  554. end;
  555. '*' : begin
  556. gettoken := AS_STAR;
  557. c:=asmgetchar;
  558. exit;
  559. end;
  560. '/' : begin
  561. gettoken := AS_SLASH;
  562. c:=asmgetchar;
  563. exit;
  564. end;
  565. '0'..'9': begin
  566. { this flag indicates if there was an error }
  567. { if so, then we use a default value instead.}
  568. errorflag := false;
  569. actasmpattern := c;
  570. c := asmgetchar;
  571. { Get the possible characters }
  572. while c in ['0'..'9','A'..'F','a'..'f'] do
  573. begin
  574. actasmpattern := actasmpattern + c;
  575. c:= asmgetchar;
  576. end;
  577. { Get ending character }
  578. uppervar(actasmpattern);
  579. c:=upcase(c);
  580. { possibly a binary number. }
  581. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  582. Begin
  583. { Delete the last binary specifier }
  584. delete(actasmpattern,length(actasmpattern),1);
  585. for j:=1 to length(actasmpattern) do
  586. if not (actasmpattern[j] in ['0','1']) then
  587. begin
  588. Message1(assem_e_error_in_binary_const,actasmpattern);
  589. errorflag := TRUE;
  590. end;
  591. { if error, then suppose a binary value of zero. }
  592. if errorflag then
  593. actasmpattern := '0';
  594. gettoken := AS_BINNUM;
  595. exit;
  596. end
  597. else
  598. Begin
  599. case c of
  600. 'O': Begin
  601. for j:=1 to length(actasmpattern) do
  602. if not (actasmpattern[j] in ['0'..'7']) then
  603. begin
  604. Message1(assem_e_error_in_octal_const,actasmpattern);
  605. errorflag := TRUE;
  606. end;
  607. { if error, then suppose an octal value of zero. }
  608. if errorflag then
  609. actasmpattern := '0';
  610. gettoken := AS_OCTALNUM;
  611. c := asmgetchar;
  612. exit;
  613. end;
  614. 'H': Begin
  615. for j:=1 to length(actasmpattern) do
  616. if not (actasmpattern[j] in ['0'..'9','A'..'F']) then
  617. begin
  618. Message1(assem_e_error_in_hex_const,actasmpattern);
  619. errorflag := TRUE;
  620. end;
  621. { if error, then suppose an hex value of zero. }
  622. if errorflag then
  623. actasmpattern := '0';
  624. gettoken := AS_HEXNUM;
  625. c := asmgetchar;
  626. exit;
  627. end;
  628. else { must be an integer number }
  629. begin
  630. for j:=1 to length(actasmpattern) do
  631. if not (actasmpattern[j] in ['0'..'9']) then
  632. begin
  633. Message1(assem_e_error_in_integer_const,actasmpattern);
  634. errorflag := TRUE;
  635. end;
  636. { if error, then suppose an int value of zero. }
  637. if errorflag then
  638. actasmpattern := '0';
  639. gettoken := AS_INTNUM;
  640. exit;
  641. end;
  642. end; { end case }
  643. end; { end if }
  644. end;
  645. ';','{',#13,newline : begin
  646. c:=asmgetchar;
  647. firsttoken := TRUE;
  648. gettoken:=AS_SEPARATOR;
  649. end;
  650. else
  651. Begin
  652. Message(scan_f_illegal_char);
  653. end;
  654. end; { end case }
  655. end; { end else if }
  656. end;
  657. {---------------------------------------------------------------------}
  658. { Routines for the output }
  659. {---------------------------------------------------------------------}
  660. { returns an appropriate ao_xxxx flag indicating the type }
  661. { of operand. }
  662. function findtype(Var Opr: TOperand): longint;
  663. Begin
  664. With Opr do
  665. Begin
  666. case operandtype of
  667. OPR_REFERENCE: Begin
  668. if assigned(ref.symbol) then
  669. { check if in local label list }
  670. { if so then it is considered }
  671. { as a displacement. }
  672. Begin
  673. if labellist.search(ref.symbol^) <> nil then
  674. findtype := ao_disp
  675. else
  676. findtype := ao_mem; { probably a mem ref. }
  677. end
  678. else
  679. findtype := ao_mem;
  680. end;
  681. OPR_CONSTANT: Begin
  682. { check if there is not already a default size }
  683. if opr.size <> S_NO then
  684. Begin
  685. findtype := _constsizes[opr.size];
  686. exit;
  687. end;
  688. if val < $ff then
  689. Begin
  690. findtype := ao_imm8;
  691. opr.size := S_B;
  692. end
  693. else if val < $ffff then
  694. Begin
  695. findtype := ao_imm16;
  696. opr.size := S_W;
  697. end
  698. else
  699. Begin
  700. findtype := ao_imm32;
  701. opr.size := S_L;
  702. end
  703. end;
  704. OPR_REGISTER: Begin
  705. findtype := _regtypes[reg];
  706. exit;
  707. end;
  708. OPR_SYMBOL: Begin
  709. findtype := ao_jumpabsolute;
  710. end;
  711. OPR_NONE: Begin
  712. findtype := 0;
  713. end;
  714. else
  715. Begin
  716. Message(assem_f_internal_error_in_findtype);
  717. end;
  718. end;
  719. end;
  720. end;
  721. Procedure ConcatLabeledInstr(var instr: TInstruction);
  722. Begin
  723. if (instr.getinstruction in [A_JO,A_JNO,A_JB,A_JC,A_JNAE,
  724. A_JNB,A_JNC,A_JAE,A_JE,A_JZ,A_JNE,A_JNZ,A_JBE,A_JNA,A_JNBE,
  725. A_JA,A_JS,A_JNS,A_JP,A_JPE,A_JNP,A_JPO,A_JL,A_JNGE,A_JNL,A_JGE,
  726. A_JLE,A_JNG,A_JNLE,A_JG,A_JCXZ,A_JECXZ,A_LOOP,A_LOOPZ,A_LOOPE,
  727. A_LOOPNZ,A_LOOPNE,A_MOV,A_JMP,A_CALL]) then
  728. Begin
  729. if instr.numops > 1 then
  730. Message(assem_e_invalid_labeled_opcode)
  731. else if instr.operands[1].operandtype <> OPR_LABINSTR then
  732. Message(assem_e_invalid_labeled_opcode)
  733. else if (instr.operands[1].operandtype = OPR_LABINSTR) and
  734. (instr.numops = 1) then
  735. if assigned(instr.operands[1].hl) then
  736. ConcatLabel(p,instr.getinstruction, instr.operands[1].hl)
  737. else
  738. Message(assem_f_internal_error_in_findtype);
  739. end
  740. else if instr.getinstruction = A_MOV then
  741. Begin
  742. { MOV to rel8 }
  743. end
  744. else
  745. Message(assem_e_invalid_operand);
  746. end;
  747. Procedure HandleExtend(var instr: TInstruction);
  748. { Handles MOVZX, MOVSX ... }
  749. var
  750. instruc: tasmop;
  751. opsize: topsize;
  752. Begin
  753. instruc:=instr.getinstruction;
  754. { return the old types ..}
  755. { these tokens still point to valid intel strings, }
  756. { but we must convert them to TRUE intel tokens }
  757. if instruc in [A_MOVSB,A_MOVSBL,A_MOVSBW,A_MOVSWL] then
  758. instruc := A_MOVSX;
  759. if instruc in [A_MOVZB,A_MOVZWL] then
  760. instruc := A_MOVZX;
  761. With instr do
  762. Begin
  763. if operands[1].size = S_B then
  764. Begin
  765. if operands[2].size = S_L then
  766. opsize := S_BL
  767. else
  768. if operands[2].size = S_W then
  769. opsize := S_BW
  770. else
  771. begin
  772. Message(assem_e_invalid_size_movzx);
  773. exit;
  774. end;
  775. end
  776. else
  777. if operands[1].size = S_W then
  778. Begin
  779. if operands[2].size = S_L then
  780. opsize := S_WL
  781. else
  782. begin
  783. Message(assem_e_invalid_size_movzx);
  784. exit;
  785. end;
  786. end
  787. else
  788. begin
  789. Message(assem_e_invalid_size_movzx);
  790. exit;
  791. end;
  792. if operands[1].operandtype = OPR_REGISTER then
  793. Begin
  794. if operands[2].operandtype <> OPR_REGISTER then
  795. Message(assem_e_invalid_opcode)
  796. else
  797. p^.concat(new(pai386,op_reg_reg(instruc,opsize,
  798. operands[1].reg,operands[2].reg)));
  799. end
  800. else
  801. if operands[1].operandtype = OPR_REFERENCE then
  802. Begin
  803. if operands[2].operandtype <> OPR_REGISTER then
  804. Message(assem_e_invalid_opcode)
  805. else
  806. p^.concat(new(pai386,op_ref_reg(instruc,opsize,
  807. newreference(operands[1].ref),operands[2].reg)));
  808. end
  809. end; { end with }
  810. end;
  811. Procedure ConcatOpCode(var instr: TInstruction);
  812. {*********************************************************************}
  813. { First Pass: }
  814. { if instr = Lxxx with a 16bit offset, we emit an error. }
  815. { If the instruction is INS,IN,OUT,OUTS,RCL,ROL,RCR,ROR, }
  816. { SAL,SAR,SHL,SHR,SHLD,SHRD,DIV,IDIV,BT,BTC,BTR,BTS,INT, }
  817. { RET,ENTER,SCAS,CMPS,STOS,LODS,FNSTSW,FSTSW. }
  818. { set up the optypes variables manually, as well as setting }
  819. { operand sizes. }
  820. { Second pass: }
  821. { Check if the combination of opcodes and operands are valid, using }
  822. { the opcode table. }
  823. { Third pass: }
  824. { If there was no error on the 2nd pass , then we check the }
  825. { following: }
  826. { - If this is a 0 operand opcode }
  827. { we verify if it is a string opcode, if so we emit a size also}
  828. { otherwise simply emit the opcode by itself. }
  829. { - If this is a 1 operand opcode, and it is a reference, we make }
  830. { sure that the operand size is valid; we emit the opcode. }
  831. { - If this is a two operand opcode }
  832. { o if the opcode is MOVSX or MOVZX then we handle it specially }
  833. { o we check the operand types (most important combinations): }
  834. { if reg,reg we make sure that both registers are of the }
  835. { same size. }
  836. { if reg,ref or ref,reg we check if the symbol name is }
  837. { assigned, if so a size must be specified and compared }
  838. { to the register size, both must be equal. If there is }
  839. { no symbol name, then we check : }
  840. { if refsize = NO_SIZE then OPCODE_SIZE = regsize }
  841. { else if refsize = regsize then OPCODE_SIZE = regsize}
  842. { else error. }
  843. { if no_error emit the opcode. }
  844. { if ref,const or const,ref if ref does not have any size }
  845. { then error, otherwise emit the opcode. }
  846. { - If this is a three operand opcode: }
  847. { imul,shld,and shrd -> check them manually. }
  848. {*********************************************************************}
  849. var
  850. fits : boolean;
  851. i: longint;
  852. opsize: topsize;
  853. optyp1, optyp2, optyp3: longint;
  854. instruc: tasmop;
  855. Begin
  856. fits := FALSE;
  857. for i:=1 to instr.numops do
  858. Begin
  859. case instr.operands[i].operandtype of
  860. OPR_REGISTER: instr.operands[i].size :=
  861. _regsizes[instr.operands[i].reg];
  862. end; { end case }
  863. end; { endif }
  864. { setup specific instructions for first pass }
  865. instruc := instr.getinstruction;
  866. if (instruc in [A_LEA,A_LDS,A_LSS,A_LES,A_LFS,A_LGS]) then
  867. Begin
  868. if instr.operands[1].size <> S_L then
  869. Begin
  870. Message(assem_e_16bit_base_in_32bit_segment);
  871. exit;
  872. end; { endif }
  873. end;
  874. With instr do
  875. Begin
  876. for i:=1 to numops do
  877. Begin
  878. With operands[i] do
  879. Begin
  880. { check for 16-bit bases/indexes and emit an error. }
  881. { we cannot only emit a warning since gas does not }
  882. { accept 16-bit indexes and bases. }
  883. if (operandtype = OPR_REFERENCE) and
  884. ((ref.base <> R_NO) or
  885. (ref.index <> R_NO)) then
  886. Begin
  887. { index or base defined. }
  888. if (ref.base <> R_NO) then
  889. Begin
  890. if not (ref.base in
  891. [R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESI,R_EDI,R_ESP]) then
  892. Message(assem_e_16bit_base_in_32bit_segment);
  893. end;
  894. { index or base defined. }
  895. if (ref.index <> R_NO) then
  896. Begin
  897. if not (ref.index in
  898. [R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESI,R_EDI,R_ESP]) then
  899. Message(assem_e_16bit_index_in_32bit_segment);
  900. end;
  901. end;
  902. { Check for constants without bases/indexes in memory }
  903. { references. }
  904. if (operandtype = OPR_REFERENCE) and
  905. (ref.base = R_NO) and
  906. (ref.index = R_NO) and
  907. (ref.symbol = nil) and
  908. (ref.offset <> 0) then
  909. Begin
  910. ref.isintvalue := TRUE;
  911. Message(assem_e_const_ref_not_allowed);
  912. end;
  913. opinfo := findtype(operands[i]);
  914. end; { end with }
  915. end; {endfor}
  916. { TAKE CARE OF SPECIAL OPCODES, TAKE CARE OF THEM INDIVUALLY. }
  917. { ALL THE REST ARE TAKEN CARE BY OPCODE TABLE AND THIRD PASS. }
  918. if instruc = A_FST then
  919. Begin
  920. end
  921. else
  922. if instruc = A_FILD then
  923. Begin
  924. end
  925. else
  926. if instruc = A_FLD then
  927. Begin
  928. {A_FLDS,A_FLDL,A_FLDT}
  929. end
  930. else
  931. if instruc = A_FIST then
  932. Begin
  933. {A_FISTQ,A_FISTS,A_FISTL}
  934. end
  935. else
  936. if instruc = A_FWAIT then
  937. FWaitWarning
  938. else
  939. if instruc = A_MOVSX then
  940. Begin
  941. { change the instruction to conform to GAS }
  942. if operands[1].size = S_W then
  943. Begin
  944. addinstr(A_MOVSBW)
  945. end
  946. else
  947. if operands[1].size = S_L then
  948. Begin
  949. if operands[2].size = S_B then
  950. addinstr(A_MOVSBL)
  951. else
  952. addinstr(A_MOVSWL);
  953. end;
  954. instruc := getinstruction; { reload instruction }
  955. end
  956. else
  957. if instruc = A_MOVZX then
  958. Begin
  959. { change the instruction to conform to GAS }
  960. if operands[1].size = S_W then
  961. Begin
  962. addinstr(A_MOVZB)
  963. end
  964. else
  965. if operands[1].size = S_L then
  966. Begin
  967. if operands[2].size = S_B then
  968. addinstr(A_MOVZB)
  969. else
  970. addinstr(A_MOVZWL);
  971. end;
  972. instruc := getinstruction; { reload instruction }
  973. end
  974. else
  975. if (instruc in [A_BT,A_BTC,A_BTR,A_BTS]) then
  976. Begin
  977. if numops = 2 then
  978. Begin
  979. if (operands[2].operandtype = OPR_CONSTANT)
  980. and (operands[2].val <= $ff) then
  981. Begin
  982. operands[2].opinfo := ao_imm8;
  983. { no operand size if using constant. }
  984. operands[2].size := S_NO;
  985. fits := TRUE;
  986. end
  987. end
  988. else
  989. Begin
  990. Message(assem_e_invalid_opcode_and_operand);
  991. exit;
  992. end;
  993. end
  994. else
  995. if instruc = A_ENTER then
  996. Begin
  997. if numops =2 then
  998. Begin
  999. if (operands[1].operandtype = OPR_CONSTANT) and
  1000. (operands[1].val <= $ffff) then
  1001. Begin
  1002. operands[1].opinfo := ao_imm16;
  1003. end { endif }
  1004. end { endif }
  1005. else
  1006. Begin
  1007. Message(assem_e_invalid_opcode_and_operand);
  1008. exit;
  1009. end
  1010. end { endif }
  1011. else
  1012. { Handle special opcodes for the opcode }
  1013. { table. Set them up correctly. }
  1014. if (instruc in [A_IN,A_INS]) then
  1015. Begin
  1016. if numops =2 then
  1017. Begin
  1018. if (operands[2].operandtype = OPR_REGISTER) and (operands[2].reg = R_DX)
  1019. then
  1020. Begin
  1021. operands[2].opinfo := ao_inoutportreg;
  1022. if (operands[1].operandtype = OPR_REGISTER) and
  1023. (operands[1].reg in [R_EAX,R_AX,R_AL]) and
  1024. (instruc = A_IN) then
  1025. Begin
  1026. operands[1].opinfo := ao_acc;
  1027. end
  1028. end
  1029. else
  1030. if (operands[2].operandtype = OPR_CONSTANT) and (operands[2].val <= $ff)
  1031. and (instruc = A_IN) then
  1032. Begin
  1033. operands[2].opinfo := ao_imm8;
  1034. operands[2].size := S_B;
  1035. if (operands[1].operandtype = OPR_REGISTER) and
  1036. (operands[1].reg in [R_EAX,R_AX,R_AL]) and
  1037. (instruc = A_IN) then
  1038. Begin
  1039. operands[1].opinfo := ao_acc;
  1040. end
  1041. end;
  1042. end
  1043. else
  1044. if not ((numops=0) and (instruc=A_INS)) then
  1045. Begin
  1046. Message(assem_e_invalid_opcode_and_operand);
  1047. exit;
  1048. end;
  1049. end
  1050. else
  1051. if (instruc in [A_OUT,A_OUTS]) then
  1052. Begin
  1053. if numops =2 then
  1054. Begin
  1055. if (operands[1].operandtype = OPR_REGISTER) and (operands[1].reg = R_DX)
  1056. then
  1057. Begin
  1058. operands[1].opinfo := ao_inoutportreg;
  1059. if (operands[2].operandtype = OPR_REGISTER) and
  1060. (operands[2].reg in [R_EAX,R_AX,R_AL]) and
  1061. (instruc = A_OUT) then
  1062. Begin
  1063. operands[2].opinfo := ao_acc;
  1064. fits := TRUE;
  1065. end
  1066. end
  1067. else
  1068. if (operands[1].operandtype = OPR_CONSTANT) and (operands[1].val <= $ff)
  1069. and (instruc = A_OUT) then
  1070. Begin
  1071. operands[1].opinfo := ao_imm8;
  1072. operands[1].size := S_B;
  1073. if (operands[2].operandtype = OPR_REGISTER) and
  1074. (operands[2].reg in [R_EAX,R_AX,R_AL]) and
  1075. (instruc = A_OUT) then
  1076. Begin
  1077. operands[2].opinfo := ao_acc;
  1078. fits := TRUE;
  1079. end
  1080. end;
  1081. end
  1082. else
  1083. if not ((numops=0) and (instruc=A_OUTS)) then
  1084. Begin
  1085. Message(assem_e_invalid_opcode_and_operand);
  1086. exit;
  1087. end;
  1088. end
  1089. else
  1090. if instruc in [A_RCL,A_RCR,A_ROL,A_ROR,A_SAL,A_SAR,A_SHL,A_SHR] then
  1091. { if RCL,ROL,... }
  1092. Begin
  1093. if numops =2 then
  1094. Begin
  1095. if (operands[2].operandtype = OPR_REGISTER) and (operands[2].reg = R_CL)
  1096. then
  1097. Begin
  1098. operands[2].opinfo := ao_shiftcount
  1099. end
  1100. else
  1101. if (operands[2].operandtype = OPR_CONSTANT) and
  1102. (operands[2].val <= $ff) then
  1103. Begin
  1104. operands[2].opinfo := ao_imm8;
  1105. operands[2].size := S_B;
  1106. end;
  1107. end
  1108. else { if numops = 2 }
  1109. Begin
  1110. Message(assem_e_invalid_opcode_and_operand);
  1111. exit;
  1112. end;
  1113. end
  1114. { endif ROL,RCL ... }
  1115. else
  1116. if instruc in [A_DIV, A_IDIV] then
  1117. Begin
  1118. if (operands[1].operandtype = OPR_REGISTER) and
  1119. (operands[1].reg in [R_AL,R_AX,R_EAX]) then
  1120. operands[1].opinfo := ao_acc;
  1121. end
  1122. else
  1123. if (instruc = A_FNSTSW) or (instruc = A_FSTSW) then
  1124. Begin
  1125. if numops = 1 then
  1126. Begin
  1127. if (operands[1].operandtype = OPR_REGISTER) and
  1128. (operands[1].reg = R_AX) then
  1129. operands[1].opinfo := ao_acc;
  1130. end
  1131. else
  1132. Begin
  1133. Message(assem_e_invalid_opcode_and_operand);
  1134. exit;
  1135. end;
  1136. end
  1137. else
  1138. if (instruc = A_SHLD) or (instruc = A_SHRD) then
  1139. { these instruction are fully parsed individually on pass three }
  1140. { so we just do a summary checking here. }
  1141. Begin
  1142. if numops = 3 then
  1143. Begin
  1144. if (operands[3].operandtype = OPR_CONSTANT)
  1145. and (operands[3].val <= $ff) then
  1146. Begin
  1147. operands[3].opinfo := ao_imm8;
  1148. operands[3].size := S_B;
  1149. end;
  1150. end
  1151. else
  1152. Begin
  1153. Message(assem_e_invalid_opcode_and_operand);
  1154. exit;
  1155. end;
  1156. end
  1157. else
  1158. if instruc = A_INT then
  1159. Begin
  1160. if numops = 1 then
  1161. Begin
  1162. if (operands[1].operandtype = OPR_CONSTANT) and
  1163. (operands[1].val <= $ff) then
  1164. operands[1].opinfo := ao_imm8;
  1165. end
  1166. end
  1167. else
  1168. if instruc = A_RET then
  1169. Begin
  1170. if numops =1 then
  1171. Begin
  1172. if (operands[1].operandtype = OPR_CONSTANT) and
  1173. (operands[1].val <= $ffff) then
  1174. operands[1].opinfo := ao_imm16;
  1175. end
  1176. end; { endif }
  1177. { all string instructions have default memory }
  1178. { location which are ignored. Take care of }
  1179. { those. }
  1180. { Here could be added the code for segment }
  1181. { overrides. }
  1182. if instruc in [A_SCAS,A_CMPS,A_STOS,A_LODS] then
  1183. Begin
  1184. if numops =1 then
  1185. Begin
  1186. if (operands[1].operandtype = OPR_REFERENCE) and
  1187. (assigned(operands[1].ref.symbol)) then
  1188. Freemem(operands[1].ref.symbol,length(operands[1].ref.symbol^)+1);
  1189. operands[1].operandtype := OPR_NONE;
  1190. numops := 0;
  1191. end;
  1192. end; { endif }
  1193. if instruc in [A_INS,A_MOVS,A_OUTS] then
  1194. Begin
  1195. if numops =2 then
  1196. Begin
  1197. if (operands[1].operandtype = OPR_REFERENCE) and
  1198. (assigned(operands[1].ref.symbol)) then
  1199. Freemem(operands[1].ref.symbol,length(operands[1].ref.symbol^)+1);
  1200. if (operands[2].operandtype = OPR_REFERENCE) and
  1201. (assigned(operands[2].ref.symbol)) then
  1202. Freemem(operands[2].ref.symbol,length(operands[1].ref.symbol^)+1);
  1203. operands[1].operandtype := OPR_NONE;
  1204. operands[2].operandtype := OPR_NONE;
  1205. numops := 0;
  1206. end;
  1207. end;
  1208. { handle parameter for segment overrides }
  1209. if instruc = A_XLAT then
  1210. Begin
  1211. { handle special TP syntax case for XLAT }
  1212. { here we accept XLAT, XLATB and XLAT m8 }
  1213. if (numops = 1) or (numops = 0) then
  1214. Begin
  1215. if (operands[1].operandtype = OPR_REFERENCE) and
  1216. (assigned(operands[1].ref.symbol)) then
  1217. Freemem(operands[1].ref.symbol,length(operands[1].ref.symbol^)+1);
  1218. operands[1].operandtype := OPR_NONE;
  1219. numops := 0;
  1220. { always a byte for XLAT }
  1221. instr.stropsize := S_B;
  1222. end;
  1223. end;
  1224. { swap the destination and source }
  1225. { to put in AT&T style direction }
  1226. { only if there are 2/3 operand }
  1227. { numbers. }
  1228. if (instruc <> A_ENTER) then
  1229. SwapOperands(instr);
  1230. { copy them to local variables }
  1231. { for faster access }
  1232. optyp1:=operands[1].opinfo;
  1233. optyp2:=operands[2].opinfo;
  1234. optyp3:=operands[3].opinfo;
  1235. end; { end with }
  1236. { after reading the operands }
  1237. { search the instruction }
  1238. { setup startvalue from cache }
  1239. if ins_cache[instruc]<>-1 then
  1240. i:=ins_cache[instruc]
  1241. else i:=0;
  1242. { this makes cpu.pp uncompilable, but i think this code should be }
  1243. { inserted in the system unit anyways. }
  1244. if (instruc >= lastop_in_table) then
  1245. { ((cs_compilesystem in aktswitches) or (aktoptprocessor > systems.i386)) then }
  1246. begin
  1247. Message(assem_w_opcode_not_in_table);
  1248. fits:=true;
  1249. end
  1250. else while not(fits) do
  1251. begin
  1252. { set the instruction cache, if the instruction }
  1253. { occurs the first time }
  1254. if (it[i].i=instruc) and (ins_cache[instruc]=-1) then
  1255. ins_cache[instruc]:=i;
  1256. if (it[i].i=instruc) and (instr.numops=it[i].ops) then
  1257. begin
  1258. { first fit }
  1259. case instr.numops of
  1260. 0 : begin
  1261. fits:=true;
  1262. break;
  1263. end;
  1264. 1 :
  1265. Begin
  1266. if (optyp1 and it[i].o1)<>0 then
  1267. Begin
  1268. fits:=true;
  1269. break;
  1270. end;
  1271. { I consider sign-extended 8bit value to }
  1272. { be equal to immediate 8bit therefore }
  1273. { convert... }
  1274. if (optyp1 = ao_imm8) then
  1275. Begin
  1276. { check if this is a simple sign extend. }
  1277. if (it[i].o1<>ao_imm8s) then
  1278. Begin
  1279. fits:=true;
  1280. break;
  1281. end;
  1282. end;
  1283. end;
  1284. 2 : if ((optyp1 and it[i].o1)<>0) and
  1285. ((optyp2 and it[i].o2)<>0) then
  1286. Begin
  1287. fits:=true;
  1288. break;
  1289. end
  1290. { if the operands can be swaped }
  1291. { then swap them }
  1292. else if ((it[i].m and af_d)<>0) and
  1293. ((optyp1 and it[i].o2)<>0) and
  1294. ((optyp2 and it[i].o1)<>0) then
  1295. begin
  1296. { swap the destination and source }
  1297. { to put in AT&T style direction }
  1298. { What does this mean !!!! ???????????????????????? }
  1299. { if (output_format in [of_o,of_att]) then }
  1300. { ???????????? }
  1301. { SwapOperands(instr); }
  1302. fits:=true;
  1303. break;
  1304. end;
  1305. 3 : if ((optyp1 and it[i].o1)<>0) and
  1306. ((optyp2 and it[i].o2)<>0) and
  1307. ((optyp3 and it[i].o3)<>0) then
  1308. Begin
  1309. fits:=true;
  1310. break;
  1311. end;
  1312. end; { end case }
  1313. end; { endif }
  1314. if it[i].i=A_NONE then
  1315. begin
  1316. { NO MATCH! }
  1317. Message(assem_e_invalid_opcode_and_operand);
  1318. exit;
  1319. end;
  1320. inc(i);
  1321. end; { end while }
  1322. { We add the opcode to the opcode linked list }
  1323. if fits then
  1324. Begin
  1325. if instr.getprefix <> A_NONE then
  1326. Begin
  1327. p^.concat(new(pai386,op_none(instr.getprefix,S_NO)));
  1328. end;
  1329. case instr.numops of
  1330. 0:
  1331. if instr.stropsize <> S_NO then
  1332. { is this a string operation opcode or xlat then check }
  1333. { the size of the operation. }
  1334. p^.concat(new(pai386,op_none(instruc,instr.stropsize)))
  1335. else
  1336. p^.concat(new(pai386,op_none(instruc,S_NO)));
  1337. 1: Begin
  1338. case instr.operands[1].operandtype of
  1339. { all one operand opcodes with constant have no defined sizes }
  1340. { at least that is what it seems in the tasm 2.0 manual. }
  1341. OPR_CONSTANT: p^.concat(new(pai386,op_const(instruc,
  1342. S_NO, instr.operands[1].val)));
  1343. OPR_REGISTER: if instruc in [A_INC,A_DEC, A_NEG,A_NOT] then
  1344. Begin
  1345. p^.concat(new(pai386,op_reg(instruc,
  1346. instr.operands[1].size,instr.operands[1].reg)));
  1347. end
  1348. else
  1349. p^.concat(new(pai386,op_reg(instruc,
  1350. S_NO,instr.operands[1].reg)));
  1351. { this is where it gets a bit more complicated... }
  1352. OPR_REFERENCE:
  1353. if instr.operands[1].size <> S_NO then
  1354. Begin
  1355. p^.concat(new(pai386,op_ref(instruc,
  1356. instr.operands[1].size,newreference(instr.operands[1].ref))));
  1357. end
  1358. else
  1359. Begin
  1360. { special jmp and call case with }
  1361. { symbolic references. }
  1362. if instruc in [A_CALL,A_JMP] then
  1363. Begin
  1364. p^.concat(new(pai386,op_ref(instruc,
  1365. S_NO,newreference(instr.operands[1].ref))));
  1366. end
  1367. else
  1368. Message(assem_e_invalid_opcode_and_operand);
  1369. end;
  1370. OPR_SYMBOL: Begin
  1371. p^.concat(new(pai386,op_csymbol(instruc,
  1372. instr.stropsize, newcsymbol(instr.operands[1].symbol^,0))));
  1373. End;
  1374. OPR_NONE: Begin
  1375. Message(assem_f_internal_error_in_concatopcode);
  1376. end;
  1377. else
  1378. Begin
  1379. Message(assem_f_internal_error_in_concatopcode);
  1380. end;
  1381. end;
  1382. end;
  1383. 2:
  1384. Begin
  1385. if instruc in [A_MOVSX,A_MOVZX,A_MOVSB,A_MOVSBL,A_MOVSBW,
  1386. A_MOVSWL,A_MOVZB,A_MOVZWL] then
  1387. { movzx and movsx }
  1388. HandleExtend(instr)
  1389. else
  1390. { other instructions }
  1391. Begin
  1392. With instr do
  1393. Begin
  1394. { source }
  1395. opsize := operands[1].size;
  1396. case operands[1].operandtype of
  1397. { reg,reg }
  1398. { reg,ref }
  1399. OPR_REGISTER:
  1400. Begin
  1401. case operands[2].operandtype of
  1402. OPR_REGISTER:
  1403. { see info in ratti386.pas, about the problem }
  1404. { which can cause gas here. }
  1405. if (opsize = operands[2].size) then
  1406. begin
  1407. p^.concat(new(pai386,op_reg_reg(instruc,
  1408. opsize,operands[1].reg,operands[2].reg)));
  1409. end
  1410. else
  1411. { these do not require any size specification. }
  1412. if (instruc in [A_IN,A_OUT,A_SAL,A_SAR,A_SHL,A_SHR,A_ROL,
  1413. A_ROR,A_RCR,A_RCL]) then
  1414. { outs and ins are already taken care by }
  1415. { the first pass. }
  1416. p^.concat(new(pai386,op_reg_reg(instruc,
  1417. S_NO,operands[1].reg,operands[2].reg)))
  1418. else
  1419. Begin
  1420. Message(assem_e_invalid_opcode_and_operand);
  1421. end;
  1422. OPR_REFERENCE:
  1423. { variable name. }
  1424. { here we must check the instruction type }
  1425. { before deciding if to use and compare }
  1426. { any sizes. }
  1427. if assigned(operands[2].ref.symbol) then
  1428. Begin
  1429. if (opsize = operands[2].size) or (instruc in
  1430. [A_RCL,A_RCR,A_ROL,A_ROR,A_SAL,A_SAR,A_SHR,A_SHL]) then
  1431. p^.concat(new(pai386,op_reg_ref(instruc,
  1432. opsize,operands[1].reg,newreference(operands[2].ref))))
  1433. else
  1434. Message(assem_e_invalid_size_in_ref);
  1435. end
  1436. else
  1437. Begin
  1438. { register reference }
  1439. { possiblities:1) local variable which }
  1440. { has been replaced by bp and offset }
  1441. { in this case size should be valid }
  1442. { 2) Indirect register }
  1443. { adressing, 1st operand determines }
  1444. { size. }
  1445. if (opsize = operands[2].size) or (operands[2].size = S_NO) then
  1446. p^.concat(new(pai386,op_reg_ref(instruc,
  1447. opsize,operands[1].reg,newreference(operands[2].ref))))
  1448. else
  1449. Message(assem_e_invalid_size_in_ref);
  1450. end;
  1451. OPR_CONSTANT: { const,reg }
  1452. Begin { OUT const,reg }
  1453. if (instruc = A_OUT) and (opsize = S_B) then
  1454. p^.concat(new(pai386,op_reg_const(instruc,
  1455. opsize,operands[1].reg,operands[2].val)))
  1456. else
  1457. Message(assem_e_invalid_size_in_ref);
  1458. end;
  1459. else { else case }
  1460. Begin
  1461. Message(assem_f_internal_error_in_concatopcode);
  1462. end;
  1463. end; { end inner case }
  1464. end;
  1465. { const,reg }
  1466. { const,const }
  1467. { const,ref }
  1468. OPR_CONSTANT:
  1469. case instr.operands[2].operandtype of
  1470. { constant, constant does not have a specific size. }
  1471. OPR_CONSTANT:
  1472. p^.concat(new(pai386,op_const_const(instruc,
  1473. S_NO,operands[1].val,operands[2].val)));
  1474. OPR_REFERENCE:
  1475. Begin
  1476. if (operands[1].val <= $ff) and
  1477. (operands[2].size in [S_B,S_W,S_L,
  1478. S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1479. p^.concat(new(pai386,op_const_ref(instruc,
  1480. operands[2].size,operands[1].val,
  1481. newreference(operands[2].ref))))
  1482. else
  1483. if (operands[1].val <= $ffff) and
  1484. (operands[2].size in [S_W,S_L,
  1485. S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1486. p^.concat(new(pai386,op_const_ref(instruc,
  1487. operands[2].size,operands[1].val,
  1488. newreference(operands[2].ref))))
  1489. else
  1490. if (operands[1].val <= $7fffffff) and
  1491. (operands[2].size in [S_L,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1492. p^.concat(new(pai386,op_const_ref(instruc,
  1493. operands[2].size,operands[1].val,
  1494. newreference(operands[2].ref))))
  1495. else
  1496. Message(assem_e_invalid_size_in_ref);
  1497. end;
  1498. OPR_REGISTER:
  1499. Begin
  1500. { size of opcode determined by register }
  1501. if (operands[1].val <= $ff) and
  1502. (operands[2].size in [S_B,S_W,S_L,S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1503. p^.concat(new(pai386,op_const_reg(instruc,
  1504. operands[2].size,operands[1].val,
  1505. operands[2].reg)))
  1506. else
  1507. if (operands[1].val <= $ffff) and
  1508. (operands[2].size in [S_W,S_L,S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1509. p^.concat(new(pai386,op_const_reg(instruc,
  1510. operands[2].size,operands[1].val,
  1511. operands[2].reg)))
  1512. else
  1513. if (operands[1].val <= $7fffffff) and
  1514. (operands[2].size in [S_L,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1515. p^.concat(new(pai386,op_const_reg(instruc,
  1516. operands[2].size,operands[1].val,
  1517. operands[2].reg)))
  1518. else
  1519. Message(assem_e_invalid_opcode_size);
  1520. end;
  1521. else
  1522. Begin
  1523. Message(assem_f_internal_error_in_concatopcode);
  1524. end;
  1525. end; { end case }
  1526. { ref,reg }
  1527. { ref,ref }
  1528. OPR_REFERENCE:
  1529. case instr.operands[2].operandtype of
  1530. OPR_REGISTER:
  1531. if assigned(operands[1].ref.symbol) then
  1532. { global variable }
  1533. Begin
  1534. if instruc in [A_LEA,A_LDS,A_LES,A_LFS,A_LGS,A_LSS]
  1535. then
  1536. p^.concat(new(pai386,op_ref_reg(instruc,
  1537. S_NO,newreference(operands[1].ref),
  1538. operands[2].reg)))
  1539. else
  1540. if (opsize = operands[2].size) then
  1541. p^.concat(new(pai386,op_ref_reg(instruc,
  1542. opsize,newreference(operands[1].ref),
  1543. operands[2].reg)))
  1544. else
  1545. Begin
  1546. Message(assem_e_invalid_opcode_and_operand);
  1547. end;
  1548. end
  1549. else
  1550. Begin
  1551. { register reference }
  1552. { possiblities:1) local variable which }
  1553. { has been replaced by bp and offset }
  1554. { in this case size should be valid }
  1555. { 2) Indirect register }
  1556. { adressing, 2nd operand determines }
  1557. { size. }
  1558. if (opsize = operands[2].size) or (opsize = S_NO) then
  1559. Begin
  1560. p^.concat(new(pai386,op_ref_reg(instruc,
  1561. operands[2].size,newreference(operands[1].ref),
  1562. operands[2].reg)));
  1563. end
  1564. else
  1565. Message(assem_e_invalid_size_in_ref);
  1566. end;
  1567. OPR_REFERENCE: { special opcodes }
  1568. p^.concat(new(pai386,op_ref_ref(instruc,
  1569. opsize,newreference(operands[1].ref),
  1570. newreference(operands[2].ref))));
  1571. else
  1572. Begin
  1573. Message(assem_f_internal_error_in_concatopcode);
  1574. end;
  1575. end; { end inner case }
  1576. end; { end case }
  1577. end; { end with }
  1578. end; {end if movsx... }
  1579. end;
  1580. 3: Begin
  1581. { only imul, shld and shrd }
  1582. { middle must be a register }
  1583. if (instruc in [A_SHLD,A_SHRD]) and (instr.operands[2].operandtype =
  1584. OPR_REGISTER) then
  1585. Begin
  1586. case instr.operands[2].size of
  1587. S_W: if instr.operands[1].operandtype = OPR_CONSTANT then
  1588. Begin
  1589. if instr.operands[1].val <= $ff then
  1590. Begin
  1591. if instr.operands[3].size in [S_W] then
  1592. Begin
  1593. case instr.operands[3].operandtype of
  1594. OPR_REFERENCE: { MISSING !!!! } ;
  1595. OPR_REGISTER: p^.concat(new(pai386,
  1596. op_const_reg_reg(instruc, S_W,
  1597. instr.operands[1].val, instr.operands[2].reg,
  1598. instr.operands[3].reg)));
  1599. else
  1600. Message(assem_e_invalid_opcode_and_operand);
  1601. Message(assem_e_invalid_opcode_and_operand);
  1602. end;
  1603. end
  1604. else
  1605. Message(assem_e_invalid_opcode_and_operand);
  1606. end;
  1607. end
  1608. else
  1609. Message(assem_e_invalid_opcode_and_operand);
  1610. S_L: if instr.operands[1].operandtype = OPR_CONSTANT then
  1611. Begin
  1612. if instr.operands[1].val <= $ff then
  1613. Begin
  1614. if instr.operands[3].size in [S_L] then
  1615. Begin
  1616. case instr.operands[3].operandtype of
  1617. OPR_REFERENCE: { MISSING !!!! } ;
  1618. OPR_REGISTER: p^.concat(new(pai386,
  1619. op_const_reg_reg(instruc, S_L,
  1620. instr.operands[1].val, instr.operands[2].reg,
  1621. instr.operands[3].reg)));
  1622. else
  1623. Message(assem_e_invalid_opcode_and_operand);
  1624. end;
  1625. end
  1626. else
  1627. Message(assem_e_invalid_opcode_and_operand);
  1628. end;
  1629. end
  1630. else
  1631. Message(assem_e_invalid_opcode_and_operand);
  1632. else
  1633. Message(assem_e_invalid_opcode_and_operand);
  1634. end; { end case }
  1635. end
  1636. else
  1637. if (instruc in [A_IMUL]) and (instr.operands[3].operandtype
  1638. = OPR_REGISTER) then
  1639. Begin
  1640. case instr.operands[3].size of
  1641. S_W: if instr.operands[1].operandtype = OPR_CONSTANT then
  1642. Begin
  1643. if instr.operands[1].val <= $ffff then
  1644. Begin
  1645. if instr.operands[2].size in [S_W] then
  1646. Begin
  1647. case instr.operands[2].operandtype of
  1648. OPR_REFERENCE: { MISSING !!!! } ;
  1649. OPR_REGISTER: p^.concat(new(pai386,
  1650. op_const_reg_reg(instruc, S_W,
  1651. instr.operands[1].val, instr.operands[2].reg,
  1652. instr.operands[3].reg)));
  1653. else
  1654. Message(assem_e_invalid_opcode_and_operand);
  1655. end; { end case }
  1656. end
  1657. else
  1658. Message(assem_e_invalid_opcode_and_operand);
  1659. end;
  1660. end
  1661. else
  1662. Message(assem_e_invalid_opcode_and_operand);
  1663. S_L: if instr.operands[1].operandtype = OPR_CONSTANT then
  1664. Begin
  1665. if instr.operands[1].val <= $7fffffff then
  1666. Begin
  1667. if instr.operands[2].size in [S_L] then
  1668. Begin
  1669. case instr.operands[2].operandtype of
  1670. OPR_REFERENCE: { MISSING !!!! } ;
  1671. OPR_REGISTER: p^.concat(new(pai386,
  1672. op_const_reg_reg(instruc, S_L,
  1673. instr.operands[1].val, instr.operands[2].reg,
  1674. instr.operands[3].reg)));
  1675. else
  1676. Message(assem_e_invalid_opcode_and_operand);
  1677. end; { end case }
  1678. end
  1679. else
  1680. Message(assem_e_invalid_opcode_and_operand);
  1681. end;
  1682. end
  1683. else
  1684. Message(assem_e_invalid_opcode_and_operand);
  1685. else
  1686. Message(assem_e_invalid_middle_sized_operand);
  1687. end; { end case }
  1688. end { endif }
  1689. else
  1690. Message(assem_e_invalid_three_operand_opcode);
  1691. end;
  1692. end; { end case }
  1693. end;
  1694. end;
  1695. {---------------------------------------------------------------------}
  1696. { Routines for the parsing }
  1697. {---------------------------------------------------------------------}
  1698. procedure consume(t : tinteltoken);
  1699. begin
  1700. if t<>actasmtoken then
  1701. Message(assem_e_syntax_error);
  1702. actasmtoken:=gettoken;
  1703. { if the token must be ignored, then }
  1704. { get another token to parse. }
  1705. if actasmtoken = AS_NONE then
  1706. actasmtoken := gettoken;
  1707. end;
  1708. function findregister(const s : string): tregister;
  1709. {*********************************************************************}
  1710. { FUNCTION findregister(s: string):tasmop; }
  1711. { Description: Determines if the s string is a valid register, }
  1712. { if so returns correct tregister token, or R_NO if not found. }
  1713. {*********************************************************************}
  1714. var
  1715. i: tregister;
  1716. begin
  1717. findregister := R_NO;
  1718. for i:=firstreg to lastreg do
  1719. if s = iasmregs[i] then
  1720. Begin
  1721. findregister := i;
  1722. exit;
  1723. end;
  1724. end;
  1725. function findoverride(const s: string; var reg:tregister): boolean;
  1726. var
  1727. i: byte;
  1728. begin
  1729. findoverride := FALSE;
  1730. reg := R_NO;
  1731. for i:=0 to _count_asmoverrides do
  1732. Begin
  1733. if s = _asmoverrides[i] then
  1734. begin
  1735. reg := _overridetokens[i];
  1736. findoverride := TRUE;
  1737. exit;
  1738. end;
  1739. end;
  1740. end;
  1741. function findprefix(const s: string; var token: tasmop): boolean;
  1742. var i: byte;
  1743. Begin
  1744. findprefix := FALSE;
  1745. for i:=0 to _count_asmprefixes do
  1746. Begin
  1747. if s = _asmprefixes[i] then
  1748. begin
  1749. token := _prefixtokens[i];
  1750. findprefix := TRUE;
  1751. exit;
  1752. end;
  1753. end;
  1754. end;
  1755. function findsegment(const s:string): tregister;
  1756. {*********************************************************************}
  1757. { FUNCTION findsegment(s: string):tasmop; }
  1758. { Description: Determines if the s string is a valid segment register}
  1759. { if so returns correct tregister token, or R_NO if not found. }
  1760. {*********************************************************************}
  1761. var
  1762. i: tregister;
  1763. Begin
  1764. findsegment := R_DEFAULT_SEG;
  1765. for i:=firstsreg to lastsreg do
  1766. if s = iasmregs[i] then
  1767. Begin
  1768. findsegment := i;
  1769. exit;
  1770. end;
  1771. end;
  1772. function findopcode(const s: string): tasmop;
  1773. {*********************************************************************}
  1774. { FUNCTION findopcode(s: string): tasmop; }
  1775. { Description: Determines if the s string is a valid opcode }
  1776. { if so returns correct tasmop token. }
  1777. {*********************************************************************}
  1778. var
  1779. i: tasmop;
  1780. j: byte;
  1781. Begin
  1782. findopcode := A_NONE;
  1783. for i:=firstop to lastop do
  1784. if s = iasmops^[i] then
  1785. begin
  1786. findopcode:=i;
  1787. exit;
  1788. end;
  1789. { not found yet, search for extended opcodes }
  1790. { now, in this case, we must use the suffix }
  1791. { to determine the size of the instruction }
  1792. for j:=0 to _count_asmspecialops do
  1793. Begin
  1794. if s = _specialops[j] then
  1795. Begin
  1796. findopcode := _specialopstokens[j];
  1797. { set the size }
  1798. case s[length(s)] of
  1799. 'B': instr.stropsize := S_B;
  1800. 'D': instr.stropsize := S_L;
  1801. 'W': instr.stropsize := S_W;
  1802. end;
  1803. exit;
  1804. end;
  1805. end;
  1806. end;
  1807. Function CheckPrefix(prefix: tasmop; opcode:tasmop): Boolean;
  1808. { Checks if the prefix is valid with the following instruction }
  1809. { return false if not, otherwise true }
  1810. Begin
  1811. CheckPrefix := TRUE;
  1812. Case prefix of
  1813. A_REP,A_REPNE,A_REPE: if not (opcode in [A_SCAS,A_INS,A_OUTS,A_MOVS,
  1814. A_CMPS,A_LODS,A_STOS]) then
  1815. Begin
  1816. CheckPrefix := FALSE;
  1817. exit;
  1818. end;
  1819. A_LOCK: if not (opcode in [A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,
  1820. A_ADC,A_SBB,A_AND,A_SUB,A_XOR,A_NOT,A_NEG,A_INC,A_DEC]) then
  1821. Begin
  1822. CheckPrefix := FALSE;
  1823. Exit;
  1824. end;
  1825. A_NONE: exit; { no prefix here }
  1826. else
  1827. CheckPrefix := FALSE;
  1828. end; { end case }
  1829. end;
  1830. Procedure InitAsmRef(var instr: TInstruction);
  1831. {*********************************************************************}
  1832. { Description: This routine first check if the instruction is of }
  1833. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  1834. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  1835. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  1836. { to point to the default segment. }
  1837. {*********************************************************************}
  1838. Begin
  1839. With instr do
  1840. Begin
  1841. case operands[operandnum].operandtype of
  1842. OPR_REFERENCE: exit;
  1843. OPR_NONE: ;
  1844. else
  1845. Message(assem_e_invalid_operand_type);
  1846. end;
  1847. operands[operandnum].operandtype := OPR_REFERENCE;
  1848. operands[operandnum].ref.segment := R_DEFAULT_SEG;
  1849. end;
  1850. end;
  1851. Function CheckOverride(segreg: tregister; var instr: TInstruction): Boolean;
  1852. { Check if the override is valid, and if so then }
  1853. { update the instr variable accordingly. }
  1854. Begin
  1855. CheckOverride := FALSE;
  1856. if instr.getinstruction in [A_MOVS,A_XLAT,A_CMPS] then
  1857. Begin
  1858. CheckOverride := TRUE;
  1859. Message(assem_e_segment_override_not_supported);
  1860. end
  1861. end;
  1862. Function CalculateExpression(expression: string): longint;
  1863. var
  1864. expr: TExprParse;
  1865. Begin
  1866. expr.Init;
  1867. CalculateExpression := expr.Evaluate(expression);
  1868. expr.Done;
  1869. end;
  1870. Function BuildRefExpression: longint;
  1871. {*********************************************************************}
  1872. { FUNCTION BuildExpression: longint }
  1873. { Description: This routine calculates a constant expression to }
  1874. { a given value. The return value is the value calculated from }
  1875. { the expression. }
  1876. { The following tokens (not strings) are recognized: }
  1877. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  1878. {*********************************************************************}
  1879. { ENTRY: On entry the token should be any valid expression token. }
  1880. { EXIT: On Exit the token points to any token after the closing }
  1881. { RBRACKET }
  1882. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  1883. { invalid tokens. }
  1884. {*********************************************************************}
  1885. var tempstr: string;
  1886. expr: string;
  1887. l : longint;
  1888. errorflag : boolean;
  1889. Begin
  1890. errorflag := FALSE;
  1891. tempstr := '';
  1892. expr := '';
  1893. { tell tokenizer that we are in }
  1894. { an expression. }
  1895. inexpression := TRUE;
  1896. Repeat
  1897. Case actasmtoken of
  1898. AS_LPAREN: Begin
  1899. Consume(AS_LPAREN);
  1900. expr := expr + '(';
  1901. end;
  1902. AS_RPAREN: Begin
  1903. Consume(AS_RPAREN);
  1904. expr := expr + ')';
  1905. end;
  1906. AS_SHL: Begin
  1907. Consume(AS_SHL);
  1908. expr := expr + '<';
  1909. end;
  1910. AS_SHR: Begin
  1911. Consume(AS_SHR);
  1912. expr := expr + '>';
  1913. end;
  1914. AS_SLASH: Begin
  1915. Consume(AS_SLASH);
  1916. expr := expr + '/';
  1917. end;
  1918. AS_MOD: Begin
  1919. Consume(AS_MOD);
  1920. expr := expr + '%';
  1921. end;
  1922. AS_STAR: Begin
  1923. Consume(AS_STAR);
  1924. expr := expr + '*';
  1925. end;
  1926. AS_PLUS: Begin
  1927. Consume(AS_PLUS);
  1928. expr := expr + '+';
  1929. end;
  1930. AS_MINUS: Begin
  1931. Consume(AS_MINUS);
  1932. expr := expr + '-';
  1933. end;
  1934. AS_AND: Begin
  1935. Consume(AS_AND);
  1936. expr := expr + '&';
  1937. end;
  1938. AS_NOT: Begin
  1939. Consume(AS_NOT);
  1940. expr := expr + '~';
  1941. end;
  1942. AS_XOR: Begin
  1943. Consume(AS_XOR);
  1944. expr := expr + '^';
  1945. end;
  1946. AS_OR: Begin
  1947. Consume(AS_OR);
  1948. expr := expr + '|';
  1949. end;
  1950. { End of reference }
  1951. AS_RBRACKET: Begin
  1952. if not ErrorFlag then
  1953. BuildRefExpression := CalculateExpression(expr)
  1954. else
  1955. BuildRefExpression := 0;
  1956. Consume(AS_RBRACKET);
  1957. { no longer in an expression }
  1958. inexpression := FALSE;
  1959. exit;
  1960. end;
  1961. AS_ID:
  1962. Begin
  1963. if NOT SearchIConstant(actasmpattern,l) then
  1964. Begin
  1965. Message1(assem_e_invalid_const_symbol,actasmpattern);
  1966. l := 0;
  1967. end;
  1968. str(l, tempstr);
  1969. expr := expr + tempstr;
  1970. Consume(AS_ID);
  1971. end;
  1972. AS_INTNUM: Begin
  1973. expr := expr + actasmpattern;
  1974. Consume(AS_INTNUM);
  1975. end;
  1976. AS_BINNUM: Begin
  1977. tempstr := BinaryToDec(actasmpattern);
  1978. if tempstr = '' then
  1979. Message(assem_f_error_converting_bin);
  1980. expr:=expr+tempstr;
  1981. Consume(AS_BINNUM);
  1982. end;
  1983. AS_HEXNUM: Begin
  1984. tempstr := HexToDec(actasmpattern);
  1985. if tempstr = '' then
  1986. Message(assem_f_error_converting_hex);
  1987. expr:=expr+tempstr;
  1988. Consume(AS_HEXNUM);
  1989. end;
  1990. AS_OCTALNUM: Begin
  1991. tempstr := OctalToDec(actasmpattern);
  1992. if tempstr = '' then
  1993. Message(assem_f_error_converting_octal);
  1994. expr:=expr+tempstr;
  1995. Consume(AS_OCTALNUM);
  1996. end;
  1997. else
  1998. Begin
  1999. { write error only once. }
  2000. if not errorflag then
  2001. Message(assem_e_invalid_constant_expression);
  2002. BuildRefExpression := 0;
  2003. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then exit;
  2004. { consume tokens until we find COMMA or SEPARATOR }
  2005. Consume(actasmtoken);
  2006. errorflag := TRUE;
  2007. end;
  2008. end;
  2009. Until false;
  2010. end;
  2011. Procedure BuildRecordOffset(var instr: TInstruction; varname: string);
  2012. {*********************************************************************}
  2013. { PROCEDURE BuildRecordOffset(var Instr: TInstruction) }
  2014. { Description: This routine takes care of field specifiers of records }
  2015. { and/or variables in asm operands. It updates the offset accordingly}
  2016. {*********************************************************************}
  2017. { ENTRY: On entry the token should be DOT. }
  2018. { name: should be the name of the variable to be expanded. '' if }
  2019. { no variabled specified. }
  2020. { EXIT: On Exit the token points to SEPARATOR or COMMA. }
  2021. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  2022. { invalid tokens. }
  2023. {*********************************************************************}
  2024. var
  2025. firstpass: boolean;
  2026. offset: longint;
  2027. basetypename : string;
  2028. Begin
  2029. basetypename := '';
  2030. firstpass := TRUE;
  2031. { // .ID[REG].ID ... // }
  2032. { // .ID.ID... // }
  2033. Consume(AS_DOT);
  2034. Repeat
  2035. case actasmtoken of
  2036. AS_ID: Begin
  2037. InitAsmRef(instr);
  2038. { // var_name.typefield.typefield // }
  2039. if (varname <> '') then
  2040. Begin
  2041. if not GetVarOffset(varname,actasmpattern,offset) then
  2042. Begin
  2043. Message1(assem_e_unknown_id,actasmpattern);
  2044. end
  2045. else
  2046. Inc(instr.operands[operandnum].ref.offset,Offset);
  2047. end
  2048. else
  2049. { [ref].var_name.typefield.typefield ... }
  2050. { [ref].var_name[reg] }
  2051. if not assigned(instr.operands[operandnum].ref.symbol) and
  2052. firstpass then
  2053. Begin
  2054. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2055. Begin
  2056. { type field ? }
  2057. basetypename := actasmpattern;
  2058. end
  2059. else
  2060. varname := actasmpattern;
  2061. end
  2062. else
  2063. if firstpass then
  2064. { [ref].typefield.typefield ... }
  2065. { where the first typefield must specifiy the base }
  2066. { object or record type. }
  2067. Begin
  2068. basetypename := actasmpattern;
  2069. end
  2070. else
  2071. { [ref].typefield.typefield ... }
  2072. { basetpyename is already set up... now look for fields. }
  2073. Begin
  2074. if not GetTypeOffset(basetypename,actasmpattern,Offset) then
  2075. Begin
  2076. Message1(assem_e_unknown_id,actasmpattern);
  2077. end
  2078. else
  2079. Inc(instr.operands[operandnum].ref.offset,Offset);
  2080. end;
  2081. Consume(AS_ID);
  2082. { Take care of index register on this variable }
  2083. if actasmtoken = AS_LBRACKET then
  2084. Begin
  2085. Consume(AS_LBRACKET);
  2086. Case actasmtoken of
  2087. AS_REGISTER: Begin
  2088. if instr.operands[operandnum].ref.index <> R_NO then
  2089. Message(assem_e_defining_index_more_than_once);
  2090. instr.operands[operandnum].ref.index :=
  2091. findregister(actasmpattern);
  2092. Consume(AS_REGISTER);
  2093. end;
  2094. else
  2095. Begin
  2096. { add offsets , assuming these are constant expressions... }
  2097. Inc(instr.operands[operandnum].ref.offset,BuildRefExpression);
  2098. end;
  2099. end;
  2100. Consume(AS_RBRACKET);
  2101. end;
  2102. { Here we should either have AS_DOT, AS_SEPARATOR or AS_COMMA }
  2103. if actasmtoken = AS_DOT then
  2104. Consume(AS_DOT);
  2105. firstpass := FALSE;
  2106. Offset := 0;
  2107. end;
  2108. AS_SEPARATOR: exit;
  2109. AS_COMMA: exit;
  2110. else
  2111. Begin
  2112. Message(assem_e_invalid_field_specifier);
  2113. Consume(actasmtoken);
  2114. firstpass := FALSE;
  2115. end;
  2116. end; { end case }
  2117. Until (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA);
  2118. end;
  2119. Function BuildExpression: longint;
  2120. {*********************************************************************}
  2121. { FUNCTION BuildExpression: longint }
  2122. { Description: This routine calculates a constant expression to }
  2123. { a given value. The return value is the value calculated from }
  2124. { the expression. }
  2125. { The following tokens (not strings) are recognized: }
  2126. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  2127. {*********************************************************************}
  2128. { ENTRY: On entry the token should be any valid expression token. }
  2129. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  2130. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  2131. { invalid tokens. }
  2132. {*********************************************************************}
  2133. var expr: string;
  2134. tempstr: string;
  2135. l : longint;
  2136. errorflag: boolean;
  2137. Begin
  2138. errorflag := FALSE;
  2139. expr := '';
  2140. tempstr := '';
  2141. { tell tokenizer that we are in an expression. }
  2142. inexpression := TRUE;
  2143. Repeat
  2144. Case actasmtoken of
  2145. AS_LPAREN: Begin
  2146. Consume(AS_LPAREN);
  2147. expr := expr + '(';
  2148. end;
  2149. AS_RPAREN: Begin
  2150. Consume(AS_RPAREN);
  2151. expr := expr + ')';
  2152. end;
  2153. AS_SHL: Begin
  2154. Consume(AS_SHL);
  2155. expr := expr + '<';
  2156. end;
  2157. AS_SHR: Begin
  2158. Consume(AS_SHR);
  2159. expr := expr + '>';
  2160. end;
  2161. AS_SLASH: Begin
  2162. Consume(AS_SLASH);
  2163. expr := expr + '/';
  2164. end;
  2165. AS_MOD: Begin
  2166. Consume(AS_MOD);
  2167. expr := expr + '%';
  2168. end;
  2169. AS_STAR: Begin
  2170. Consume(AS_STAR);
  2171. expr := expr + '*';
  2172. end;
  2173. AS_PLUS: Begin
  2174. Consume(AS_PLUS);
  2175. expr := expr + '+';
  2176. end;
  2177. AS_MINUS: Begin
  2178. Consume(AS_MINUS);
  2179. expr := expr + '-';
  2180. end;
  2181. AS_AND: Begin
  2182. Consume(AS_AND);
  2183. expr := expr + '&';
  2184. end;
  2185. AS_NOT: Begin
  2186. Consume(AS_NOT);
  2187. expr := expr + '~';
  2188. end;
  2189. AS_XOR: Begin
  2190. Consume(AS_XOR);
  2191. expr := expr + '^';
  2192. end;
  2193. AS_OR: Begin
  2194. Consume(AS_OR);
  2195. expr := expr + '|';
  2196. end;
  2197. AS_ID: Begin
  2198. if NOT SearchIConstant(actasmpattern,l) then
  2199. Begin
  2200. Message1(assem_e_invalid_const_symbol,actasmpattern);
  2201. l := 0;
  2202. end;
  2203. str(l, tempstr);
  2204. expr := expr + tempstr;
  2205. Consume(AS_ID);
  2206. end;
  2207. AS_INTNUM: Begin
  2208. expr := expr + actasmpattern;
  2209. Consume(AS_INTNUM);
  2210. end;
  2211. AS_BINNUM: Begin
  2212. tempstr := BinaryToDec(actasmpattern);
  2213. if tempstr = '' then
  2214. Message(assem_f_error_converting_bin);
  2215. expr:=expr+tempstr;
  2216. Consume(AS_BINNUM);
  2217. end;
  2218. AS_HEXNUM: Begin
  2219. tempstr := HexToDec(actasmpattern);
  2220. if tempstr = '' then
  2221. Message(assem_f_error_converting_hex);
  2222. expr:=expr+tempstr;
  2223. Consume(AS_HEXNUM);
  2224. end;
  2225. AS_OCTALNUM: Begin
  2226. tempstr := OctalToDec(actasmpattern);
  2227. if tempstr = '' then
  2228. Message(assem_f_error_converting_octal);
  2229. expr:=expr+tempstr;
  2230. Consume(AS_OCTALNUM);
  2231. end;
  2232. { go to next term }
  2233. AS_COMMA: Begin
  2234. if not ErrorFlag then
  2235. BuildExpression := CalculateExpression(expr)
  2236. else
  2237. BuildExpression := 0;
  2238. inexpression := FALSE;
  2239. Exit;
  2240. end;
  2241. { go to next symbol }
  2242. AS_SEPARATOR: Begin
  2243. if not ErrorFlag then
  2244. BuildExpression := CalculateExpression(expr)
  2245. else
  2246. BuildExpression := 0;
  2247. inexpression := FALSE;
  2248. Exit;
  2249. end;
  2250. else
  2251. Begin
  2252. { only write error once. }
  2253. if not errorflag then
  2254. Message(assem_e_invalid_constant_expression);
  2255. { consume tokens until we find COMMA or SEPARATOR }
  2256. Consume(actasmtoken);
  2257. errorflag := TRUE;
  2258. End;
  2259. end;
  2260. Until false;
  2261. end;
  2262. Procedure BuildScaling(Var instr: TInstruction);
  2263. {*********************************************************************}
  2264. { Takes care of parsing expression starting from the scaling value }
  2265. { up to and including possible field specifiers. }
  2266. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR }
  2267. { or AS_COMMA. On entry should point to AS_STAR token. }
  2268. {*********************************************************************}
  2269. var str:string;
  2270. l: longint;
  2271. code: integer;
  2272. Begin
  2273. Consume(AS_STAR);
  2274. if (instr.operands[operandnum].ref.scalefactor <> 0)
  2275. and (instr.operands[operandnum].ref.scalefactor <> 1) then
  2276. Begin
  2277. Message(assem_f_internal_error_in_buildscale);
  2278. end;
  2279. case actasmtoken of
  2280. AS_INTNUM: str := actasmpattern;
  2281. AS_HEXNUM: str := HexToDec(actasmpattern);
  2282. AS_BINNUM: str := BinaryToDec(actasmpattern);
  2283. AS_OCTALNUM: str := OctalToDec(actasmpattern);
  2284. else
  2285. Message(assem_e_syntax_error);
  2286. end;
  2287. val(str, l, code);
  2288. if code <> 0 then
  2289. Message(assem_e_invalid_scaling_factor);
  2290. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) and (code = 0) then
  2291. begin
  2292. instr.operands[operandnum].ref.scalefactor := l;
  2293. end
  2294. else
  2295. Begin
  2296. Message(assem_e_invalid_scaling_value);
  2297. instr.operands[operandnum].ref.scalefactor := 0;
  2298. end;
  2299. if instr.operands[operandnum].ref.index = R_NO then
  2300. Begin
  2301. Message(assem_e_scaling_value_only_allowed_with_index);
  2302. instr.operands[operandnum].ref.scalefactor := 0;
  2303. end;
  2304. { Consume the scaling number }
  2305. Consume(actasmtoken);
  2306. case actasmtoken of
  2307. { // [...*SCALING-expr] ... // }
  2308. AS_MINUS: Begin
  2309. if instr.operands[operandnum].ref.offset <> 0 then
  2310. Message(assem_f_internal_error_in_buildscale);
  2311. instr.operands[operandnum].ref.offset :=
  2312. BuildRefExpression;
  2313. end;
  2314. { // [...*SCALING+expr] ... // }
  2315. AS_PLUS: Begin
  2316. if instr.operands[operandnum].ref.offset <> 0 then
  2317. Message(assem_f_internal_error_in_buildscale);
  2318. instr.operands[operandnum].ref.offset :=
  2319. BuildRefExpression;
  2320. end;
  2321. { // [...*SCALING] ... // }
  2322. AS_RBRACKET: Consume(AS_RBRACKET);
  2323. else
  2324. Message(assem_e_invalid_scaling_value);
  2325. end;
  2326. { // .Field.Field ... or separator/comma // }
  2327. Case actasmtoken of
  2328. AS_DOT: BuildRecordOffset(instr,'');
  2329. AS_COMMA, AS_SEPARATOR: ;
  2330. else
  2331. Message(assem_e_syntax_error);
  2332. end;
  2333. end;
  2334. Procedure BuildReference(var instr: TInstruction);
  2335. {*********************************************************************}
  2336. { EXIT CONDITION: On exit the routine should point to either the }
  2337. { AS_COMMA or AS_SEPARATOR token. }
  2338. { On entry: contains the register after the opening bracket if any. }
  2339. {*********************************************************************}
  2340. var
  2341. reg:string;
  2342. segreg: boolean;
  2343. negative: boolean;
  2344. expr: string;
  2345. Begin
  2346. expr := '';
  2347. if instr.operands[operandnum].operandtype <> OPR_REFERENCE then
  2348. Begin
  2349. Message(assem_e_syn_no_ref_with_brackets);
  2350. InitAsmRef(instr);
  2351. consume(AS_REGISTER);
  2352. end
  2353. else
  2354. Begin
  2355. { save the reg }
  2356. reg := actasmpattern;
  2357. { is the syntax of the form: [REG:REG...] }
  2358. consume(AS_REGISTER);
  2359. if actasmtoken = AS_COLON then
  2360. begin
  2361. segreg := TRUE;
  2362. Message(assem_e_expression_form_not_supported);
  2363. if instr.operands[operandnum].ref.segment <> R_NO then
  2364. Message(assem_e_defining_seg_more_than_once);
  2365. instr.operands[operandnum].ref.segment := findsegment(reg);
  2366. { Here we should process the syntax of the form }
  2367. { [reg:reg...] }
  2368. {!!!!!!!!!!!!!!!!!!!!!!!! }
  2369. end
  2370. { This is probably of the following syntax: }
  2371. { SREG:[REG...] where SReg: is optional. }
  2372. { Therefore we immediately say that reg }
  2373. { is the base. }
  2374. else
  2375. Begin
  2376. if instr.operands[operandnum].ref.base <> R_NO then
  2377. Message(assem_e_defining_base_more_than_once);
  2378. instr.operands[operandnum].ref.base := findregister(reg);
  2379. end;
  2380. { we process this type of syntax immediately... }
  2381. case actasmtoken of
  2382. { // REG:[REG].Field.Field ... // }
  2383. { // REG:[REG].Field[REG].Field... // }
  2384. AS_RBRACKET: Begin
  2385. Consume(AS_RBRACKET);
  2386. { check for record fields }
  2387. if actasmtoken = AS_DOT then
  2388. BuildRecordOffset(instr,'');
  2389. if (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA) then
  2390. exit
  2391. else
  2392. Message(assem_e_syn_reference);
  2393. end;
  2394. { // REG:[REG +/- ...].Field.Field ... // }
  2395. AS_PLUS,AS_MINUS: Begin
  2396. if actasmtoken = AS_MINUS then
  2397. Begin
  2398. expr := '-';
  2399. negative := TRUE
  2400. end
  2401. else
  2402. Begin
  2403. negative := FALSE;
  2404. expr := '+';
  2405. end;
  2406. Consume(actasmtoken);
  2407. { // REG:[REG+REG+/-...].Field.Field // }
  2408. if actasmtoken = AS_REGISTER then
  2409. Begin
  2410. if negative then
  2411. Message(assem_e_negative_index_register);
  2412. if instr.operands[operandnum].ref.index <> R_NO then
  2413. Message(assem_e_defining_index_more_than_once);
  2414. instr.operands[operandnum].ref.index := findregister(actasmpattern);
  2415. Consume(AS_REGISTER);
  2416. case actasmtoken of
  2417. AS_RBRACKET: { // REG:[REG+REG].Field.Field... // }
  2418. Begin
  2419. Consume(AS_RBRACKET);
  2420. Case actasmtoken of
  2421. AS_DOT: BuildRecordOffset(instr,'');
  2422. AS_COMMA,AS_SEPARATOR: exit;
  2423. else
  2424. Message(assem_e_syntax_error);
  2425. end
  2426. end;
  2427. AS_PLUS,AS_MINUS: { // REG:[REG+REG+/-expr].Field.Field... // }
  2428. Begin
  2429. if instr.operands[operandnum].ref.offset <> 0 then
  2430. Message(assem_f_internal_error_in_buildreference);
  2431. instr.operands[operandnum].ref.offset :=
  2432. BuildRefExpression;
  2433. case actasmtoken of
  2434. AS_DOT: BuildRecordOffset(instr,'');
  2435. AS_COMMA,AS_SEPARATOR: ;
  2436. else
  2437. Message(assem_e_syntax_error);
  2438. end; { end case }
  2439. end;
  2440. AS_STAR: Begin { // REG:[REG+REG*SCALING...].Field.Field... // }
  2441. BuildScaling(instr);
  2442. end;
  2443. else
  2444. Begin
  2445. Message(assem_e_syntax_error);
  2446. end;
  2447. end; { end case }
  2448. end
  2449. else if actasmtoken = AS_STAR then
  2450. { // REG:[REG*SCALING ... ] // }
  2451. Begin
  2452. BuildScaling(instr);
  2453. end
  2454. else
  2455. { // REG:[REG+expr].Field.Field // }
  2456. Begin
  2457. if instr.operands[operandnum].ref.offset <> 0 then
  2458. Message(assem_f_internal_error_in_buildreference);
  2459. instr.operands[operandnum].ref.offset := BuildRefExpression;
  2460. case actasmtoken of
  2461. AS_DOT: BuildRecordOffset(instr,'');
  2462. AS_COMMA,AS_SEPARATOR: ;
  2463. else
  2464. Message(assem_e_syntax_error);
  2465. end; { end case }
  2466. end; { end if }
  2467. end; { end this case }
  2468. { // REG:[REG*scaling] ... // }
  2469. AS_STAR: Begin
  2470. BuildScaling(instr);
  2471. end;
  2472. end;
  2473. end; { end outer if }
  2474. end;
  2475. Procedure BuildBracketExpression(var Instr: TInstruction; var_prefix: boolean);
  2476. {*********************************************************************}
  2477. { PROCEDURE BuildBracketExpression }
  2478. { Description: This routine builds up an expression after a LBRACKET }
  2479. { token is encountered. }
  2480. { On entry actasmtoken should be equal to AS_LBRACKET. }
  2481. { var_prefix : Should be set to true if variable identifier has }
  2482. { been defined, such as in ID[ }
  2483. {*********************************************************************}
  2484. { EXIT CONDITION: On exit the routine should point to either the }
  2485. { AS_COMMA or AS_SEPARATOR token. }
  2486. {*********************************************************************}
  2487. var
  2488. l:longint;
  2489. Begin
  2490. Consume(AS_LBRACKET);
  2491. initAsmRef(instr);
  2492. Case actasmtoken of
  2493. { // Constant reference expression OR variable reference expression // }
  2494. AS_ID: Begin
  2495. if actasmpattern[1] = '@' then
  2496. Message(assem_e_local_symbol_not_allowed_as_ref);
  2497. if SearchIConstant(actasmpattern,l) then
  2498. Begin
  2499. { if there was a variable prefix then }
  2500. { add to offset }
  2501. If var_prefix then
  2502. Begin
  2503. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2504. end
  2505. else
  2506. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2507. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2508. Message(assem_e_invalid_operand_in_bracket_expression);
  2509. end
  2510. else if NOT var_prefix then
  2511. Begin
  2512. InitAsmRef(instr);
  2513. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2514. Message1(assem_e_unknown_id,actasmpattern);
  2515. Consume(AS_ID);
  2516. { is there a constant expression following }
  2517. { the variable name? }
  2518. if actasmtoken <> AS_RBRACKET then
  2519. Begin
  2520. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2521. end
  2522. else
  2523. Consume(AS_RBRACKET);
  2524. end
  2525. else
  2526. Message1(assem_e_invalid_symbol_name,actasmpattern);
  2527. end;
  2528. { Here we handle the special case in tp where }
  2529. { the + operator is allowed with reg and var }
  2530. { references, such as in mov al, byte ptr [+bx] }
  2531. AS_PLUS: Begin
  2532. Consume(AS_PLUS);
  2533. Case actasmtoken of
  2534. AS_REGISTER: Begin
  2535. BuildReference(instr);
  2536. end;
  2537. AS_ID: Begin
  2538. if actasmpattern[1] = '@' then
  2539. Message(assem_e_local_symbol_not_allowed_as_ref);
  2540. if SearchIConstant(actasmpattern,l) then
  2541. Begin
  2542. { if there was a variable prefix then }
  2543. { add to offset }
  2544. If var_prefix then
  2545. Begin
  2546. Inc(instr.operands[operandnum].ref.offset,
  2547. BuildRefExpression);
  2548. end
  2549. else
  2550. instr.operands[operandnum].ref.offset :=
  2551. BuildRefExpression;
  2552. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2553. Message(assem_e_invalid_operand_in_bracket_expression);
  2554. end
  2555. else if NOT var_prefix then
  2556. Begin
  2557. InitAsmRef(instr);
  2558. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2559. Message1(assem_e_unknown_id,actasmpattern);
  2560. Consume(AS_ID);
  2561. { is there a constant expression following }
  2562. { the variable name? }
  2563. if actasmtoken <> AS_RBRACKET then
  2564. Begin
  2565. Inc(instr.operands[operandnum].ref.offset,
  2566. BuildRefExpression);
  2567. end
  2568. else
  2569. Consume(AS_RBRACKET);
  2570. end
  2571. else
  2572. Message1(assem_e_invalid_symbol_name,actasmpattern);
  2573. end;
  2574. { // Constant reference expression // }
  2575. AS_INTNUM,AS_BINNUM,AS_OCTALNUM,
  2576. AS_HEXNUM: Begin
  2577. { if there was a variable prefix then }
  2578. { add to offset instead. }
  2579. If var_prefix then
  2580. Begin
  2581. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2582. end
  2583. else
  2584. Begin
  2585. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2586. end;
  2587. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2588. Message(assem_e_invalid_operand_in_bracket_expression);
  2589. end;
  2590. else
  2591. Message(assem_e_syntax_error);
  2592. end;
  2593. end;
  2594. { // Constant reference expression // }
  2595. AS_MINUS,AS_NOT,AS_LPAREN:
  2596. Begin
  2597. { if there was a variable prefix then }
  2598. { add to offset instead. }
  2599. If var_prefix then
  2600. Begin
  2601. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2602. end
  2603. else
  2604. Begin
  2605. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2606. end;
  2607. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2608. Message(assem_e_invalid_operand_in_bracket_expression);
  2609. end;
  2610. { // Constant reference expression // }
  2611. AS_INTNUM,AS_OCTALNUM,AS_BINNUM,AS_HEXNUM: Begin
  2612. { if there was a variable prefix then }
  2613. { add to offset instead. }
  2614. If var_prefix then
  2615. Begin
  2616. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2617. end
  2618. else
  2619. Begin
  2620. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2621. end;
  2622. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2623. Message(assem_e_invalid_operand_in_bracket_expression);
  2624. end;
  2625. { // Variable reference expression // }
  2626. AS_REGISTER: BuildReference(instr);
  2627. else
  2628. Begin
  2629. Message(assem_e_invalid_reference_syntax);
  2630. while (actasmtoken <> AS_SEPARATOR) do
  2631. Consume(actasmtoken);
  2632. end;
  2633. end; { end case }
  2634. end;
  2635. Procedure BuildOperand(var instr: TInstruction);
  2636. {*********************************************************************}
  2637. { EXIT CONDITION: On exit the routine should point to either the }
  2638. { AS_COMMA or AS_SEPARATOR token. }
  2639. {*********************************************************************}
  2640. var
  2641. tempstr: string;
  2642. expr: string;
  2643. lab: Pasmlabel;
  2644. l : longint;
  2645. hl: plabel;
  2646. Begin
  2647. tempstr := '';
  2648. expr := '';
  2649. case actasmtoken of
  2650. { // Constant expression // }
  2651. AS_PLUS,AS_MINUS,AS_NOT,AS_LPAREN:
  2652. Begin
  2653. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2654. Message(assem_e_invalid_operand_type);
  2655. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2656. instr.operands[operandnum].val :=BuildExpression;
  2657. end;
  2658. { // Constant expression // }
  2659. AS_STRING: Begin
  2660. if not (instr.operands[operandnum].operandtype in [OPR_NONE]) then
  2661. Message(assem_e_invalid_operand_type);
  2662. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2663. if not PadZero(actasmpattern,4) then
  2664. Message1(assem_e_invalid_string_as_opcode_operand,actasmpattern);
  2665. instr.operands[operandnum].val :=
  2666. ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  2667. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1])
  2668. shl 24;
  2669. Consume(AS_STRING);
  2670. Case actasmtoken of
  2671. AS_COMMA, AS_SEPARATOR: ;
  2672. else
  2673. Message(assem_e_invalid_string_expression);
  2674. end; { end case }
  2675. end;
  2676. { // Constant expression // }
  2677. AS_INTNUM,AS_BINNUM,
  2678. AS_OCTALNUM,
  2679. AS_HEXNUM: Begin
  2680. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2681. Message(assem_e_invalid_operand_type);
  2682. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2683. instr.operands[operandnum].val :=BuildExpression;
  2684. end;
  2685. { // A constant expression, or a Variable ref. // }
  2686. AS_ID: Begin
  2687. if actasmpattern[1] = '@' then
  2688. { // Label or Special symbol reference // }
  2689. Begin
  2690. if actasmpattern = '@RESULT' then
  2691. Begin
  2692. InitAsmRef(instr);
  2693. SetUpResult(instr,operandnum);
  2694. end
  2695. else
  2696. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  2697. Message(assem_w_CODE_and_DATA_not_supported)
  2698. else
  2699. Begin
  2700. delete(actasmpattern,1,1);
  2701. if actasmpattern = '' then
  2702. Message(assem_e_null_label_ref_not_allowed);
  2703. lab := labellist.search(actasmpattern);
  2704. { check if the label is already defined }
  2705. { if so, we then check if the plabel is }
  2706. { non-nil, if so we add it to instruction }
  2707. if assigned(lab) then
  2708. Begin
  2709. if assigned(lab^.lab) then
  2710. Begin
  2711. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2712. instr.operands[operandnum].hl := lab^.lab;
  2713. instr.labeled := TRUE;
  2714. end;
  2715. end
  2716. else
  2717. { the label does not exist, create it }
  2718. { emit the opcode, but set that the }
  2719. { label has not been emitted }
  2720. Begin
  2721. getlabel(hl);
  2722. labellist.insert(actasmpattern,hl,FALSE);
  2723. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2724. instr.operands[operandnum].hl := hl;
  2725. instr.labeled := TRUE;
  2726. end;
  2727. end;
  2728. Consume(AS_ID);
  2729. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2730. Begin
  2731. Message(assem_e_syntax_error);
  2732. end;
  2733. end
  2734. { probably a variable or normal expression }
  2735. { or a procedure (such as in CALL ID) }
  2736. else
  2737. Begin
  2738. { is it a constant ? }
  2739. if SearchIConstant(actasmpattern,l) then
  2740. Begin
  2741. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2742. Message(assem_e_invalid_operand_type);
  2743. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2744. instr.operands[operandnum].val :=BuildExpression;
  2745. end
  2746. else { is it a label variable ? }
  2747. Begin
  2748. { // ID[ , ID.Field.Field or simple ID // }
  2749. { check if this is a label, if so then }
  2750. { emit it as a label. }
  2751. if SearchLabel(actasmpattern,hl) then
  2752. Begin
  2753. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2754. instr.operands[operandnum].hl := hl;
  2755. instr.labeled := TRUE;
  2756. Consume(AS_ID);
  2757. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2758. Message(assem_e_syntax_error);
  2759. end
  2760. else
  2761. { is it a normal variable ? }
  2762. Begin
  2763. initAsmRef(instr);
  2764. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2765. Begin
  2766. { not a variable.. }
  2767. { check special variables.. }
  2768. if actasmpattern = 'SELF' then
  2769. { special self variable }
  2770. Begin
  2771. if assigned(procinfo._class) then
  2772. Begin
  2773. instr.operands[operandnum].ref.offset := procinfo.ESI_offset;
  2774. instr.operands[operandnum].ref.base := procinfo.framepointer;
  2775. end
  2776. else
  2777. Message(assem_e_cannot_use_SELF_outside_a_method);
  2778. end
  2779. else
  2780. Message1(assem_e_unknown_id,actasmpattern);
  2781. end;
  2782. expr := actasmpattern;
  2783. Consume(AS_ID);
  2784. case actasmtoken of
  2785. AS_LBRACKET: { indexing }
  2786. BuildBracketExpression(instr,TRUE);
  2787. AS_DOT: BuildRecordOffset(instr,expr);
  2788. AS_SEPARATOR,AS_COMMA: ;
  2789. else
  2790. Message(assem_e_syntax_error);
  2791. end;
  2792. end;
  2793. end;
  2794. end;
  2795. end;
  2796. { // Register, a variable reference or a constant reference // }
  2797. AS_REGISTER: Begin
  2798. { save the type of register used. }
  2799. tempstr := actasmpattern;
  2800. Consume(AS_REGISTER);
  2801. if actasmtoken = AS_COLON then
  2802. Begin
  2803. Consume(AS_COLON);
  2804. if actasmtoken <> AS_LBRACKET then
  2805. Message(assem_e_syn_start_with_bracket)
  2806. else
  2807. Begin
  2808. initAsmRef(instr);
  2809. instr.operands[operandnum].ref.segment := findsegment(tempstr);
  2810. BuildBracketExpression(instr,false);
  2811. end;
  2812. end
  2813. { // Simple register // }
  2814. else if (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA) then
  2815. Begin
  2816. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_REGISTER]) then
  2817. Message(assem_e_invalid_operand_type);
  2818. instr.operands[operandnum].operandtype := OPR_REGISTER;
  2819. instr.operands[operandnum].reg := findregister(tempstr);
  2820. end
  2821. else
  2822. Message1(assem_e_syn_register,tempstr);
  2823. end;
  2824. { // a variable reference, register ref. or a constant reference // }
  2825. AS_LBRACKET: Begin
  2826. BuildBracketExpression(instr,false);
  2827. end;
  2828. { // Unsupported // }
  2829. AS_SEG,AS_OFFSET: Begin
  2830. Message(assem_e_SEG_and_OFFSET_not_supported);
  2831. Consume(actasmtoken);
  2832. { error recovery }
  2833. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  2834. Consume(actasmtoken);
  2835. end;
  2836. AS_SEPARATOR, AS_COMMA: ;
  2837. else
  2838. Message(assem_e_syn_opcode_operand);
  2839. end; { end case }
  2840. end;
  2841. Procedure BuildConstant(maxvalue: longint);
  2842. {*********************************************************************}
  2843. { PROCEDURE BuildConstant }
  2844. { Description: This routine takes care of parsing a DB,DD,or DW }
  2845. { line and adding those to the assembler node. Expressions, range- }
  2846. { checking are fullly taken care of. }
  2847. { maxvalue: $ff -> indicates that this is a DB node. }
  2848. { $ffff -> indicates that this is a DW node. }
  2849. { $ffffffff -> indicates that this is a DD node. }
  2850. {*********************************************************************}
  2851. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  2852. {*********************************************************************}
  2853. var
  2854. strlength: byte;
  2855. expr: string;
  2856. value : longint;
  2857. Begin
  2858. strlength := 0; { assume it is a DB }
  2859. Repeat
  2860. Case actasmtoken of
  2861. AS_STRING: Begin
  2862. if maxvalue = $ffff then
  2863. strlength := 2
  2864. else if maxvalue = $ffffffff then
  2865. strlength := 4;
  2866. if strlength <> 0 then
  2867. { DD and DW cases }
  2868. Begin
  2869. if Not PadZero(actasmpattern,strlength) then
  2870. Message(scan_f_string_exceeds_line);
  2871. end;
  2872. expr := actasmpattern;
  2873. Consume(AS_STRING);
  2874. Case actasmtoken of
  2875. AS_COMMA: Consume(AS_COMMA);
  2876. AS_SEPARATOR: ;
  2877. else
  2878. Message(assem_e_invalid_string_expression);
  2879. end; { end case }
  2880. ConcatString(p,expr);
  2881. end;
  2882. AS_INTNUM,AS_BINNUM,
  2883. AS_OCTALNUM,AS_HEXNUM:
  2884. Begin
  2885. value:=BuildExpression;
  2886. ConcatConstant(p,value,maxvalue);
  2887. end;
  2888. AS_ID:
  2889. Begin
  2890. value:=BuildExpression;
  2891. if value > maxvalue then
  2892. Begin
  2893. Message(assem_e_expression_out_of_bounds);
  2894. { assuming a value of maxvalue }
  2895. value := maxvalue;
  2896. end;
  2897. ConcatConstant(p,value,maxvalue);
  2898. end;
  2899. { These terms can start an assembler expression }
  2900. AS_PLUS,AS_MINUS,AS_LPAREN,AS_NOT: Begin
  2901. value := BuildExpression;
  2902. ConcatConstant(p,value,maxvalue);
  2903. end;
  2904. AS_COMMA: BEGIN
  2905. Consume(AS_COMMA);
  2906. END;
  2907. AS_SEPARATOR: ;
  2908. else
  2909. Begin
  2910. Message(assem_f_internal_error_in_buildconstant);
  2911. end;
  2912. end; { end case }
  2913. Until actasmtoken = AS_SEPARATOR;
  2914. end;
  2915. Procedure BuildOpCode;
  2916. {*********************************************************************}
  2917. { PROCEDURE BuildOpcode; }
  2918. { Description: Parses the intel opcode and operands, and writes it }
  2919. { in the TInstruction object. }
  2920. {*********************************************************************}
  2921. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  2922. { On ENTRY: Token should point to AS_OPCODE }
  2923. {*********************************************************************}
  2924. var asmtok: tasmop;
  2925. op: tasmop;
  2926. expr: string;
  2927. segreg: tregister;
  2928. Begin
  2929. expr := '';
  2930. asmtok := A_NONE; { assmume no prefix }
  2931. segreg := R_NO; { assume no segment override }
  2932. { // prefix seg opcode // }
  2933. { // prefix opcode // }
  2934. if findprefix(actasmpattern,asmtok) then
  2935. Begin
  2936. { standard opcode prefix }
  2937. if asmtok <> A_NONE then
  2938. instr.addprefix(asmtok);
  2939. Consume(AS_OPCODE);
  2940. if findoverride(actasmpattern,segreg) then
  2941. Begin
  2942. Consume(AS_OPCODE);
  2943. Message(assem_w_repeat_prefix_and_seg_override);
  2944. end;
  2945. end
  2946. else
  2947. { // seg prefix opcode // }
  2948. { // seg opcode // }
  2949. if findoverride(actasmpattern,segreg) then
  2950. Begin
  2951. Consume(AS_OPCODE);
  2952. if findprefix(actasmpattern,asmtok) then
  2953. Begin
  2954. { standard opcode prefix }
  2955. Message(assem_w_repeat_prefix_and_seg_override);
  2956. if asmtok <> A_NONE then
  2957. instr.addprefix(asmtok);
  2958. Consume(AS_OPCODE);
  2959. end;
  2960. end;
  2961. { // opcode // }
  2962. if (actasmtoken <> AS_OPCODE) then
  2963. Begin
  2964. Message(assem_e_invalid_or_missing_opcode);
  2965. { error recovery }
  2966. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  2967. Consume(actasmtoken);
  2968. exit;
  2969. end
  2970. else
  2971. Begin
  2972. op := findopcode(actasmpattern);
  2973. instr.addinstr(op);
  2974. { // Valid combination of prefix and instruction ? // }
  2975. if (asmtok <> A_NONE) and (NOT CheckPrefix(asmtok,op)) then
  2976. Message1(assem_e_invalid_prefix_and_opcode,actasmpattern);
  2977. { // Valid combination of segment override // }
  2978. if (segreg <> R_NO) and (NOT CheckOverride(segreg,instr)) then
  2979. Message1(assem_e_invalid_override_and_opcode,actasmpattern);
  2980. Consume(AS_OPCODE);
  2981. { // Zero operand opcode ? // }
  2982. if actasmtoken = AS_SEPARATOR then
  2983. exit
  2984. else
  2985. operandnum := 1;
  2986. end;
  2987. While actasmtoken <> AS_SEPARATOR do
  2988. Begin
  2989. case actasmtoken of
  2990. { // Operand delimiter // }
  2991. AS_COMMA: Begin
  2992. if operandnum > MaxOperands then
  2993. Message(assem_e_too_many_operands)
  2994. else
  2995. Inc(operandnum);
  2996. Consume(AS_COMMA);
  2997. end;
  2998. { // Typecast, Constant Expression, Type Specifier // }
  2999. AS_DWORD,AS_BYTE,AS_WORD,AS_TBYTE,AS_QWORD: Begin
  3000. Case actasmtoken of
  3001. AS_DWORD: instr.operands[operandnum].size := S_L;
  3002. AS_WORD: instr.operands[operandnum].size := S_W;
  3003. AS_BYTE: instr.operands[operandnum].size := S_B;
  3004. AS_QWORD: instr.operands[operandnum].size := S_IQ;
  3005. AS_TBYTE: instr.operands[operandnum].size := S_FX;
  3006. end;
  3007. Consume(actasmtoken);
  3008. Case actasmtoken of
  3009. { // Reference // }
  3010. AS_PTR: Begin
  3011. initAsmRef(instr);
  3012. Consume(AS_PTR);
  3013. BuildOperand(instr);
  3014. end;
  3015. { // Possibly a typecast or a constant // }
  3016. { // expression. // }
  3017. AS_LPAREN: Begin
  3018. if actasmtoken = AS_ID then
  3019. Begin
  3020. { Case vartype of }
  3021. { LOCAL: Replace by offset and }
  3022. { BP in treference. }
  3023. { GLOBAL: Replace by mangledname}
  3024. { in symbol of treference }
  3025. { Check if next token = RPAREN }
  3026. { otherwise syntax error. }
  3027. initAsmRef(instr);
  3028. if not CreateVarInstr(instr,actasmpattern,
  3029. operandnum) then
  3030. Begin
  3031. Message1(assem_e_unknown_id,actasmpattern);
  3032. end;
  3033. end
  3034. else
  3035. begin
  3036. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  3037. instr.operands[operandnum].val := BuildExpression;
  3038. end;
  3039. end;
  3040. else
  3041. BuildOperand(instr);
  3042. end; { end case }
  3043. end;
  3044. { // Type specifier // }
  3045. AS_NEAR,AS_FAR: Begin
  3046. if actasmtoken = AS_NEAR then
  3047. Message(assem_w_near_ignored)
  3048. else
  3049. Message(assem_w_far_ignored);
  3050. Consume(actasmtoken);
  3051. if actasmtoken = AS_PTR then
  3052. begin
  3053. initAsmRef(instr);
  3054. Consume(AS_PTR);
  3055. end;
  3056. BuildOperand(instr);
  3057. end;
  3058. { // End of asm operands for this opcode // }
  3059. AS_SEPARATOR: ;
  3060. { // Constant expression // }
  3061. AS_LPAREN: Begin
  3062. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  3063. instr.operands[operandnum].val := BuildExpression;
  3064. end;
  3065. else
  3066. BuildOperand(instr);
  3067. end; { end case }
  3068. end; { end while }
  3069. end;
  3070. Function Assemble: Ptree;
  3071. {*********************************************************************}
  3072. { PROCEDURE Assemble; }
  3073. { Description: Parses the intel assembler syntax, parsing is done }
  3074. { according to the rules in the Turbo Pascal manual. }
  3075. {*********************************************************************}
  3076. Var
  3077. hl: plabel;
  3078. labelptr: pasmlabel;
  3079. Begin
  3080. Message(assem_d_start_intel);
  3081. inexpression := FALSE;
  3082. firsttoken := TRUE;
  3083. operandnum := 0;
  3084. if assigned(procinfo.retdef) and
  3085. (is_fpu(procinfo.retdef) or
  3086. ret_in_acc(procinfo.retdef)) then
  3087. procinfo.funcret_is_valid:=true;
  3088. { sets up all opcode and register tables in uppercase }
  3089. if not _asmsorted then
  3090. Begin
  3091. SetupTables;
  3092. _asmsorted := TRUE;
  3093. end;
  3094. p:=new(paasmoutput,init);
  3095. { setup label linked list }
  3096. labellist.init;
  3097. c:=asmgetchar;
  3098. actasmtoken:=gettoken;
  3099. while actasmtoken<>AS_END do
  3100. Begin
  3101. case actasmtoken of
  3102. AS_LLABEL: Begin
  3103. labelptr := labellist.search(actasmpattern);
  3104. if not assigned(labelptr) then
  3105. Begin
  3106. getlabel(hl);
  3107. labellist.insert(actasmpattern,hl,TRUE);
  3108. ConcatLabel(p,A_LABEL,hl);
  3109. end
  3110. else
  3111. { the label has already been inserted into the }
  3112. { label list, either as an intruction label (in }
  3113. { this case it has not been emitted), or as a }
  3114. { duplicate local symbol (in this case it has }
  3115. { already been emitted). }
  3116. Begin
  3117. if labelptr^.emitted then
  3118. Message1(assem_e_dup_local_sym,'@'+labelptr^.name^)
  3119. else
  3120. Begin
  3121. if assigned(labelptr^.lab) then
  3122. ConcatLabel(p,A_LABEL,labelptr^.lab);
  3123. labelptr^.emitted := TRUE;
  3124. end;
  3125. end;
  3126. Consume(AS_LLABEL);
  3127. end;
  3128. AS_LABEL: Begin
  3129. if SearchLabel(actasmpattern,hl) then
  3130. ConcatLabel(p,A_LABEL, hl)
  3131. else
  3132. Message1(assem_e_unknown_label_identifer,actasmpattern);
  3133. Consume(AS_LABEL);
  3134. end;
  3135. AS_DW: Begin
  3136. Consume(AS_DW);
  3137. BuildConstant($ffff);
  3138. end;
  3139. AS_DB: Begin
  3140. Consume(AS_DB);
  3141. BuildConstant($ff);
  3142. end;
  3143. AS_DD: Begin
  3144. Consume(AS_DD);
  3145. BuildConstant($ffffffff);
  3146. end;
  3147. AS_OPCODE: Begin
  3148. instr.init;
  3149. BuildOpcode;
  3150. instr.numops := operandnum;
  3151. if instr.labeled then
  3152. ConcatLabeledInstr(instr)
  3153. else
  3154. ConcatOpCode(instr);
  3155. end;
  3156. AS_SEPARATOR:Begin
  3157. Consume(AS_SEPARATOR);
  3158. { let us go back to the first operand }
  3159. operandnum := 0;
  3160. end;
  3161. AS_END: ; { end assembly block }
  3162. else
  3163. Begin
  3164. Message(assem_e_assemble_node_syntax_error);
  3165. { error recovery }
  3166. Consume(actasmtoken);
  3167. end;
  3168. end; { end case }
  3169. end; { end while }
  3170. { check if there were undefined symbols. }
  3171. { if so, then list each of those undefined }
  3172. { labels. }
  3173. if assigned(labellist.First) then
  3174. Begin
  3175. labelptr := labellist.First;
  3176. if labellist.First <> nil then
  3177. Begin
  3178. { first label }
  3179. if not labelptr^.emitted then
  3180. Message1(assem_e_unknown_local_sym,'@'+labelptr^.name^);
  3181. { other labels ... }
  3182. While (labelptr^.Next <> nil) do
  3183. Begin
  3184. labelptr := labelptr^.Next;
  3185. if not labelptr^.emitted then
  3186. Message1(assem_e_unknown_local_sym,'@'+labelptr^.name^);
  3187. end;
  3188. end;
  3189. end;
  3190. assemble := genasmnode(p);
  3191. labellist.done;
  3192. Message(assem_d_finish_intel);
  3193. end;
  3194. procedure ra386int_exit;{$ifndef FPC}far;{$endif}
  3195. begin
  3196. if assigned(iasmops) then
  3197. dispose(iasmops);
  3198. exitproc:=old_exit;
  3199. end;
  3200. begin
  3201. old_exit:=exitproc;
  3202. exitproc:=@ra386int_exit;
  3203. end.
  3204. {
  3205. $Log$
  3206. Revision 1.2 1998-06-24 14:06:38 peter
  3207. * fixed the name changes
  3208. Revision 1.1 1998/06/23 14:00:18 peter
  3209. * renamed RA* units
  3210. Revision 1.11 1998/06/16 08:56:28 peter
  3211. + targetcpu
  3212. * cleaner pmodules for newppu
  3213. Revision 1.10 1998/06/12 10:32:33 pierre
  3214. * column problem hopefully solved
  3215. + C vars declaration changed
  3216. Revision 1.9 1998/05/31 14:13:32 peter
  3217. * fixed call bugs with assembler readers
  3218. + OPR_SYMBOL to hold a symbol in the asm parser
  3219. * fixed staticsymtable vars which were acessed through %ebp instead of
  3220. name
  3221. Revision 1.8 1998/05/30 14:31:07 peter
  3222. + $ASMMODE
  3223. Revision 1.7 1998/05/28 16:32:05 carl
  3224. * bugfix with operands main branch version (patched manually)
  3225. Revision 1.6 1998/05/23 01:21:26 peter
  3226. + aktasmmode, aktoptprocessor, aktoutputformat
  3227. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  3228. + $LIBNAME to set the library name where the unit will be put in
  3229. * splitted cgi386 a bit (codeseg to large for bp7)
  3230. * nasm, tasm works again. nasm moved to ag386nsm.pas
  3231. Revision 1.5 1998/05/20 09:42:36 pierre
  3232. + UseTokenInfo now default
  3233. * unit in interface uses and implementation uses gives error now
  3234. * only one error for unknown symbol (uses lastsymknown boolean)
  3235. the problem came from the label code !
  3236. + first inlined procedures and function work
  3237. (warning there might be allowed cases were the result is still wrong !!)
  3238. * UseBrower updated gives a global list of all position of all used symbols
  3239. with switch -gb
  3240. Revision 1.4 1998/04/29 10:34:03 pierre
  3241. + added some code for ansistring (not complete nor working yet)
  3242. * corrected operator overloading
  3243. * corrected nasm output
  3244. + started inline procedures
  3245. + added starstarn : use ** for exponentiation (^ gave problems)
  3246. + started UseTokenInfo cond to get accurate positions
  3247. Revision 1.3 1998/04/08 16:58:06 pierre
  3248. * several bugfixes
  3249. ADD ADC and AND are also sign extended
  3250. nasm output OK (program still crashes at end
  3251. and creates wrong assembler files !!)
  3252. procsym types sym in tdef removed !!
  3253. }