ra386int.pas 141 KB

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