2
0

rax86int.pas 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Does the parsing process for the intel styled inline assembler.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. Unit Rax86int;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. cpubase,
  23. globtype,
  24. aasmbase,
  25. cgbase,
  26. rasm,
  27. rax86;
  28. type
  29. tasmtoken = (
  30. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  31. AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  32. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  33. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,
  34. AS_LOPMASK,AS_VOPMASK,AS_LOPZEROMASK,AS_VOPZEROMASK,AS_LOPBCST,AS_OPBCST1TO2,AS_OPBCST1TO4,AS_OPBCST1TO8,AS_OPBCST1TO16,AS_LOPSAE,AS_OPSAE,
  35. AS_LOPER,AS_OPRNSAE,AS_OPRDSAE,AS_OPRUSAE,AS_OPRZSAE,
  36. {------------------ Assembler directives --------------------}
  37. AS_ALIGN,AS_DB,AS_DW,AS_DD,AS_DQ,AS_PUBLIC,AS_END,
  38. {------------------ Assembler Operators --------------------}
  39. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_DQWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_ZWORD,AS_ZMMWORD,AS_NEAR,AS_FAR,
  40. AS_HIGH,AS_LOW,AS_OFFSET,AS_SIZEOF,AS_VMTOFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  41. AS_AND,AS_OR,AS_XOR,AS_WRT,AS___GOTPCREL,AS_TARGET_DIRECTIVE,AS_RNSAE
  42. );
  43. type
  44. { input flags for BuildConstSymbolExpression }
  45. tconstsymbolexpressioninputflag = (
  46. cseif_needofs,
  47. cseif_isref,
  48. cseif_startingminus,
  49. { allows using full reference-like syntax for constsymbol expressions,
  50. for example:
  51. Rec.Str[5] -> Rec.Str+5 }
  52. cseif_referencelike
  53. );
  54. tconstsymbolexpressioninputflags = set of tconstsymbolexpressioninputflag;
  55. { output flags for BuildConstSymbolExpression }
  56. tconstsymbolexpressionoutputflag = (
  57. cseof_isseg,
  58. cseof_is_farproc_entry,
  59. cseof_hasofs
  60. );
  61. tconstsymbolexpressionoutputflags = set of tconstsymbolexpressionoutputflag;
  62. { tx86intreader }
  63. tx86intreader = class(tasmreader)
  64. actasmpattern_origcase : string;
  65. actasmtoken : tasmtoken;
  66. actoperextention: string;
  67. prevasmtoken : tasmtoken;
  68. ActOpsize : topsize;
  69. inexpression : boolean;
  70. constructor create;override;
  71. function is_asmopcode(const s: string):boolean;
  72. function is_asmoperator(const s: string):boolean;
  73. function is_asmdirective(const s: string):boolean;
  74. function is_register(const s:string):boolean;
  75. function is_locallabel(const s:string):boolean;
  76. function Assemble: tlinkedlist;override;
  77. procedure GetToken(check_operand_extention: boolean = false);
  78. function consume(t : tasmtoken; check_operand_extention: boolean = false):boolean;
  79. //procedure ConsumeOperExtention(oper: tx86operand; const aOperExtention: string);
  80. procedure RecoverConsume(allowcomma:boolean);
  81. procedure AddReferences(dest,src : tx86operand);
  82. procedure SetSegmentOverride(oper:tx86operand;seg:tregister);
  83. procedure BuildRecordOffsetSize(const expr: string;out offset:tcgint;out size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean);
  84. procedure BuildConstSymbolExpression(in_flags: tconstsymbolexpressioninputflags;out value:tcgint;out asmsym:string;out asmsymtyp:TAsmsymtype;out size:tcgint;out out_flags:tconstsymbolexpressionoutputflags);
  85. function BuildConstExpression:aint;
  86. function BuildRefConstExpression(out size:tcgint;startingminus:boolean=false):aint;
  87. procedure BuildReference(oper : tx86operand);
  88. procedure BuildOperand(oper: tx86operand;istypecast:boolean);
  89. procedure BuildConstantOperand(oper: tx86operand);
  90. procedure BuildOpCode(instr : tx86instruction);
  91. procedure BuildConstant(constsize: byte);
  92. procedure consume_voperand_ext(aop: tx86operand; aConsumeVOpExt: boolean = true);
  93. function is_targetdirective(const s: string): boolean;virtual;
  94. procedure HandleTargetDirective;virtual;
  95. end;
  96. implementation
  97. uses
  98. { common }
  99. cutils,
  100. { global }
  101. globals,verbose,
  102. systems,
  103. { aasm }
  104. aasmdata,aasmcpu,
  105. {$ifdef i8086}
  106. aasmtai,
  107. {$endif i8086}
  108. { symtable }
  109. symconst,symbase,symtype,symsym,symutil,symdef,
  110. {$ifdef i8086}
  111. symcpu,
  112. {$endif i8086}
  113. { parser }
  114. scanner,pbase,
  115. { register allocator }
  116. rautils,itx86int,
  117. { codegen }
  118. procinfo,paramgr
  119. ;
  120. type
  121. tasmkeyword = string[9];
  122. const
  123. { These tokens should be modified accordingly to the modifications }
  124. { in the different enumerations. }
  125. firstdirective = AS_ALIGN;
  126. lastdirective = AS_END;
  127. firstoperator = AS_BYTE;
  128. lastoperator = AS___GOTPCREL;
  129. OPEXT_STARTASMTOKEN: set of tasmtoken = [AS_LOPMASK,AS_LOPZEROMASK,AS_LOPBCST,AS_LOPSAE,AS_LOPER];
  130. _asmdirectives : array[firstdirective..lastdirective] of tasmkeyword =
  131. ('ALIGN','DB','DW','DD','DQ','PUBLIC','END');
  132. { problems with shl,shr,not,and,or and xor, they are }
  133. { context sensitive. }
  134. _asmoperators : array[firstoperator..lastoperator] of tasmkeyword = (
  135. 'BYTE','WORD','DWORD','QWORD','TBYTE','DQWORD','OWORD','XMMWORD','YWORD','YMMWORD','ZWORD','ZMMWORD','NEAR','FAR','HIGH',
  136. 'LOW','OFFSET','SIZEOF','VMTOFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  137. 'OR','XOR','WRT','GOTPCREL');
  138. token2str : array[tasmtoken] of string[10] = (
  139. '','Label','LLabel','String','Integer',
  140. ',','[',']','(',
  141. ')',':','.','+','-','*',
  142. ';','identifier','register','opcode','/',
  143. '','','','','','','','','','','',
  144. '','','','','',
  145. '','','','','','','END',
  146. '','','','','','','','','','','','','','',
  147. '','','','sizeof','vmtoffset','','type','ptr','mod','shl','shr','not',
  148. 'and','or','xor','wrt','..gotpcrel','','{RN-SAE}'
  149. );
  150. constructor tx86intreader.create;
  151. var
  152. i : tasmop;
  153. Begin
  154. inherited create;
  155. iasmops:=TFPHashList.create;
  156. for i:=firstop to lastop do
  157. iasmops.Add(upper(std_op2str[i]),Pointer(PtrInt(i)));
  158. actoperextention := '';
  159. end;
  160. {---------------------------------------------------------------------}
  161. { Routines for the tokenizing }
  162. {---------------------------------------------------------------------}
  163. function tx86intreader.is_asmopcode(const s: string):boolean;
  164. var
  165. cond : string[4];
  166. cnd : tasmcond;
  167. j: longint;
  168. Begin
  169. is_asmopcode:=FALSE;
  170. actopcode:=A_None;
  171. actcondition:=C_None;
  172. actopsize:=S_NO;
  173. { Search opcodes }
  174. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  175. if actopcode<>A_NONE then
  176. begin
  177. actasmtoken:=AS_OPCODE;
  178. result:=TRUE;
  179. exit;
  180. end;
  181. { not found yet, check condition opcodes }
  182. j:=0;
  183. while (j<CondAsmOps) do
  184. begin
  185. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  186. begin
  187. cond:=Copy(s,Length(CondAsmOpStr[j])+1,255);
  188. if cond<>'' then
  189. begin
  190. for cnd:=low(TasmCond) to high(TasmCond) do
  191. if Cond=Upper(cond2str[cnd]) then
  192. begin
  193. actopcode:=CondASmOp[j];
  194. actcondition:=cnd;
  195. is_asmopcode:=TRUE;
  196. actasmtoken:=AS_OPCODE;
  197. exit
  198. end;
  199. end;
  200. end;
  201. inc(j);
  202. end;
  203. end;
  204. function tx86intreader.is_asmoperator(const s: string):boolean;
  205. var
  206. i : tasmtoken;
  207. Begin
  208. for i:=firstoperator to lastoperator do
  209. if s=_asmoperators[i] then
  210. begin
  211. actasmtoken:=i;
  212. is_asmoperator:=true;
  213. exit;
  214. end;
  215. is_asmoperator:=false;
  216. end;
  217. Function tx86intreader.is_asmdirective(const s: string):boolean;
  218. var
  219. i : tasmtoken;
  220. Begin
  221. for i:=firstdirective to lastdirective do
  222. if s=_asmdirectives[i] then
  223. begin
  224. actasmtoken:=i;
  225. is_asmdirective:=true;
  226. exit;
  227. end;
  228. is_asmdirective:=false;
  229. end;
  230. function tx86intreader.is_register(const s:string):boolean;
  231. begin
  232. is_register:=false;
  233. actasmregister:=masm_regnum_search(lower(s));
  234. { don't acceps "flags" as register name in an instruction }
  235. if (getsupreg(actasmregister)=RS_DEFAULTFLAGS) and (getregtype(actasmregister)=getregtype(NR_DEFAULTFLAGS)) then
  236. actasmregister:=NR_NO;
  237. if actasmregister<>NR_NO then
  238. begin
  239. is_register:=true;
  240. actasmtoken:=AS_REGISTER;
  241. end;
  242. end;
  243. function tx86intreader.is_locallabel(const s:string):boolean;
  244. begin
  245. is_locallabel:=(length(s)>1) and (s[1]='@');
  246. end;
  247. function tx86intreader.is_targetdirective(const s: string): boolean;
  248. begin
  249. result:=false;
  250. end;
  251. procedure tx86intreader.handletargetdirective;
  252. begin
  253. end;
  254. Procedure tx86intreader.GetToken(check_operand_extention: boolean);
  255. var
  256. len : longint;
  257. forcelabel : boolean;
  258. srsym : tsym;
  259. srsymtable : TSymtable;
  260. begin
  261. actoperextention := '';
  262. c:=scanner.c;
  263. { save old token and reset new token }
  264. prevasmtoken:=actasmtoken;
  265. actasmtoken:=AS_NONE;
  266. { reset }
  267. forcelabel:=FALSE;
  268. actasmpattern:='';
  269. { while space and tab , continue scan... }
  270. while (c in [' ',#9]) do
  271. c:=current_scanner.asmgetchar;
  272. { get token pos }
  273. if not (c in [#10,#13,'{',';','(','/']) then
  274. current_scanner.gettokenpos;
  275. { Local Label, Label, Directive, Prefix or Opcode }
  276. if firsttoken and not (c in [#10,#13,'{',';','(','/']) then
  277. begin
  278. firsttoken:=FALSE;
  279. len:=0;
  280. { directive check }
  281. if c = '.' then
  282. begin
  283. actasmpattern:='.';
  284. c:=current_scanner.asmgetchar;
  285. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  286. begin
  287. actasmpattern:=actasmpattern+c;
  288. c:=current_scanner.asmgetchar;
  289. end;
  290. { directives are case sensitive!! }
  291. if is_asmdirective(actasmpattern) then
  292. exit;
  293. if is_targetdirective(actasmpattern) then
  294. begin
  295. actasmtoken:=AS_TARGET_DIRECTIVE;
  296. exit;
  297. end;
  298. Message1(asmr_e_not_directive_or_local_symbol,actasmpattern);
  299. exit;
  300. end;
  301. while (c in ['A'..'Z','a'..'z','0'..'9','_','@']) or
  302. { TP7 also allows $&? characters in local labels }
  303. (forcelabel and (c in ['$','&','?'])) do
  304. begin
  305. { if there is an at_sign, then this must absolutely be a label }
  306. if c = '@' then
  307. forcelabel:=TRUE;
  308. inc(len);
  309. actasmpattern[len]:=c;
  310. c:=current_scanner.asmgetchar;
  311. end;
  312. actasmpattern[0]:=chr(len);
  313. actasmpattern_origcase:=actasmpattern;
  314. uppervar(actasmpattern);
  315. { allow spaces }
  316. while (c in [' ',#9]) do
  317. c:=current_scanner.asmgetchar;
  318. { label ? }
  319. if c = ':' then
  320. begin
  321. if actasmpattern[1]='@' then
  322. actasmtoken:=AS_LLABEL
  323. else
  324. actasmtoken:=AS_LABEL;
  325. { let us point to the next character }
  326. c:=current_scanner.asmgetchar;
  327. firsttoken:=true;
  328. exit;
  329. end;
  330. { Are we trying to create an identifier with }
  331. { an at-sign...? }
  332. if forcelabel then
  333. Message(asmr_e_none_label_contain_at);
  334. { opcode ? }
  335. If is_asmopcode(actasmpattern) then
  336. Begin
  337. { check if we are in an expression }
  338. { then continue with asm directives }
  339. if not inexpression then
  340. exit;
  341. end;
  342. if is_asmdirective(actasmpattern) then
  343. exit;
  344. message1(asmr_e_unknown_opcode,actasmpattern);
  345. actasmtoken:=AS_NONE;
  346. exit;
  347. end
  348. else { else firsttoken }
  349. begin
  350. case c of
  351. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  352. { - @Result, @Code or @Data special variables. }
  353. begin
  354. actasmpattern:=c;
  355. c:=current_scanner.asmgetchar;
  356. while c in ['A'..'Z','a'..'z','0'..'9','_','@','$','&','?'] do
  357. begin
  358. actasmpattern:=actasmpattern + c;
  359. c:=current_scanner.asmgetchar;
  360. end;
  361. actasmpattern_origcase:=actasmpattern;
  362. uppervar(actasmpattern);
  363. actasmtoken:=AS_ID;
  364. exit;
  365. end;
  366. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  367. begin
  368. actasmpattern:=c;
  369. c:=current_scanner.asmgetchar;
  370. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  371. begin
  372. actasmpattern:=actasmpattern + c;
  373. c:=current_scanner.asmgetchar;
  374. end;
  375. actasmpattern_origcase:=actasmpattern;
  376. uppervar(actasmpattern);
  377. { after prefix (or segment override) we allow also a new opcode }
  378. If (is_prefix(actopcode) or is_override(actopcode)) and is_asmopcode(actasmpattern) then
  379. Begin
  380. { if we are not in a constant }
  381. { expression than this is an }
  382. { opcode. }
  383. if not inexpression then
  384. exit;
  385. end;
  386. { support st(X) for fpu registers }
  387. if (actasmpattern = 'ST') and (c='(') then
  388. Begin
  389. actasmpattern:=actasmpattern+c;
  390. c:=current_scanner.asmgetchar;
  391. { allow spaces }
  392. while (c in [' ',#9]) do
  393. c:=current_scanner.asmgetchar;
  394. if c in ['0'..'7'] then
  395. actasmpattern:=actasmpattern + c
  396. else
  397. Message(asmr_e_invalid_fpu_register);
  398. c:=current_scanner.asmgetchar;
  399. { allow spaces }
  400. while (c in [' ',#9]) do
  401. c:=current_scanner.asmgetchar;
  402. if c <> ')' then
  403. Message(asmr_e_invalid_fpu_register)
  404. else
  405. Begin
  406. actasmpattern:=actasmpattern + c;
  407. c:=current_scanner.asmgetchar;
  408. end;
  409. end;
  410. //if prevasmtoken in [AS_LOPMASK,AS_LOPZEROMASK,AS_LOPBCST,AS_LOPSAE,AS_LOPER] then
  411. if prevasmtoken in OPEXT_STARTASMTOKEN then
  412. begin
  413. if (prevasmtoken = AS_LOPER) and (c = '-') then
  414. begin
  415. actasmpattern := actasmpattern + c;
  416. c:=current_scanner.asmgetchar;
  417. while c in ['A'..'Z','a'..'z'] do
  418. begin
  419. actasmpattern:=actasmpattern + c;
  420. c:=current_scanner.asmgetchar;
  421. end;
  422. end;
  423. { allow spaces }
  424. while (c in [' ',#9]) do
  425. c:=current_scanner.asmgetchar;
  426. if c = '}' then
  427. begin
  428. current_scanner.readchar;
  429. case prevasmtoken of
  430. AS_LOPMASK: if (length(actasmpattern) = 2) and
  431. (actasmpattern[2] in ['1'..'7']) then
  432. begin
  433. actasmtoken := AS_VOPMASK;
  434. end;
  435. AS_LOPZEROMASK:
  436. if (actasmpattern = 'z') or
  437. (actasmpattern = 'Z') then
  438. begin
  439. actasmtoken := AS_VOPZEROMASK;
  440. end;
  441. AS_LOPBCST:
  442. begin
  443. actasmpattern_origcase:=actasmpattern;
  444. uppervar(actasmpattern);
  445. if (actasmpattern = '1TO2') then actasmtoken := AS_OPBCST1TO2
  446. else if (actasmpattern = '1TO4') then actasmtoken := AS_OPBCST1TO4
  447. else if (actasmpattern = '1TO8') then actasmtoken := AS_OPBCST1TO8
  448. else if (actasmpattern = '1TO16') then actasmtoken := AS_OPBCST1TO16
  449. else actasmpattern := actasmpattern_origcase;
  450. end;
  451. AS_LOPSAE:
  452. begin
  453. actasmpattern_origcase:=actasmpattern;
  454. uppervar(actasmpattern);
  455. if (actasmpattern = 'SAE') then actasmtoken := AS_OPSAE
  456. else actasmpattern := actasmpattern_origcase;
  457. end;
  458. AS_LOPER:
  459. begin
  460. actasmpattern_origcase:=actasmpattern;
  461. uppervar(actasmpattern);
  462. if (actasmpattern = 'RD-SAE') then actasmtoken := AS_OPRDSAE
  463. else if (actasmpattern = 'RN-SAE') then actasmtoken := AS_OPRNSAE
  464. else if (actasmpattern = 'RU-SAE') then actasmtoken := AS_OPRUSAE
  465. else if (actasmpattern = 'RZ-SAE') then actasmtoken := AS_OPRZSAE
  466. else actasmpattern := actasmpattern_origcase;
  467. end
  468. else ; // is completely comment =>> nothing todo
  469. end;
  470. exit;
  471. end
  472. else
  473. begin
  474. if c = '{' then current_scanner.inc_comment_level;
  475. current_scanner.skipcomment(false); // is comment
  476. actasmpattern := '';
  477. actasmtoken := AS_NONE;
  478. exit;
  479. end;
  480. end;
  481. if is_asmdirective(actasmpattern) then
  482. exit;
  483. if is_asmoperator(actasmpattern) then
  484. exit;
  485. if is_register(actasmpattern) then
  486. exit;
  487. { allow spaces }
  488. while (c in [' ',#9]) do
  489. c:=current_scanner.asmgetchar;
  490. { if next is a '.' and this is a unitsym then we also need to
  491. parse the identifier }
  492. if (c='.') then
  493. begin
  494. asmsearchsym(actasmpattern,srsym,srsymtable);
  495. if assigned(srsym) and
  496. (srsym.typ=unitsym) and
  497. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  498. srsym.owner.iscurrentunit then
  499. begin
  500. { Add . to create System.Identifier }
  501. actasmpattern:=actasmpattern+c;
  502. c:=current_scanner.asmgetchar;
  503. { Delphi allows System.@Halt, just ignore the @ }
  504. if c='@' then
  505. c:=current_scanner.asmgetchar;
  506. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  507. begin
  508. actasmpattern:=actasmpattern + upcase(c);
  509. c:=current_scanner.asmgetchar;
  510. end;
  511. end;
  512. end;
  513. actasmtoken:=AS_ID;
  514. exit;
  515. end;
  516. '''' : { string or character }
  517. begin
  518. actasmpattern:='';
  519. repeat
  520. if c = '''' then
  521. begin
  522. c:=current_scanner.asmgetchar;
  523. if c in [#10,#13] then
  524. begin
  525. Message(scan_f_string_exceeds_line);
  526. break;
  527. end;
  528. repeat
  529. if c='''' then
  530. begin
  531. c:=current_scanner.asmgetchar;
  532. if c='''' then
  533. begin
  534. actasmpattern:=actasmpattern+'''';
  535. c:=current_scanner.asmgetchar;
  536. if c in [#10,#13] then
  537. begin
  538. Message(scan_f_string_exceeds_line);
  539. break;
  540. end;
  541. end
  542. else
  543. break;
  544. end
  545. else
  546. begin
  547. actasmpattern:=actasmpattern+c;
  548. c:=current_scanner.asmgetchar;
  549. if c in [#10,#13] then
  550. begin
  551. Message(scan_f_string_exceeds_line);
  552. break
  553. end;
  554. end;
  555. until false; { end repeat }
  556. end
  557. else
  558. break; { end if }
  559. until false;
  560. actasmtoken:=AS_STRING;
  561. exit;
  562. end;
  563. '"' : { string or character }
  564. begin
  565. actasmpattern:='';
  566. repeat
  567. if c = '"' then
  568. begin
  569. c:=current_scanner.asmgetchar;
  570. if c in [#10,#13] then
  571. begin
  572. Message(scan_f_string_exceeds_line);
  573. break;
  574. end;
  575. repeat
  576. if c='"' then
  577. begin
  578. c:=current_scanner.asmgetchar;
  579. if c='"' then
  580. begin
  581. actasmpattern:=actasmpattern+'"';
  582. c:=current_scanner.asmgetchar;
  583. if c in [#10,#13] then
  584. begin
  585. Message(scan_f_string_exceeds_line);
  586. break;
  587. end;
  588. end
  589. else
  590. break;
  591. end
  592. else
  593. begin
  594. actasmpattern:=actasmpattern+c;
  595. c:=current_scanner.asmgetchar;
  596. if c in [#10,#13] then
  597. begin
  598. Message(scan_f_string_exceeds_line);
  599. break
  600. end;
  601. end;
  602. until false; { end repeat }
  603. end
  604. else
  605. break; { end if }
  606. until false;
  607. actasmtoken:=AS_STRING;
  608. exit;
  609. end;
  610. '$' :
  611. begin
  612. c:=current_scanner.asmgetchar;
  613. while c in ['0'..'9','A'..'F','a'..'f'] do
  614. begin
  615. actasmpattern:=actasmpattern + c;
  616. c:=current_scanner.asmgetchar;
  617. end;
  618. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  619. actasmtoken:=AS_INTNUM;
  620. exit;
  621. end;
  622. '&' : { identifier }
  623. begin
  624. actasmpattern:='';
  625. c:=current_scanner.asmgetchar;
  626. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  627. begin
  628. actasmpattern:=actasmpattern + c;
  629. c:=current_scanner.asmgetchar;
  630. end;
  631. actasmpattern_origcase:=actasmpattern;
  632. uppervar(actasmpattern);
  633. actasmtoken:=AS_ID;
  634. exit;
  635. end;
  636. ',' :
  637. begin
  638. actasmtoken:=AS_COMMA;
  639. c:=current_scanner.asmgetchar;
  640. exit;
  641. end;
  642. '[' :
  643. begin
  644. actasmtoken:=AS_LBRACKET;
  645. c:=current_scanner.asmgetchar;
  646. exit;
  647. end;
  648. ']' :
  649. begin
  650. actasmtoken:=AS_RBRACKET;
  651. c:=current_scanner.asmgetchar;
  652. exit;
  653. end;
  654. '(' :
  655. begin
  656. c:=current_scanner.asmgetchar;
  657. if c='*' then
  658. begin
  659. current_scanner.skipoldtpcomment(true);
  660. GetToken;
  661. end
  662. else
  663. actasmtoken:=AS_LPAREN;
  664. exit;
  665. end;
  666. ')' :
  667. begin
  668. actasmtoken:=AS_RPAREN;
  669. c:=current_scanner.asmgetchar;
  670. exit;
  671. end;
  672. ':' :
  673. begin
  674. actasmtoken:=AS_COLON;
  675. c:=current_scanner.asmgetchar;
  676. exit;
  677. end;
  678. '.' :
  679. begin
  680. c:=current_scanner.asmgetchar;
  681. {$ifdef x86_64}
  682. if c='.' then
  683. begin
  684. actasmpattern:='..';
  685. c:=current_scanner.asmgetchar;
  686. repeat
  687. actasmpattern:=actasmpattern+c;
  688. c:=current_scanner.asmgetchar;
  689. until not(c in ['A'..'Z','a'..'z','0'..'9','_']);
  690. if upper(actasmpattern)<>'..GOTPCREL' then
  691. begin
  692. actasmtoken:=AS_ID;
  693. consume(AS___GOTPCREL);
  694. end;
  695. actasmtoken:=AS___GOTPCREL
  696. end
  697. else
  698. {$endif x86_64}
  699. actasmtoken:=AS_DOT;
  700. exit;
  701. end;
  702. '+' :
  703. begin
  704. actasmtoken:=AS_PLUS;
  705. c:=current_scanner.asmgetchar;
  706. exit;
  707. end;
  708. '-' :
  709. begin
  710. actasmtoken:=AS_MINUS;
  711. c:=current_scanner.asmgetchar;
  712. exit;
  713. end;
  714. '*' :
  715. begin
  716. actasmtoken:=AS_STAR;
  717. c:=current_scanner.asmgetchar;
  718. exit;
  719. end;
  720. '/' :
  721. begin
  722. c:=current_scanner.asmgetchar;
  723. if c='/' then
  724. begin
  725. current_scanner.skipdelphicomment;
  726. GetToken;
  727. end
  728. else
  729. actasmtoken:=AS_SLASH;
  730. exit;
  731. end;
  732. '0'..'9':
  733. begin
  734. if prevasmtoken = AS_LOPBCST then
  735. begin
  736. actasmpattern:=c;
  737. c:=current_scanner.asmgetchar;
  738. { Get the possible characters }
  739. while c in ['1','2','4','6','8','t','T','o','O'] do
  740. begin
  741. actasmpattern:=actasmpattern + c;
  742. c:=current_scanner.asmgetchar;
  743. end;
  744. while (c in [' ',#9]) do
  745. c:=current_scanner.asmgetchar;
  746. if c = '}' then
  747. begin
  748. actasmpattern_origcase:=actasmpattern;
  749. uppervar(actasmpattern);
  750. if (actasmpattern = '1TO2') then actasmtoken := AS_OPBCST1TO2
  751. else if (actasmpattern = '1TO4') then actasmtoken := AS_OPBCST1TO4
  752. else if (actasmpattern = '1TO8') then actasmtoken := AS_OPBCST1TO8
  753. else if (actasmpattern = '1TO16') then actasmtoken := AS_OPBCST1TO16
  754. else actasmpattern := actasmpattern_origcase;
  755. c:=current_scanner.asmgetchar;
  756. end
  757. else
  758. begin
  759. if c = '{' then current_scanner.inc_comment_level;
  760. current_scanner.skipcomment(false); // is comment
  761. end;
  762. actasmpattern := '';
  763. exit;
  764. end
  765. else
  766. begin
  767. actasmpattern:=c;
  768. c:=current_scanner.asmgetchar;
  769. { Get the possible characters }
  770. while c in ['0'..'9','A'..'F','a'..'f'] do
  771. begin
  772. actasmpattern:=actasmpattern + c;
  773. c:=current_scanner.asmgetchar;
  774. end;
  775. { Get ending character }
  776. actasmpattern_origcase:=actasmpattern;
  777. uppervar(actasmpattern);
  778. c:=upcase(c);
  779. { possibly a binary number. }
  780. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  781. Begin
  782. { Delete the last binary specifier }
  783. delete(actasmpattern,length(actasmpattern),1);
  784. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  785. actasmtoken:=AS_INTNUM;
  786. exit;
  787. end
  788. else
  789. Begin
  790. case c of
  791. 'O' :
  792. Begin
  793. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  794. actasmtoken:=AS_INTNUM;
  795. c:=current_scanner.asmgetchar;
  796. exit;
  797. end;
  798. 'H' :
  799. Begin
  800. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  801. actasmtoken:=AS_INTNUM;
  802. c:=current_scanner.asmgetchar;
  803. exit;
  804. end;
  805. else { must be an integer number }
  806. begin
  807. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  808. actasmtoken:=AS_INTNUM;
  809. exit;
  810. end;
  811. end;
  812. end;
  813. end;
  814. end;
  815. #13,#10:
  816. begin
  817. current_scanner.linebreak;
  818. c:=current_scanner.asmgetchar;
  819. firsttoken:=TRUE;
  820. actasmtoken:=AS_SEPARATOR;
  821. exit;
  822. end;
  823. ';':
  824. begin
  825. c:=current_scanner.asmgetchar;
  826. firsttoken:=TRUE;
  827. actasmtoken:=AS_SEPARATOR;
  828. exit;
  829. end;
  830. '{':
  831. begin
  832. if not(check_operand_extention) then current_scanner.skipcomment(true)
  833. else // exists operand extention e.g. AVX512 {k1..k7} or {z} or {1to8}
  834. begin
  835. case current_scanner.asmgetchar of
  836. '{': begin
  837. current_scanner.inc_comment_level;
  838. current_scanner.skipcomment(true);
  839. end;
  840. '}': ; // local comment closed
  841. 'k',
  842. 'K': begin
  843. actasmtoken := AS_LOPMASK;
  844. exit;
  845. end;
  846. 'z',
  847. 'Z': begin
  848. actasmtoken := AS_LOPZEROMASK;
  849. exit;
  850. end;
  851. '1': begin
  852. actasmtoken := AS_LOPBCST;
  853. exit;
  854. end;
  855. 's',
  856. 'S': begin
  857. actasmtoken := AS_LOPSAE;
  858. exit;
  859. end;
  860. 'r',
  861. 'R': begin
  862. actasmtoken := AS_LOPER;
  863. exit;
  864. end;
  865. else begin
  866. current_scanner.skipcomment(false);
  867. end;
  868. end;
  869. end;
  870. GetToken(check_operand_extention);
  871. end;
  872. else
  873. current_scanner.illegal_char(c);
  874. end;
  875. end;
  876. end;
  877. function tx86intreader.consume(t : tasmtoken; check_operand_extention: boolean):boolean;
  878. begin
  879. Consume:=true;
  880. if t<>actasmtoken then
  881. begin
  882. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  883. Consume:=false;
  884. end;
  885. repeat
  886. gettoken(check_operand_extention);
  887. until actasmtoken<>AS_NONE;
  888. end;
  889. //procedure tx86intreader.ConsumeOperExtention(oper: tx86operand; const aOperExtention: string);
  890. //begin
  891. // //if oper.reg
  892. //end;
  893. procedure tx86intreader.consume_voperand_ext(aop: tx86operand; aConsumeVOpExt: boolean);
  894. var
  895. kreg: tregister;
  896. begin
  897. Consume(actasmtoken, true);
  898. if actasmtoken in [AS_VOPMASK, AS_VOPZEROMASK, AS_OPBCST1TO2, AS_OPBCST1TO4, AS_OPBCST1TO8, AS_OPBCST1TO16,
  899. AS_OPSAE,AS_OPRNSAE,AS_OPRDSAE,AS_OPRUSAE,AS_OPRZSAE] then
  900. begin
  901. case actasmtoken of
  902. AS_VOPMASK: begin
  903. kreg := masm_regnum_search(lower(actasmpattern));
  904. if (kreg >= NR_K1) and
  905. (kreg <= NR_K7) then
  906. begin
  907. aop.vopext := aop.vopext or (tregisterrec(kreg).supreg and $07);
  908. aop.vopext := aop.vopext or OTVE_VECTOR_WRITEMASK;
  909. end;
  910. end;
  911. AS_VOPZEROMASK: aop.vopext := aop.vopext or OTVE_VECTOR_ZERO;
  912. AS_OPBCST1TO2: begin
  913. aop.vopext := aop.vopext or OTVE_VECTOR_BCST or OTVE_VECTOR_BCST2;
  914. aop.vbcst := 2;
  915. end;
  916. AS_OPBCST1TO4: begin
  917. aop.vopext := aop.vopext or OTVE_VECTOR_BCST or OTVE_VECTOR_BCST4;
  918. aop.vbcst := 4;
  919. end;
  920. AS_OPBCST1TO8: begin
  921. aop.vopext := aop.vopext or OTVE_VECTOR_BCST or OTVE_VECTOR_BCST8;
  922. aop.vbcst := 8;
  923. end;
  924. AS_OPBCST1TO16: begin
  925. aop.vopext := aop.vopext or OTVE_VECTOR_BCST or OTVE_VECTOR_BCST16;
  926. aop.vbcst := 16;
  927. end;
  928. AS_OPSAE: aop.vopext := aop.vopext or OTVE_VECTOR_SAE;
  929. AS_OPRNSAE: aop.vopext := aop.vopext or OTVE_VECTOR_RNSAE;
  930. AS_OPRDSAE: aop.vopext := aop.vopext or OTVE_VECTOR_RDSAE;
  931. AS_OPRUSAE: aop.vopext := aop.vopext or OTVE_VECTOR_RUSAE;
  932. AS_OPRZSAE: aop.vopext := aop.vopext or OTVE_VECTOR_RZSAE;
  933. else
  934. Internalerror(2019081009);
  935. end;
  936. if aConsumeVOpExt then
  937. Consume(actasmtoken, true);
  938. end;
  939. end;
  940. procedure tx86intreader.RecoverConsume(allowcomma:boolean);
  941. begin
  942. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  943. begin
  944. if allowcomma and (actasmtoken=AS_COMMA) then
  945. break;
  946. Consume(actasmtoken);
  947. end;
  948. end;
  949. {*****************************************************************************
  950. Parsing Helpers
  951. *****************************************************************************}
  952. { Adds two references (dest:=dest+src) }
  953. procedure tx86intreader.AddReferences(dest,src : tx86operand);
  954. procedure AddRegister(reg:tregister;scalefactor:byte);
  955. begin
  956. if reg=NR_NO then
  957. exit;
  958. if (dest.opr.ref.base=NR_NO) and (scalefactor=1) then
  959. begin
  960. dest.opr.ref.base:=reg;
  961. exit;
  962. end;
  963. if dest.opr.ref.index=NR_NO then
  964. begin
  965. dest.opr.ref.index:=reg;
  966. dest.opr.ref.scalefactor:=scalefactor;
  967. exit;
  968. end;
  969. if dest.opr.ref.index=reg then
  970. begin
  971. Inc(dest.opr.ref.scalefactor,scalefactor);
  972. exit;
  973. end;
  974. Message(asmr_e_multiple_index);
  975. end;
  976. var
  977. tmplocal: TOprRec;
  978. segreg: TRegister;
  979. begin
  980. case dest.opr.typ of
  981. OPR_REFERENCE:
  982. begin
  983. case src.opr.typ of
  984. OPR_REFERENCE:
  985. begin
  986. AddRegister(src.opr.ref.base,1);
  987. AddRegister(src.opr.ref.index,src.opr.ref.scalefactor);
  988. if src.opr.ref.segment<>NR_NO then
  989. SetSegmentOverride(dest,src.opr.ref.segment);
  990. Inc(dest.opr.ref.offset,src.opr.ref.offset);
  991. Inc(dest.opr.constoffset,src.opr.constoffset);
  992. dest.haslabelref:=dest.haslabelref or src.haslabelref;
  993. dest.hasproc:=dest.hasproc or src.hasproc;
  994. dest.hasvar:=dest.hasvar or src.hasvar;
  995. if assigned(src.opr.ref.symbol) then
  996. begin
  997. if assigned(dest.opr.ref.symbol) then
  998. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  999. dest.opr.ref.symbol:=src.opr.ref.symbol;
  1000. end;
  1001. if assigned(src.opr.ref.relsymbol) then
  1002. begin
  1003. if assigned(dest.opr.ref.relsymbol) then
  1004. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1005. dest.opr.ref.relsymbol:=src.opr.ref.relsymbol;
  1006. end;
  1007. if dest.opr.ref.refaddr=addr_no then
  1008. dest.opr.ref.refaddr:=src.opr.ref.refaddr;
  1009. end;
  1010. OPR_LOCAL:
  1011. begin
  1012. tmplocal:=src.opr;
  1013. if dest.opr.ref.base<>NR_NO then
  1014. begin
  1015. if tmplocal.localindexreg=NR_NO then
  1016. begin
  1017. tmplocal.localindexreg:=dest.opr.ref.base;
  1018. tmplocal.localscale:=0;
  1019. end
  1020. else if tmplocal.localindexreg=dest.opr.ref.base then
  1021. tmplocal.localscale:=Min(tmplocal.localscale,1)+1
  1022. else
  1023. Message(asmr_e_multiple_index);
  1024. end;
  1025. if dest.opr.ref.index<>NR_NO then
  1026. begin
  1027. if tmplocal.localindexreg=NR_NO then
  1028. begin
  1029. tmplocal.localindexreg:=dest.opr.ref.index;
  1030. tmplocal.localscale:=dest.opr.ref.scalefactor;
  1031. end
  1032. else if tmplocal.localindexreg=dest.opr.ref.index then
  1033. tmplocal.localscale:=Min(tmplocal.localscale,1)+Min(dest.opr.ref.scalefactor,1)
  1034. else
  1035. Message(asmr_e_multiple_index);
  1036. end;
  1037. Inc(tmplocal.localconstoffset,dest.opr.constoffset);
  1038. Inc(tmplocal.localsymofs,dest.opr.ref.offset);
  1039. segreg:=dest.opr.ref.segment;
  1040. dest.opr:=tmplocal;
  1041. if segreg<>NR_NO then
  1042. SetSegmentOverride(dest,segreg);
  1043. end;
  1044. else
  1045. internalerror(2018030704);
  1046. end;
  1047. end;
  1048. OPR_LOCAL:
  1049. begin
  1050. case src.opr.typ of
  1051. OPR_REFERENCE:
  1052. begin
  1053. if src.opr.ref.base<>NR_NO then
  1054. begin
  1055. if dest.opr.localindexreg=NR_NO then
  1056. begin
  1057. dest.opr.localindexreg:=src.opr.ref.base;
  1058. dest.opr.localscale:=0;
  1059. end
  1060. else if dest.opr.localindexreg=src.opr.ref.base then
  1061. dest.opr.localscale:=Min(dest.opr.localscale,1)+1
  1062. else
  1063. Message(asmr_e_multiple_index);
  1064. end;
  1065. if src.opr.ref.index<>NR_NO then
  1066. begin
  1067. if dest.opr.localindexreg=NR_NO then
  1068. begin
  1069. dest.opr.localindexreg:=src.opr.ref.index;
  1070. dest.opr.localscale:=src.opr.ref.scalefactor;
  1071. end
  1072. else if dest.opr.localindexreg=src.opr.ref.index then
  1073. dest.opr.localscale:=Min(dest.opr.localscale,1)+Min(src.opr.ref.scalefactor,1)
  1074. else
  1075. Message(asmr_e_multiple_index);
  1076. end;
  1077. Inc(dest.opr.localconstoffset,src.opr.constoffset);
  1078. Inc(dest.opr.localsymofs,src.opr.ref.offset);
  1079. if src.opr.ref.segment<>NR_NO then
  1080. SetSegmentOverride(dest,src.opr.ref.segment);
  1081. end;
  1082. OPR_LOCAL:
  1083. Message(asmr_e_no_local_or_para_allowed);
  1084. else
  1085. internalerror(2018030703);
  1086. end;
  1087. end;
  1088. else
  1089. internalerror(2018030702);
  1090. end;
  1091. end;
  1092. procedure tx86intreader.SetSegmentOverride(oper:tx86operand;seg:tregister);
  1093. begin
  1094. if not is_segment_reg(seg) then
  1095. Message(asmr_e_invalid_seg_override);
  1096. {$ifdef x86_64}
  1097. if (seg=NR_CS) or (seg=NR_DS) or (seg=NR_SS) or (seg=NR_ES) then
  1098. Message1(asmr_w_segment_override_ignored_in_64bit_mode,masm_regname(seg));
  1099. {$endif x86_64}
  1100. case oper.opr.typ of
  1101. OPR_REFERENCE:
  1102. begin
  1103. if oper.opr.ref.segment<>NR_NO then
  1104. begin
  1105. if m_tp7 in current_settings.modeswitches then
  1106. Message(asmr_w_multiple_segment_overrides)
  1107. else
  1108. Message(asmr_e_multiple_segment_overrides);
  1109. end;
  1110. oper.opr.ref.segment:=seg;
  1111. end;
  1112. OPR_LOCAL:
  1113. begin
  1114. if oper.opr.localsegment<>NR_NO then
  1115. begin
  1116. if m_tp7 in current_settings.modeswitches then
  1117. Message(asmr_w_multiple_segment_overrides)
  1118. else
  1119. Message(asmr_e_multiple_segment_overrides);
  1120. end;
  1121. oper.opr.localsegment:=seg;
  1122. end;
  1123. else
  1124. internalerror(2018030705);
  1125. end;
  1126. end;
  1127. { This routine builds up a record offset after a AS_DOT
  1128. token is encountered.
  1129. On entry actasmtoken should be equal to AS_DOT }
  1130. Procedure tx86intreader.BuildRecordOffsetSize(const expr: string;out offset:tcgint;out size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean);
  1131. var
  1132. s: string;
  1133. Begin
  1134. offset:=0;
  1135. size:=0;
  1136. mangledname:='';
  1137. hastypecast:=false;
  1138. s:=expr;
  1139. while (actasmtoken=AS_DOT) do
  1140. begin
  1141. Consume(AS_DOT);
  1142. if actasmtoken in [AS_BYTE,AS_ID,AS_WORD,AS_DWORD,AS_QWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_ZWORD,AS_ZMMWORD,AS_REGISTER] then
  1143. begin
  1144. s:=s+'.'+actasmpattern;
  1145. consume(actasmtoken);
  1146. end
  1147. else
  1148. begin
  1149. Consume(AS_ID);
  1150. RecoverConsume(true);
  1151. break;
  1152. end;
  1153. end;
  1154. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs,hastypecast) then
  1155. Message(asmr_e_building_record_offset);
  1156. end;
  1157. Procedure tx86intreader.BuildConstSymbolExpression(in_flags: tconstsymbolexpressioninputflags;out value:tcgint;out asmsym:string;out asmsymtyp:TAsmsymtype;out size:tcgint;out out_flags:tconstsymbolexpressionoutputflags);
  1158. var
  1159. tempstr,expr,hs,mangledname : string;
  1160. parenlevel : longint;
  1161. l,k : tcgint;
  1162. hasparen,
  1163. errorflag,
  1164. needvmtofs : boolean;
  1165. prevtok : tasmtoken;
  1166. hl : tasmlabel;
  1167. hssymtyp : Tasmsymtype;
  1168. def : tdef;
  1169. sym : tsym;
  1170. srsymtable : TSymtable;
  1171. hastypecast : boolean;
  1172. Begin
  1173. { reset }
  1174. value:=0;
  1175. asmsym:='';
  1176. asmsymtyp:=AT_DATA;
  1177. size:=0;
  1178. out_flags:=[];
  1179. errorflag:=FALSE;
  1180. tempstr:='';
  1181. expr:='';
  1182. if cseif_startingminus in in_flags then
  1183. expr:='-';
  1184. inexpression:=TRUE;
  1185. parenlevel:=0;
  1186. sym:=nil;
  1187. needvmtofs:=FALSE;
  1188. Repeat
  1189. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  1190. if (cseif_isref in in_flags) and (actasmtoken=AS_LBRACKET) then
  1191. break;
  1192. if (cseif_referencelike in in_flags) and
  1193. (actasmtoken in [AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_DQWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD]) then
  1194. begin
  1195. { Support ugly tp7 and delphi constructs like 'DD DWORD PTR 5' }
  1196. Consume(actasmtoken);
  1197. Consume(AS_PTR);
  1198. end;
  1199. if (cseif_referencelike in in_flags) and
  1200. (actasmtoken in [AS_LBRACKET,AS_RBRACKET]) then
  1201. case actasmtoken of
  1202. AS_LBRACKET:
  1203. begin
  1204. Consume(AS_LBRACKET);
  1205. if (length(expr)>0) and
  1206. not (expr[length(expr)] in ['+','-']) then
  1207. expr:=expr+'+';
  1208. expr:=expr+'[';
  1209. end;
  1210. AS_RBRACKET:
  1211. begin
  1212. Consume(AS_RBRACKET);
  1213. expr:=expr+']';
  1214. end;
  1215. else
  1216. ;
  1217. end;
  1218. Case actasmtoken of
  1219. AS_LPAREN:
  1220. Begin
  1221. Consume(AS_LPAREN);
  1222. expr:=expr + '(';
  1223. inc(parenlevel);
  1224. end;
  1225. AS_RPAREN:
  1226. Begin
  1227. { Keep the AS_PAREN in actasmtoken, it is maybe a typecast }
  1228. if parenlevel=0 then
  1229. break;
  1230. Consume(AS_RPAREN);
  1231. expr:=expr + ')';
  1232. dec(parenlevel);
  1233. end;
  1234. AS_SHL:
  1235. Begin
  1236. Consume(AS_SHL);
  1237. expr:=expr + '<';
  1238. end;
  1239. AS_SHR:
  1240. Begin
  1241. Consume(AS_SHR);
  1242. expr:=expr + '>';
  1243. end;
  1244. AS_SLASH:
  1245. Begin
  1246. Consume(AS_SLASH);
  1247. expr:=expr + '/';
  1248. end;
  1249. AS_MOD:
  1250. Begin
  1251. Consume(AS_MOD);
  1252. expr:=expr + '%';
  1253. end;
  1254. AS_STAR:
  1255. Begin
  1256. Consume(AS_STAR);
  1257. if (cseif_isref in in_flags) and (actasmtoken=AS_REGISTER) then
  1258. break;
  1259. expr:=expr + '*';
  1260. end;
  1261. AS_PLUS:
  1262. Begin
  1263. Consume(AS_PLUS);
  1264. if (cseif_isref in in_flags) and ((actasmtoken=AS_REGISTER) or (actasmtoken=AS_LBRACKET)) then
  1265. break;
  1266. expr:=expr + '+';
  1267. end;
  1268. AS_MINUS:
  1269. Begin
  1270. Consume(AS_MINUS);
  1271. expr:=expr + '-';
  1272. end;
  1273. AS_AND:
  1274. Begin
  1275. Consume(AS_AND);
  1276. expr:=expr + '&';
  1277. end;
  1278. AS_NOT:
  1279. Begin
  1280. Consume(AS_NOT);
  1281. expr:=expr + '~';
  1282. end;
  1283. AS_XOR:
  1284. Begin
  1285. Consume(AS_XOR);
  1286. expr:=expr + '^';
  1287. end;
  1288. AS_OR:
  1289. Begin
  1290. Consume(AS_OR);
  1291. expr:=expr + '|';
  1292. end;
  1293. AS_INTNUM:
  1294. Begin
  1295. expr:=expr + actasmpattern;
  1296. Consume(AS_INTNUM);
  1297. end;
  1298. {$ifdef i8086}
  1299. AS_SEG:
  1300. begin
  1301. include(out_flags,cseof_isseg);
  1302. Consume(actasmtoken);
  1303. if actasmtoken<>AS_ID then
  1304. Message(asmr_e_seg_without_identifier);
  1305. end;
  1306. {$endif i8086}
  1307. AS_VMTOFFSET,
  1308. AS_OFFSET:
  1309. begin
  1310. if (actasmtoken = AS_OFFSET) then
  1311. begin
  1312. include(in_flags,cseif_needofs);
  1313. include(out_flags,cseof_hasofs);
  1314. end
  1315. else
  1316. needvmtofs:=true;
  1317. Consume(actasmtoken);
  1318. if actasmtoken<>AS_ID then
  1319. Message(asmr_e_offset_without_identifier);
  1320. end;
  1321. AS_SIZEOF,
  1322. AS_TYPE:
  1323. begin
  1324. l:=0;
  1325. hasparen:=false;
  1326. Consume(actasmtoken);
  1327. if actasmtoken=AS_LPAREN then
  1328. begin
  1329. hasparen:=true;
  1330. Consume(AS_LPAREN);
  1331. end;
  1332. if actasmtoken<>AS_ID then
  1333. Message(asmr_e_type_without_identifier)
  1334. else
  1335. begin
  1336. tempstr:=actasmpattern;
  1337. Consume(AS_ID);
  1338. if actasmtoken=AS_DOT then
  1339. begin
  1340. BuildRecordOffsetSize(tempstr,k,l,mangledname,false,hastypecast);
  1341. if mangledname<>'' then
  1342. { procsym }
  1343. Message(asmr_e_wrong_sym_type);
  1344. if hastypecast then
  1345. end
  1346. else
  1347. begin
  1348. asmsearchsym(tempstr,sym,srsymtable);
  1349. if assigned(sym) then
  1350. begin
  1351. case sym.typ of
  1352. staticvarsym,
  1353. localvarsym,
  1354. paravarsym :
  1355. l:=tabstractvarsym(sym).getsize;
  1356. typesym :
  1357. l:=ttypesym(sym).typedef.size;
  1358. else
  1359. Message(asmr_e_wrong_sym_type);
  1360. end;
  1361. end
  1362. else
  1363. Message1(sym_e_unknown_id,tempstr);
  1364. end;
  1365. end;
  1366. str(l, tempstr);
  1367. expr:=expr + tempstr;
  1368. if hasparen then
  1369. Consume(AS_RPAREN);
  1370. end;
  1371. AS_PTR :
  1372. begin
  1373. { Support ugly delphi constructs like <constant> PTR [ref] }
  1374. break;
  1375. end;
  1376. AS_STRING:
  1377. begin
  1378. l:=0;
  1379. case Length(actasmpattern) of
  1380. 1 :
  1381. l:=ord(actasmpattern[1]);
  1382. 2 :
  1383. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1384. 3 :
  1385. l:=ord(actasmpattern[3]) +
  1386. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1387. 4 :
  1388. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1389. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1390. 8 :
  1391. begin
  1392. move(actasmpattern[1],l,8);
  1393. l:=SwapEndian(l);
  1394. end;
  1395. else
  1396. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1397. end;
  1398. str(l, tempstr);
  1399. expr:=expr + tempstr;
  1400. Consume(AS_STRING);
  1401. end;
  1402. AS_ID:
  1403. begin
  1404. hs:='';
  1405. hssymtyp:=AT_DATA;
  1406. def:=nil;
  1407. tempstr:=actasmpattern;
  1408. prevtok:=prevasmtoken;
  1409. { stop parsing a constant expression if we find an opcode after a
  1410. non-operator like "db $66 mov eax,ebx" }
  1411. if (prevtok in [AS_ID,AS_INTNUM,AS_RPAREN]) and
  1412. is_asmopcode(actasmpattern) then
  1413. break;
  1414. consume(AS_ID);
  1415. if (tempstr='@CODE') or (tempstr='@DATA') then
  1416. begin
  1417. if asmsym='' then
  1418. begin
  1419. asmsym:=tempstr;
  1420. asmsymtyp:=AT_SECTION;
  1421. end
  1422. else
  1423. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1424. end
  1425. else if SearchIConstant(tempstr,l) then
  1426. begin
  1427. str(l, tempstr);
  1428. expr:=expr + tempstr;
  1429. end
  1430. else
  1431. begin
  1432. if is_locallabel(tempstr) then
  1433. begin
  1434. CreateLocalLabel(tempstr,hl,false);
  1435. hs:=hl.name;
  1436. hssymtyp:=AT_FUNCTION;
  1437. end
  1438. else
  1439. if SearchLabel(tempstr,hl,false) then
  1440. begin
  1441. hs:=hl.name;
  1442. hssymtyp:=AT_FUNCTION;
  1443. end
  1444. else
  1445. begin
  1446. asmsearchsym(tempstr,sym,srsymtable);
  1447. if assigned(sym) then
  1448. begin
  1449. case sym.typ of
  1450. staticvarsym :
  1451. begin
  1452. hs:=tstaticvarsym(sym).mangledname;
  1453. def:=tstaticvarsym(sym).vardef;
  1454. end;
  1455. localvarsym,
  1456. paravarsym :
  1457. begin
  1458. Message(asmr_e_no_local_or_para_allowed);
  1459. end;
  1460. procsym :
  1461. begin
  1462. if Tprocsym(sym).ProcdefList.Count>1 then
  1463. Message(asmr_w_calling_overload_func);
  1464. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1465. {$ifdef i8086}
  1466. if is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  1467. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions) then
  1468. include(out_flags,cseof_is_farproc_entry)
  1469. else
  1470. exclude(out_flags,cseof_is_farproc_entry);
  1471. {$endif i8086}
  1472. hssymtyp:=AT_FUNCTION;
  1473. end;
  1474. typesym :
  1475. begin
  1476. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1477. Message(asmr_e_wrong_sym_type);
  1478. size:=ttypesym(sym).typedef.size;
  1479. end;
  1480. fieldvarsym :
  1481. begin
  1482. tempstr:=upper(tdef(sym.owner.defowner).GetTypeName)+'.'+tempstr;
  1483. end;
  1484. else
  1485. Message(asmr_e_wrong_sym_type);
  1486. end;
  1487. end
  1488. else
  1489. Message1(sym_e_unknown_id,tempstr);
  1490. end;
  1491. { symbol found? }
  1492. if hs<>'' then
  1493. begin
  1494. if asmsym='' then
  1495. begin
  1496. asmsym:=hs;
  1497. asmsymtyp:=hssymtyp;
  1498. end
  1499. else
  1500. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1501. if (expr='') or (expr[length(expr)]='+') then
  1502. begin
  1503. { don't remove the + if there could be a record field }
  1504. if actasmtoken<>AS_DOT then
  1505. delete(expr,length(expr),1);
  1506. end
  1507. else
  1508. if (cseif_needofs in in_flags) then
  1509. begin
  1510. if (prevtok<>AS_OFFSET) then
  1511. Message(asmr_e_need_offset);
  1512. end
  1513. else
  1514. Message(asmr_e_only_add_relocatable_symbol);
  1515. end;
  1516. if (actasmtoken=AS_DOT) or
  1517. (assigned(sym) and
  1518. is_normal_fieldvarsym(sym)) then
  1519. begin
  1520. BuildRecordOffsetSize(tempstr,l,size,hs,needvmtofs,hastypecast);
  1521. if hs <> '' then
  1522. hssymtyp:=AT_FUNCTION
  1523. else
  1524. begin
  1525. str(l, tempstr);
  1526. expr:=expr + tempstr;
  1527. end
  1528. end
  1529. else if (actasmtoken<>AS_DOT) and
  1530. assigned(sym) and
  1531. (sym.typ=typesym) and
  1532. (ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1533. begin
  1534. { just a record type (without being followed by dot)
  1535. evaluates to 0. Ugly, but TP7 compatible. }
  1536. expr:=expr+'0';
  1537. end
  1538. else
  1539. begin
  1540. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1541. delete(expr,length(expr),1);
  1542. end;
  1543. if (actasmtoken=AS_LBRACKET) and
  1544. assigned(def) and
  1545. (def.typ=arraydef) then
  1546. begin
  1547. consume(AS_LBRACKET);
  1548. l:=BuildConstExpression;
  1549. if l<tarraydef(def).lowrange then
  1550. begin
  1551. Message(asmr_e_constant_out_of_bounds);
  1552. l:=0;
  1553. end
  1554. else
  1555. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  1556. str(l, tempstr);
  1557. expr:=expr + '+' + tempstr;
  1558. consume(AS_RBRACKET);
  1559. end;
  1560. end;
  1561. { check if there are wrong operator used like / or mod etc. }
  1562. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  1563. Message(asmr_e_only_add_relocatable_symbol);
  1564. end;
  1565. AS_ALIGN,
  1566. AS_DB,
  1567. AS_DW,
  1568. AS_DD,
  1569. AS_DQ,
  1570. AS_END,
  1571. AS_RBRACKET,
  1572. AS_SEPARATOR,
  1573. AS_COMMA,
  1574. AS_COLON:
  1575. break;
  1576. else
  1577. begin
  1578. { write error only once. }
  1579. if not errorflag then
  1580. Message(asmr_e_invalid_constant_expression);
  1581. { consume tokens until we find COMMA or SEPARATOR }
  1582. Consume(actasmtoken);
  1583. errorflag:=TRUE;
  1584. end;
  1585. end;
  1586. Until false;
  1587. { calculate expression }
  1588. if not ErrorFlag then
  1589. value:=CalculateExpression(expr)
  1590. else
  1591. value:=0;
  1592. { no longer in an expression }
  1593. inexpression:=FALSE;
  1594. end;
  1595. Function tx86intreader.BuildConstExpression:aint;
  1596. var
  1597. l,size : tcgint;
  1598. hs : string;
  1599. hssymtyp : TAsmsymtype;
  1600. out_flags : tconstsymbolexpressionoutputflags;
  1601. begin
  1602. BuildConstSymbolExpression([],l,hs,hssymtyp,size,out_flags);
  1603. if hs<>'' then
  1604. Message(asmr_e_relocatable_symbol_not_allowed);
  1605. BuildConstExpression:=l;
  1606. end;
  1607. Function tx86intreader.BuildRefConstExpression(out size:tcgint;startingminus:boolean):aint;
  1608. var
  1609. l : tcgint;
  1610. hs : string;
  1611. hssymtyp : TAsmsymtype;
  1612. in_flags : tconstsymbolexpressioninputflags;
  1613. out_flags : tconstsymbolexpressionoutputflags;
  1614. begin
  1615. in_flags:=[cseif_isref];
  1616. if startingminus then
  1617. include(in_flags,cseif_startingminus);
  1618. BuildConstSymbolExpression(in_flags,l,hs,hssymtyp,size,out_flags);
  1619. if hs<>'' then
  1620. Message(asmr_e_relocatable_symbol_not_allowed);
  1621. BuildRefConstExpression:=l;
  1622. end;
  1623. procedure tx86intreader.BuildReference(oper : tx86operand);
  1624. var
  1625. scale : byte;
  1626. k,l,size : tcgint;
  1627. tempstr,hs : string;
  1628. tempsymtyp : tasmsymtype;
  1629. code : integer;
  1630. hreg : tregister;
  1631. GotStar,GotOffset,HadVar,
  1632. GotPlus,Negative,BracketlessReference : boolean;
  1633. hl : tasmlabel;
  1634. hastypecast: boolean;
  1635. tmpoper: tx86operand;
  1636. cse_in_flags: tconstsymbolexpressioninputflags;
  1637. cse_out_flags: tconstsymbolexpressionoutputflags;
  1638. Begin
  1639. if actasmtoken=AS_LBRACKET then
  1640. begin
  1641. Consume(AS_LBRACKET);
  1642. BracketlessReference:=false;
  1643. end
  1644. else
  1645. BracketlessReference:=true;
  1646. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1647. oper.InitRef;
  1648. GotStar:=false;
  1649. GotPlus:=true;
  1650. GotOffset:=false;
  1651. Negative:=false;
  1652. Scale:=0;
  1653. repeat
  1654. if GotOffset and (actasmtoken<>AS_ID) then
  1655. Message(asmr_e_invalid_reference_syntax);
  1656. Case actasmtoken of
  1657. AS_ID, { Constant reference expression OR variable reference expression }
  1658. AS_VMTOFFSET:
  1659. Begin
  1660. if not GotPlus then
  1661. Message(asmr_e_invalid_reference_syntax);
  1662. GotStar:=false;
  1663. GotPlus:=false;
  1664. if (actasmtoken = AS_VMTOFFSET) or
  1665. (SearchIConstant(actasmpattern,l) or
  1666. SearchRecordType(actasmpattern)) then
  1667. begin
  1668. l:=BuildRefConstExpression(size,negative);
  1669. if size<>0 then
  1670. oper.SetSize(size,false);
  1671. negative:=false; { "l" was negated if necessary }
  1672. GotPlus:=(prevasmtoken=AS_PLUS);
  1673. GotStar:=(prevasmtoken=AS_STAR);
  1674. case oper.opr.typ of
  1675. OPR_LOCAL :
  1676. begin
  1677. if GotStar then
  1678. Message(asmr_e_invalid_reference_syntax);
  1679. Inc(oper.opr.localsymofs,l);
  1680. end;
  1681. OPR_REFERENCE :
  1682. begin
  1683. if GotStar then
  1684. oper.opr.ref.scalefactor:=l
  1685. else
  1686. Inc(oper.opr.ref.offset,l);
  1687. end;
  1688. else
  1689. internalerror(2019050715);
  1690. end;
  1691. end
  1692. else
  1693. Begin
  1694. if negative and not oper.hasvar then
  1695. Message(asmr_e_only_add_relocatable_symbol)
  1696. else if oper.hasvar and not GotOffset and
  1697. (not negative or assigned(oper.opr.ref.relsymbol)) then
  1698. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1699. HadVar:=oper.hasvar and GotOffset;
  1700. tempstr:=actasmpattern;
  1701. Consume(AS_ID);
  1702. { typecasting? }
  1703. if (actasmtoken=AS_LPAREN) and
  1704. SearchType(tempstr,l) then
  1705. begin
  1706. oper.hastype:=true;
  1707. oper.typesize:=l;
  1708. Consume(AS_LPAREN);
  1709. BuildOperand(oper,true);
  1710. Consume(AS_RPAREN);
  1711. end
  1712. else
  1713. if is_locallabel(tempstr) then
  1714. begin
  1715. CreateLocalLabel(tempstr,hl,false);
  1716. oper.InitRef;
  1717. oper.haslabelref:=true;
  1718. if not negative then
  1719. begin
  1720. oper.opr.ref.symbol:=hl;
  1721. oper.hasvar:=true;
  1722. end
  1723. else
  1724. oper.opr.ref.relsymbol:=hl;
  1725. {$ifdef i8086}
  1726. if oper.opr.ref.segment=NR_NO then
  1727. oper.opr.ref.segment:=NR_CS;
  1728. {$endif i8086}
  1729. end
  1730. else
  1731. if oper.SetupVar(tempstr,GotOffset) then
  1732. begin
  1733. { convert OPR_LOCAL register para into a reference base }
  1734. if (oper.opr.typ=OPR_LOCAL) and
  1735. AsmRegisterPara(oper.opr.localsym) then
  1736. oper.InitRefConvertLocal
  1737. else
  1738. begin
  1739. {$ifdef x86_64}
  1740. if actasmtoken=AS_WRT then
  1741. begin
  1742. if (oper.opr.typ=OPR_REFERENCE) then
  1743. begin
  1744. Consume(AS_WRT);
  1745. Consume(AS___GOTPCREL);
  1746. if (oper.opr.ref.base<>NR_NO) or
  1747. (oper.opr.ref.index<>NR_NO) or
  1748. (oper.opr.ref.offset<>0) then
  1749. Message(asmr_e_wrong_gotpcrel_intel_syntax);
  1750. if tf_no_pic_supported in target_info.flags then
  1751. Message(asmr_e_no_gotpcrel_support);
  1752. oper.opr.ref.refaddr:=addr_pic;
  1753. oper.opr.ref.base:=NR_RIP;
  1754. end
  1755. else
  1756. message(asmr_e_invalid_reference_syntax);
  1757. end;
  1758. {$endif x86_64}
  1759. end;
  1760. end
  1761. else
  1762. Message1(sym_e_unknown_id,tempstr);
  1763. { record.field ? }
  1764. if actasmtoken=AS_DOT then
  1765. begin
  1766. BuildRecordOffsetSize(tempstr,l,k,hs,false,hastypecast);
  1767. if (hs<>'') then
  1768. Message(asmr_e_invalid_symbol_ref);
  1769. case oper.opr.typ of
  1770. OPR_LOCAL :
  1771. inc(oper.opr.localsymofs,l);
  1772. OPR_REFERENCE :
  1773. inc(oper.opr.ref.offset,l);
  1774. else
  1775. internalerror(2019050716);
  1776. end;
  1777. if hastypecast then
  1778. oper.hastype:=true;
  1779. oper.SetSize(k,false);
  1780. end;
  1781. if GotOffset then
  1782. begin
  1783. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1784. begin
  1785. if (oper.opr.typ=OPR_REFERENCE) then
  1786. oper.opr.ref.base:=NR_NO;
  1787. oper.hasvar:=hadvar;
  1788. end
  1789. else
  1790. begin
  1791. if oper.hasvar and hadvar then
  1792. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1793. { should we allow ?? }
  1794. end;
  1795. end;
  1796. end;
  1797. GotOffset:=false;
  1798. end;
  1799. AS_PLUS :
  1800. Begin
  1801. Consume(AS_PLUS);
  1802. Negative:=false;
  1803. GotPlus:=true;
  1804. GotStar:=false;
  1805. Scale:=0;
  1806. end;
  1807. AS_DOT :
  1808. Begin
  1809. { Handle like a + }
  1810. Consume(AS_DOT);
  1811. Negative:=false;
  1812. GotPlus:=true;
  1813. GotStar:=false;
  1814. Scale:=0;
  1815. end;
  1816. AS_MINUS :
  1817. begin
  1818. Consume(AS_MINUS);
  1819. Negative:=true;
  1820. GotPlus:=true;
  1821. GotStar:=false;
  1822. Scale:=0;
  1823. end;
  1824. AS_STAR : { Scaling, with eax*4 order }
  1825. begin
  1826. Consume(AS_STAR);
  1827. hs:='';
  1828. l:=0;
  1829. case actasmtoken of
  1830. AS_ID,
  1831. AS_LPAREN :
  1832. l:=BuildConstExpression;
  1833. AS_INTNUM:
  1834. Begin
  1835. hs:=actasmpattern;
  1836. Consume(AS_INTNUM);
  1837. end;
  1838. AS_REGISTER :
  1839. begin
  1840. case oper.opr.typ of
  1841. OPR_REFERENCE :
  1842. begin
  1843. if oper.opr.ref.scalefactor=0 then
  1844. begin
  1845. if scale<>0 then
  1846. begin
  1847. oper.opr.ref.scalefactor:=scale;
  1848. scale:=0;
  1849. end
  1850. else
  1851. Message(asmr_e_wrong_scale_factor);
  1852. end
  1853. else
  1854. Message(asmr_e_invalid_reference_syntax);
  1855. end;
  1856. OPR_LOCAL :
  1857. begin
  1858. if oper.opr.localscale=0 then
  1859. begin
  1860. if scale<>0 then
  1861. begin
  1862. oper.opr.localscale:=scale;
  1863. scale:=0;
  1864. end
  1865. else
  1866. Message(asmr_e_wrong_scale_factor);
  1867. end
  1868. else
  1869. Message(asmr_e_invalid_reference_syntax);
  1870. end;
  1871. else
  1872. internalerror(2019050719);
  1873. end;
  1874. end;
  1875. else
  1876. Message(asmr_e_invalid_reference_syntax);
  1877. end;
  1878. if actasmtoken<>AS_REGISTER then
  1879. begin
  1880. if hs<>'' then
  1881. val(hs,l,code);
  1882. case oper.opr.typ of
  1883. OPR_REFERENCE :
  1884. oper.opr.ref.scalefactor:=l;
  1885. OPR_LOCAL :
  1886. oper.opr.localscale:=l;
  1887. else
  1888. internalerror(2019050717);
  1889. end;
  1890. if l>9 then
  1891. Message(asmr_e_wrong_scale_factor);
  1892. end;
  1893. GotPlus:=false;
  1894. GotStar:=false;
  1895. end;
  1896. AS_REGISTER :
  1897. begin
  1898. hreg:=actasmregister;
  1899. Consume(AS_REGISTER, MightHaveExtension(actopcode));
  1900. while actasmtoken in OPEXT_STARTASMTOKEN do
  1901. begin
  1902. consume_voperand_ext(oper);
  1903. end;
  1904. if actasmtoken=AS_COLON then
  1905. begin
  1906. Consume(AS_COLON);
  1907. oper.InitRefConvertLocal;
  1908. SetSegmentOverride(oper,hreg);
  1909. end
  1910. else
  1911. begin
  1912. if not((GotPlus and (not Negative)) or
  1913. GotStar) then
  1914. Message(asmr_e_invalid_reference_syntax);
  1915. { this register will be the index:
  1916. 1. just read a *
  1917. 2. next token is a *
  1918. 3. base register is already used }
  1919. case oper.opr.typ of
  1920. OPR_LOCAL :
  1921. begin
  1922. if (oper.opr.localindexreg<>NR_NO) then
  1923. Message(asmr_e_multiple_index);
  1924. {$ifdef x86_64}
  1925. { Locals/parameters cannot be accessed RIP-relative. Need a dedicated error message here? }
  1926. if (hreg=NR_RIP) then
  1927. Message(asmr_e_no_local_or_para_allowed);
  1928. {$endif x86_64}
  1929. oper.opr.localindexreg:=hreg;
  1930. if scale<>0 then
  1931. begin
  1932. oper.opr.localscale:=scale;
  1933. scale:=0;
  1934. end;
  1935. end;
  1936. OPR_REFERENCE :
  1937. begin
  1938. if (GotStar) or
  1939. (actasmtoken=AS_STAR) or
  1940. (oper.opr.ref.base<>NR_NO) then
  1941. begin
  1942. if (oper.opr.ref.index<>NR_NO) then
  1943. Message(asmr_e_multiple_index);
  1944. oper.opr.ref.index:=hreg;
  1945. if scale<>0 then
  1946. begin
  1947. oper.opr.ref.scalefactor:=scale;
  1948. scale:=0;
  1949. end;
  1950. end
  1951. else
  1952. begin
  1953. oper.opr.ref.base:=hreg;
  1954. {$ifdef x86_64}
  1955. { non-GOT based RIP-relative accesses are also position-independent }
  1956. if (oper.opr.ref.base=NR_RIP) and
  1957. (oper.opr.ref.refaddr<>addr_pic) then
  1958. oper.opr.ref.refaddr:=addr_pic_no_got;
  1959. {$endif x86_64}
  1960. end;
  1961. end;
  1962. else
  1963. internalerror(2019050718);
  1964. end;
  1965. GotPlus:=false;
  1966. GotStar:=false;
  1967. end;
  1968. end;
  1969. AS_OFFSET :
  1970. begin
  1971. Consume(AS_OFFSET);
  1972. GotOffset:=true;
  1973. end;
  1974. AS_TYPE,
  1975. AS_NOT,
  1976. AS_STRING,
  1977. AS_INTNUM,
  1978. AS_LPAREN : { Constant reference expression }
  1979. begin
  1980. if not GotPlus and not GotStar then
  1981. Message(asmr_e_invalid_reference_syntax);
  1982. cse_in_flags:=[cseif_needofs,cseif_isref];
  1983. if GotPlus and negative then
  1984. include(cse_in_flags,cseif_startingminus);
  1985. BuildConstSymbolExpression(cse_in_flags,l,tempstr,tempsymtyp,size,cse_out_flags);
  1986. { already handled by BuildConstSymbolExpression(); must be
  1987. handled there to avoid [reg-1+1] being interpreted as
  1988. [reg-(1+1)] }
  1989. negative:=false;
  1990. if tempstr<>'' then
  1991. begin
  1992. if GotStar then
  1993. Message(asmr_e_only_add_relocatable_symbol);
  1994. if not assigned(oper.opr.ref.symbol) then
  1995. begin
  1996. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1997. {$ifdef i8086}
  1998. if cseof_isseg in cse_out_flags then
  1999. begin
  2000. if not (oper.opr.ref.refaddr in [addr_fardataseg,addr_dgroup]) then
  2001. oper.opr.ref.refaddr:=addr_seg;
  2002. end
  2003. else if (tempsymtyp=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  2004. oper.opr.ref.segment:=NR_CS;
  2005. {$endif i8086}
  2006. end
  2007. else
  2008. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  2009. end;
  2010. case oper.opr.typ of
  2011. OPR_REFERENCE :
  2012. begin
  2013. if GotStar then
  2014. oper.opr.ref.scalefactor:=l
  2015. else if (prevasmtoken = AS_STAR) then
  2016. begin
  2017. if scale<>0 then
  2018. scale:=l*scale
  2019. else
  2020. scale:=l;
  2021. end
  2022. else
  2023. begin
  2024. Inc(oper.opr.ref.offset,l);
  2025. Inc(oper.opr.constoffset,l);
  2026. end;
  2027. end;
  2028. OPR_LOCAL :
  2029. begin
  2030. if GotStar then
  2031. oper.opr.localscale:=l
  2032. else if (prevasmtoken = AS_STAR) then
  2033. begin
  2034. if scale<>0 then
  2035. scale:=l*scale
  2036. else
  2037. scale:=l;
  2038. end
  2039. else
  2040. Inc(oper.opr.localsymofs,l);
  2041. end;
  2042. else
  2043. internalerror(2019050714);
  2044. end;
  2045. GotPlus:=(prevasmtoken=AS_PLUS) or
  2046. (prevasmtoken=AS_MINUS);
  2047. if GotPlus then
  2048. negative := prevasmtoken = AS_MINUS;
  2049. GotStar:=(prevasmtoken=AS_STAR);
  2050. end;
  2051. AS_LBRACKET :
  2052. begin
  2053. if (GotPlus and Negative) or GotStar then
  2054. Message(asmr_e_invalid_reference_syntax);
  2055. tmpoper:=Tx86Operand.create;
  2056. BuildReference(tmpoper);
  2057. AddReferences(oper,tmpoper);
  2058. tmpoper.Free;
  2059. GotPlus:=false;
  2060. GotStar:=false;
  2061. end;
  2062. AS_RBRACKET :
  2063. begin
  2064. if GotPlus or GotStar or BracketlessReference then
  2065. Message(asmr_e_invalid_reference_syntax);
  2066. Consume(AS_RBRACKET, MightHaveExtension(actopcode));
  2067. while actasmtoken in OPEXT_STARTASMTOKEN do
  2068. begin
  2069. consume_voperand_ext(oper);
  2070. end;
  2071. if actasmtoken=AS_LBRACKET then
  2072. begin
  2073. tmpoper:=Tx86Operand.create;
  2074. BuildReference(tmpoper);
  2075. AddReferences(oper,tmpoper);
  2076. tmpoper.Free;
  2077. end;
  2078. break;
  2079. end;
  2080. AS_SEPARATOR,
  2081. AS_END,
  2082. AS_COMMA:
  2083. begin
  2084. if not BracketlessReference then
  2085. begin
  2086. Message(asmr_e_invalid_reference_syntax);
  2087. RecoverConsume(true);
  2088. end;
  2089. break;
  2090. end;
  2091. else
  2092. Begin
  2093. Message(asmr_e_invalid_reference_syntax);
  2094. RecoverConsume(true);
  2095. break;
  2096. end;
  2097. end;
  2098. until false;
  2099. end;
  2100. { Disable range check because opr.val must accept values from min(longint) to max(dword) for i386 }
  2101. {$R-}
  2102. Procedure tx86intreader.BuildConstantOperand(oper: tx86operand);
  2103. var
  2104. l,size : tcgint;
  2105. tempstr : string;
  2106. tempsymtyp : tasmsymtype;
  2107. cse_out_flags : tconstsymbolexpressionoutputflags;
  2108. begin
  2109. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  2110. Message(asmr_e_invalid_operand_type);
  2111. BuildConstSymbolExpression([cseif_needofs],l,tempstr,tempsymtyp,size,cse_out_flags);
  2112. {$ifdef i8086}
  2113. if tempstr='@DATA' then
  2114. begin
  2115. if not (cseof_isseg in cse_out_flags) then
  2116. Message(asmr_e_CODE_or_DATA_without_SEG);
  2117. oper.SetupData;
  2118. end
  2119. else if tempstr='@CODE' then
  2120. begin
  2121. if not (cseof_isseg in cse_out_flags) then
  2122. Message(asmr_e_CODE_or_DATA_without_SEG);
  2123. oper.SetupCode;
  2124. end
  2125. else
  2126. {$endif i8086}
  2127. if tempstr<>'' then
  2128. begin
  2129. oper.opr.typ:=OPR_SYMBOL;
  2130. oper.opr.symofs:=l;
  2131. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  2132. oper.opr.symseg:=cseof_isseg in cse_out_flags;
  2133. oper.opr.sym_farproc_entry:=cseof_is_farproc_entry in cse_out_flags;
  2134. end
  2135. else
  2136. if oper.opr.typ=OPR_NONE then
  2137. begin
  2138. oper.opr.typ:=OPR_CONSTANT;
  2139. oper.opr.val:=l;
  2140. end
  2141. else
  2142. inc(oper.opr.val,l);
  2143. end;
  2144. Procedure tx86intreader.BuildOperand(oper: tx86operand;istypecast:boolean);
  2145. procedure AddLabelOperand(hl:tasmlabel);
  2146. begin
  2147. if (oper.opr.typ=OPR_NONE) and
  2148. is_calljmp(actopcode) then
  2149. begin
  2150. oper.opr.typ:=OPR_SYMBOL;
  2151. oper.opr.symbol:=hl;
  2152. end
  2153. else
  2154. begin
  2155. oper.InitRef;
  2156. oper.opr.ref.symbol:=hl;
  2157. oper.haslabelref:=true;
  2158. {$ifdef i8086}
  2159. if oper.opr.ref.segment=NR_NO then
  2160. oper.opr.ref.segment:=NR_CS;
  2161. {$endif i8086}
  2162. end;
  2163. end;
  2164. var
  2165. expr,
  2166. hs : string;
  2167. tempreg : tregister;
  2168. l : tcgint;
  2169. hl : tasmlabel;
  2170. toffset,
  2171. tsize : tcgint;
  2172. hastypecast: boolean;
  2173. begin
  2174. oper.vopext := 0;
  2175. expr:='';
  2176. repeat
  2177. if actasmtoken=AS_DOT then
  2178. begin
  2179. if expr<>'' then
  2180. begin
  2181. BuildRecordOffsetSize(expr,toffset,tsize,hs,false,hastypecast);
  2182. if (oper.opr.typ<>OPR_NONE) and
  2183. (hs<>'') then
  2184. Message(asmr_e_wrong_sym_type);
  2185. oper.SetSize(tsize,true);
  2186. if hastypecast then
  2187. oper.hastype:=true;
  2188. { we have used the size of a field. Reset the typesize of the record }
  2189. oper.typesize:=0;
  2190. case oper.opr.typ of
  2191. OPR_LOCAL :
  2192. begin
  2193. { don't allow direct access to fields of parameters, because that
  2194. will generate buggy code. Allow it only for explicit typecasting
  2195. and when the parameter is in a register (delphi compatible) }
  2196. if (not oper.hastype) then
  2197. checklocalsubscript(oper.opr.localsym);
  2198. oper.opr.localforceref:=true;
  2199. inc(oper.opr.localsymofs,toffset);
  2200. oper.opr.localvarsize := tsize;
  2201. end;
  2202. OPR_CONSTANT :
  2203. inc(oper.opr.val,toffset);
  2204. OPR_REFERENCE :
  2205. begin
  2206. inc(oper.opr.ref.offset,toffset);
  2207. oper.opr.varsize := tsize;
  2208. end;
  2209. OPR_NONE :
  2210. begin
  2211. if (hs <> '') then
  2212. begin
  2213. oper.opr.typ:=OPR_SYMBOL;
  2214. oper.opr.symbol:=current_asmdata.RefAsmSymbol(hs,AT_FUNCTION);
  2215. end
  2216. else
  2217. begin
  2218. oper.opr.typ:=OPR_CONSTANT;
  2219. oper.opr.val:=toffset;
  2220. end;
  2221. end;
  2222. OPR_REGISTER :
  2223. Message(asmr_e_invalid_reference_syntax);
  2224. OPR_SYMBOL:
  2225. Message(asmr_e_invalid_symbol_ref);
  2226. else
  2227. internalerror(200309222);
  2228. end;
  2229. expr:='';
  2230. end
  2231. else
  2232. begin
  2233. { See it as a separator }
  2234. Consume(AS_DOT);
  2235. end;
  2236. end;
  2237. case actasmtoken of
  2238. {$ifndef i8086}
  2239. AS_SEG :
  2240. Begin
  2241. Message(asmr_e_seg_not_supported);
  2242. Consume(actasmtoken);
  2243. end;
  2244. {$else not i8086}
  2245. AS_SEG,
  2246. {$endif not i8086}
  2247. AS_OFFSET,
  2248. AS_SIZEOF,
  2249. AS_VMTOFFSET,
  2250. AS_TYPE,
  2251. AS_NOT,
  2252. AS_STRING,
  2253. AS_PLUS,
  2254. AS_MINUS,
  2255. AS_LPAREN,
  2256. AS_INTNUM :
  2257. begin
  2258. case oper.opr.typ of
  2259. OPR_REFERENCE :
  2260. {$ifndef x86_64}
  2261. { this is for the i386 scenario where you have
  2262. <load got into ebx>
  2263. mov eax, [ebx].offset globalvar
  2264. x86-64 uses RIP-based addresses (both for GOT and non-GOT
  2265. relative accesses)
  2266. }
  2267. if (actasmtoken=AS_OFFSET) and
  2268. (cs_create_pic in current_settings.moduleswitches) then
  2269. begin
  2270. Consume(AS_OFFSET);
  2271. oper.opr.ref.refaddr:=addr_pic;
  2272. BuildOperand(oper,false);
  2273. end
  2274. else
  2275. {$endif x86_64}
  2276. begin
  2277. l := BuildRefConstExpression(tsize);
  2278. if tsize<>0 then
  2279. oper.SetSize(tsize,false);
  2280. inc(oper.opr.ref.offset,l);
  2281. inc(oper.opr.constoffset,l);
  2282. end;
  2283. OPR_LOCAL :
  2284. begin
  2285. l := BuildConstExpression;
  2286. inc(oper.opr.localsymofs,l);
  2287. inc(oper.opr.localconstoffset,l);
  2288. end;
  2289. OPR_NONE,
  2290. OPR_CONSTANT :
  2291. BuildConstantOperand(oper);
  2292. else
  2293. Message(asmr_e_invalid_operand_type);
  2294. end;
  2295. end;
  2296. AS_PTR :
  2297. begin
  2298. if not oper.hastype then
  2299. begin
  2300. if (oper.opr.typ=OPR_CONSTANT) then
  2301. begin
  2302. oper.typesize:=oper.opr.val;
  2303. { reset constant value of operand }
  2304. oper.opr.typ:=OPR_NONE;
  2305. oper.opr.val:=0;
  2306. end
  2307. else
  2308. Message(asmr_e_syn_operand);
  2309. end;
  2310. Consume(AS_PTR);
  2311. { in delphi mode, allow e.g. call dword ptr eax,
  2312. see also webtbs/tw18225.pp }
  2313. if not(m_delphi in current_settings.modeswitches) then
  2314. oper.InitRef;
  2315. { if the operand subscripts a record, the typesize will be
  2316. rest -> save it here and restore it afterwards }
  2317. l:=oper.typesize;
  2318. BuildOperand(oper,false);
  2319. oper.setsize(l,true);
  2320. end;
  2321. AS_ID : { A constant expression, or a Variable ref. }
  2322. Begin
  2323. { Label or Special symbol reference? }
  2324. if actasmpattern[1] = '@' then
  2325. Begin
  2326. if actasmpattern = '@RESULT' then
  2327. Begin
  2328. oper.SetupResult;
  2329. Consume(AS_ID);
  2330. expr:='result';
  2331. end
  2332. else
  2333. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  2334. begin
  2335. {$ifdef i8086}
  2336. Message(asmr_e_CODE_or_DATA_without_SEG);
  2337. {$else i8086}
  2338. Message(asmr_w_CODE_and_DATA_not_supported);
  2339. {$endif i8086}
  2340. Consume(AS_ID);
  2341. end
  2342. else
  2343. { Local Label }
  2344. begin
  2345. CreateLocalLabel(actasmpattern,hl,false);
  2346. Consume(AS_ID);
  2347. AddLabelOperand(hl);
  2348. end;
  2349. end
  2350. else
  2351. { support result for delphi modes }
  2352. if (m_objpas in current_settings.modeswitches) and (actasmpattern='RESULT') then
  2353. begin
  2354. oper.SetUpResult;
  2355. Consume(AS_ID);
  2356. expr:='result';
  2357. end
  2358. { probably a variable or normal expression }
  2359. { or a procedure (such as in CALL ID) }
  2360. else
  2361. Begin
  2362. { is it a constant ? }
  2363. if SearchIConstant(actasmpattern,l) then
  2364. Begin
  2365. case oper.opr.typ of
  2366. OPR_REFERENCE :
  2367. begin
  2368. l := BuildRefConstExpression(tsize);
  2369. if tsize<>0 then
  2370. oper.SetSize(tsize,false);
  2371. inc(oper.opr.ref.offset,l);
  2372. inc(oper.opr.constoffset,l);
  2373. end;
  2374. OPR_LOCAL :
  2375. begin
  2376. l := BuildRefConstExpression(tsize);
  2377. if tsize<>0 then
  2378. oper.SetSize(tsize,false);
  2379. inc(oper.opr.localsymofs,l);
  2380. inc(oper.opr.localconstoffset,l);
  2381. end;
  2382. OPR_NONE,
  2383. OPR_CONSTANT :
  2384. BuildConstantOperand(oper);
  2385. else
  2386. Message(asmr_e_invalid_operand_type);
  2387. end;
  2388. end
  2389. else
  2390. { Check for pascal label }
  2391. if SearchLabel(actasmpattern,hl,false) then
  2392. begin
  2393. Consume(AS_ID);
  2394. AddLabelOperand(hl);
  2395. end
  2396. else
  2397. { is it a normal variable ? }
  2398. Begin
  2399. expr:=actasmpattern;
  2400. Consume(AS_ID, MightHaveExtension(actopcode));
  2401. while actasmtoken in OPEXT_STARTASMTOKEN do
  2402. begin
  2403. consume_voperand_ext(oper);
  2404. end;
  2405. { typecasting? }
  2406. if SearchType(expr,l) then
  2407. begin
  2408. oper.hastype:=true;
  2409. oper.typesize:=l;
  2410. case actasmtoken of
  2411. AS_LPAREN :
  2412. begin
  2413. { Support Type([Reference]) }
  2414. Consume(AS_LPAREN);
  2415. BuildOperand(oper,true);
  2416. { Delphi also supports Type(Register) and
  2417. interprets it the same as Type([Register]). }
  2418. if (oper.opr.typ = OPR_REGISTER) then
  2419. { This also sets base to the register. }
  2420. oper.InitRef;
  2421. Consume(AS_RPAREN);
  2422. end;
  2423. AS_LBRACKET :
  2424. begin
  2425. { Support Var.Type[Index] }
  2426. { Convert @label.Byte[1] to reference }
  2427. if oper.opr.typ=OPR_SYMBOL then
  2428. oper.initref;
  2429. end;
  2430. else
  2431. ;
  2432. end;
  2433. end
  2434. else
  2435. begin
  2436. if not oper.SetupVar(expr,false) then
  2437. Begin
  2438. { not a variable, check special variables.. }
  2439. if expr = 'SELF' then
  2440. begin
  2441. oper.SetupSelf;
  2442. expr:='self';
  2443. end
  2444. else
  2445. begin
  2446. Message1(sym_e_unknown_id,expr);
  2447. expr:='';
  2448. end;
  2449. end;
  2450. { indexed access to variable? }
  2451. if actasmtoken=AS_LBRACKET then
  2452. begin
  2453. { ... then the operand size is not known anymore }
  2454. oper.size:=OS_NO;
  2455. BuildReference(oper);
  2456. end;
  2457. end;
  2458. end;
  2459. end;
  2460. end;
  2461. AS_REGISTER : { Register, a variable reference or a constant reference }
  2462. begin
  2463. { save the type of register used. }
  2464. tempreg:=actasmregister;
  2465. Consume(AS_REGISTER, MightHaveExtension(actopcode));
  2466. if (getregtype(tempreg) in [R_MMREGISTER, R_ADDRESSREGISTER]) then
  2467. begin
  2468. while actasmtoken in OPEXT_STARTASMTOKEN do
  2469. begin
  2470. consume_voperand_ext(oper);
  2471. end;
  2472. end;
  2473. if actasmtoken = AS_COLON then
  2474. Begin
  2475. Consume(AS_COLON);
  2476. oper.InitRef;
  2477. SetSegmentOverride(oper,tempreg);
  2478. BuildReference(oper);
  2479. end
  2480. else
  2481. { Simple register }
  2482. begin
  2483. if (oper.opr.typ <> OPR_NONE) then
  2484. Message(asmr_e_syn_operand);
  2485. oper.opr.typ:=OPR_REGISTER;
  2486. oper.opr.reg:=tempreg;
  2487. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  2488. end;
  2489. end;
  2490. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  2491. Begin
  2492. BuildReference(oper);
  2493. end;
  2494. AS_DWORD,
  2495. AS_BYTE,
  2496. AS_WORD,
  2497. AS_TBYTE,
  2498. AS_DQWORD,
  2499. AS_QWORD,
  2500. AS_OWORD,
  2501. AS_XMMWORD,
  2502. AS_YWORD,
  2503. AS_YMMWORD,
  2504. AS_ZWORD,
  2505. AS_ZMMWORD
  2506. :
  2507. begin
  2508. { Type specifier }
  2509. oper.hastype:=true;
  2510. oper.typesize:=0;
  2511. case actasmtoken of
  2512. AS_DWORD : oper.typesize:=4;
  2513. AS_WORD : oper.typesize:=2;
  2514. AS_BYTE : oper.typesize:=1;
  2515. AS_QWORD : oper.typesize:=8;
  2516. AS_DQWORD : oper.typesize:=16;
  2517. AS_TBYTE : oper.typesize:=10;
  2518. AS_OWORD,
  2519. AS_XMMWORD: oper.typesize:=16;
  2520. AS_YWORD,
  2521. AS_YMMWORD: oper.typesize:=32;
  2522. AS_ZWORD,
  2523. AS_ZMMWORD: oper.typesize:=64;
  2524. else
  2525. internalerror(2010061101);
  2526. end;
  2527. Consume(actasmtoken);
  2528. if (actasmtoken=AS_LPAREN) then
  2529. begin
  2530. { Support "xxx ptr [Reference]" }
  2531. { in case the expression subscripts a record, the typesize
  2532. is reset, so save the explicit size we set above }
  2533. l:=oper.typesize;
  2534. Consume(AS_LPAREN);
  2535. BuildOperand(oper,true);
  2536. Consume(AS_RPAREN);
  2537. oper.setsize(l,true);
  2538. end;
  2539. end;
  2540. AS_SEPARATOR,
  2541. AS_END,
  2542. AS_COMMA,
  2543. AS_COLON:
  2544. begin
  2545. break;
  2546. end;
  2547. AS_RPAREN:
  2548. begin
  2549. if not istypecast then
  2550. begin
  2551. Message(asmr_e_syn_operand);
  2552. Consume(AS_RPAREN);
  2553. end
  2554. else
  2555. break;
  2556. end;
  2557. else
  2558. begin
  2559. Message(asmr_e_syn_operand);
  2560. RecoverConsume(true);
  2561. break;
  2562. end;
  2563. end;
  2564. until false;
  2565. { End of operand, update size if a typecast is forced }
  2566. if (oper.typesize<>0) and
  2567. (oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL]) then
  2568. oper.SetSize(oper.typesize,true);
  2569. {$ifdef i8086}
  2570. { references to a procedure/function entry, without an explicit segment
  2571. override, are added an CS: override by default (this is Turbo Pascal 7
  2572. compatible) }
  2573. if (oper.opr.typ=OPR_REFERENCE) and assigned(oper.opr.ref.symbol) and
  2574. (oper.opr.ref.symbol.typ=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  2575. oper.opr.ref.segment:=NR_CS;
  2576. {$endif i8086}
  2577. end;
  2578. Procedure tx86intreader.BuildOpCode(instr : tx86instruction);
  2579. var
  2580. PrefixOp,OverrideOp: tasmop;
  2581. operandnum : longint;
  2582. t: TRegister;
  2583. is_far_const:boolean;
  2584. i:byte;
  2585. tmp: toperand;
  2586. di_param, si_param: ShortInt;
  2587. prefix_or_override_pending_concat: boolean = false;
  2588. {$ifdef i8086}
  2589. hsymbol: TAsmSymbol;
  2590. hoffset: ASizeInt;
  2591. href_farproc_entry: Boolean;
  2592. {$endif i8086}
  2593. begin
  2594. PrefixOp:=A_None;
  2595. OverrideOp:=A_None;
  2596. is_far_const:=false;
  2597. { prefix seg opcode / prefix opcode }
  2598. repeat
  2599. if is_prefix(actopcode) then
  2600. with instr do
  2601. begin
  2602. if prefix_or_override_pending_concat then
  2603. ConcatInstruction(curlist);
  2604. PrefixOp:=ActOpcode;
  2605. opcode:=ActOpcode;
  2606. condition:=ActCondition;
  2607. opsize:=ActOpsize;
  2608. prefix_or_override_pending_concat:=true;
  2609. consume(AS_OPCODE);
  2610. end
  2611. else
  2612. if is_override(actopcode) then
  2613. with instr do
  2614. begin
  2615. if prefix_or_override_pending_concat then
  2616. ConcatInstruction(curlist);
  2617. OverrideOp:=ActOpcode;
  2618. opcode:=ActOpcode;
  2619. condition:=ActCondition;
  2620. opsize:=ActOpsize;
  2621. prefix_or_override_pending_concat:=true;
  2622. consume(AS_OPCODE);
  2623. end
  2624. else
  2625. break;
  2626. { allow for newline after prefix or override }
  2627. while actasmtoken=AS_SEPARATOR do
  2628. consume(AS_SEPARATOR);
  2629. until (actasmtoken<>AS_OPCODE);
  2630. { opcode }
  2631. if (actasmtoken <> AS_OPCODE) then
  2632. begin
  2633. { allow a prefix or override to be used standalone, like an opcode
  2634. with zero operands; this is TP7 compatible and allows compiling
  2635. ugly code like 'seges; db $67,$66; lodsw' }
  2636. if prefix_or_override_pending_concat then
  2637. exit
  2638. else
  2639. begin
  2640. Message(asmr_e_invalid_or_missing_opcode);
  2641. RecoverConsume(false);
  2642. exit;
  2643. end;
  2644. end;
  2645. if prefix_or_override_pending_concat then
  2646. begin
  2647. instr.ConcatInstruction(curlist);
  2648. prefix_or_override_pending_concat:=false;
  2649. end;
  2650. { Fill the instr object with the current state }
  2651. with instr do
  2652. begin
  2653. Opcode:=ActOpcode;
  2654. condition:=ActCondition;
  2655. opsize:=ActOpsize;
  2656. { Valid combination of prefix/override and instruction ? }
  2657. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  2658. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  2659. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  2660. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  2661. end;
  2662. { pushf/popf/pusha/popa have to default to 16 bit in Intel mode
  2663. (Intel manual and Delphi-compatbile) -- setting the opsize for
  2664. these instructions doesn't change anything in the internal assember,
  2665. so change the opcode }
  2666. if (instr.opcode=A_POPF) then
  2667. instr.opcode:=A_POPFW
  2668. else if (instr.opcode=A_PUSHF) then
  2669. instr.opcode:=A_PUSHFW
  2670. {$ifndef x86_64}
  2671. else if (instr.opcode=A_PUSHA) then
  2672. instr.opcode:=A_PUSHAW
  2673. else if (instr.opcode=A_POPA) then
  2674. instr.opcode:=A_POPAW
  2675. {$endif x86_64}
  2676. {$ifdef i8086}
  2677. { ret is converted to retn or retf, depending on the call model of the
  2678. current procedure (BP7 compatible) }
  2679. else if (instr.opcode=A_RET) then
  2680. begin
  2681. if is_proc_far(current_procinfo.procdef) and
  2682. not (po_interrupt in current_procinfo.procdef.procoptions) then
  2683. instr.opcode:=A_RETF
  2684. else
  2685. instr.opcode:=A_RETN;
  2686. end
  2687. {$endif i8086}
  2688. ;
  2689. { We are reading operands, so opcode will be an AS_ID }
  2690. { process operands backwards to get them in AT&T order }
  2691. operandnum:=max_operands;
  2692. is_far_const:=false;
  2693. Consume(AS_OPCODE);
  2694. { Zero operand opcode ? }
  2695. if actasmtoken in [AS_SEPARATOR,AS_END] then
  2696. exit;
  2697. { Read Operands }
  2698. repeat
  2699. case actasmtoken of
  2700. { End of asm operands for this opcode }
  2701. AS_END,
  2702. AS_SEPARATOR :
  2703. break;
  2704. { Operand delimiter }
  2705. AS_COMMA :
  2706. begin
  2707. { should have something before the comma }
  2708. if instr.operands[operandnum].opr.typ=OPR_NONE then
  2709. Message(asmr_e_syntax_error);
  2710. if operandnum <= 1 then
  2711. Message(asmr_e_too_many_operands)
  2712. else
  2713. Dec(operandnum);
  2714. Consume(AS_COMMA,instr.MightHaveExtension);
  2715. end;
  2716. {Far constant, i.e. jmp $0000:$11111111.}
  2717. AS_COLON:
  2718. begin
  2719. is_far_const:=true;
  2720. if operandnum<max_operands then
  2721. message(asmr_e_too_many_operands)
  2722. else
  2723. dec(operandnum);
  2724. consume(AS_COLON);
  2725. end;
  2726. { Type specifier }
  2727. AS_NEAR,
  2728. AS_FAR :
  2729. begin
  2730. if actasmtoken = AS_NEAR then
  2731. begin
  2732. {$ifndef i8086}
  2733. Message(asmr_w_near_ignored);
  2734. {$endif not i8086}
  2735. instr.opsize:=S_NEAR;
  2736. end
  2737. else
  2738. begin
  2739. {$ifndef i8086}
  2740. Message(asmr_w_far_ignored);
  2741. {$endif not i8086}
  2742. instr.opsize:=S_FAR;
  2743. end;
  2744. Consume(actasmtoken);
  2745. if actasmtoken=AS_PTR then
  2746. begin
  2747. Consume(AS_PTR);
  2748. instr.Operands[operandnum].InitRef;
  2749. end;
  2750. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2751. end;
  2752. AS_LOPSAE,
  2753. AS_LOPER:
  2754. if operandnum < max_operands then
  2755. begin
  2756. consume_voperand_ext(instr.Operands[operandnum + 1] as tx86operand, false);
  2757. if actasmtoken in [AS_OPSAE,AS_OPRNSAE,AS_OPRDSAE,AS_OPRUSAE,AS_OPRZSAE] then
  2758. begin
  2759. consume(actasmtoken);
  2760. // ignore operand
  2761. if actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA] then inc(operandnum)
  2762. else Message(asmr_e_syntax_error);
  2763. end
  2764. else Message(asmr_e_syntax_error);
  2765. end
  2766. else Message(asmr_e_syntax_error);
  2767. else
  2768. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2769. end; { end case }
  2770. until false;
  2771. { shift operands to start from 1, exchange to make sure they are destroyed correctly }
  2772. for i:=operandnum to max_operands do
  2773. begin
  2774. tmp:=instr.operands[i+1-operandnum];
  2775. instr.operands[i+1-operandnum]:=instr.operands[i];
  2776. instr.operands[i]:=tmp;
  2777. end;
  2778. operandnum:=(max_operands+1)-operandnum;
  2779. instr.ops:=operandnum;
  2780. { Check operands }
  2781. for i:=1 to operandnum do
  2782. begin
  2783. if is_far_const and
  2784. (instr.operands[i].opr.typ<>OPR_CONSTANT) then
  2785. message(asmr_e_expr_illegal)
  2786. else
  2787. if instr.operands[i].opr.typ=OPR_NONE then
  2788. Message(asmr_e_syntax_error);
  2789. end;
  2790. { Check for invalid ES: overrides }
  2791. if is_x86_parameterized_string_op(instr.opcode) then
  2792. begin
  2793. si_param:=get_x86_string_op_si_param(instr.opcode);
  2794. if si_param<>-1 then
  2795. si_param:=x86_parameterized_string_op_param_count(instr.opcode)-si_param;
  2796. di_param:=get_x86_string_op_di_param(instr.opcode);
  2797. if di_param<>-1 then
  2798. begin
  2799. di_param:=x86_parameterized_string_op_param_count(instr.opcode)-di_param;
  2800. if di_param<=operandnum then
  2801. with instr.operands[di_param] do
  2802. if (opr.typ=OPR_REFERENCE) and
  2803. (opr.ref.segment<>NR_NO) and
  2804. (opr.ref.segment<>NR_ES) then
  2805. Message(asmr_e_cannot_override_es_segment);
  2806. end;
  2807. { if two memory parameters, check whether their address sizes are equal }
  2808. if (si_param<>-1) and (di_param<>-1) and
  2809. (si_param<=operandnum) and (di_param<=operandnum) and
  2810. (instr.operands[si_param].opr.typ=OPR_REFERENCE) and
  2811. (instr.operands[di_param].opr.typ=OPR_REFERENCE) then
  2812. begin
  2813. if get_ref_address_size(instr.operands[si_param].opr.ref)<>
  2814. get_ref_address_size(instr.operands[di_param].opr.ref) then
  2815. Message(asmr_e_address_sizes_do_not_match);
  2816. end;
  2817. end;
  2818. { e.g. for "push dword 1", "push word 6" }
  2819. if (instr.ops=1) and
  2820. (instr.operands[1].typesize<>0) then
  2821. instr.operands[1].setsize(instr.operands[1].typesize,false);
  2822. {$ifdef i8086}
  2823. for i:=1 to operandnum do
  2824. with instr.operands[i].opr do
  2825. begin
  2826. { convert 'call/jmp [proc/label]' to 'call/jmp proc/label'. Ugly,
  2827. but Turbo Pascal 7 compatible. }
  2828. if (instr.opcode in [A_CALL,A_JMP]) and
  2829. (instr.operands[i].haslabelref or instr.operands[i].hasproc) and
  2830. (not instr.operands[i].hastype)
  2831. and (typ=OPR_REFERENCE) and
  2832. assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL,AT_ADDR]) and
  2833. (ref.base=NR_NO) and (ref.index=NR_NO) then
  2834. begin
  2835. hsymbol:=ref.symbol;
  2836. hoffset:=ref.offset;
  2837. href_farproc_entry:=ref_farproc_entry;
  2838. typ:=OPR_SYMBOL;
  2839. symbol:=hsymbol;
  2840. symofs:=hoffset;
  2841. symseg:=False;
  2842. sym_farproc_entry:=href_farproc_entry;
  2843. end;
  2844. { convert 'call/jmp symbol' to 'call/jmp far symbol' for symbols that are an entry point of a far procedure }
  2845. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2846. (typ=OPR_SYMBOL) and sym_farproc_entry then
  2847. instr.opsize:=S_FAR;
  2848. { convert 'call/jmp dword [something]' to 'call/jmp far [something]' (BP7 compatibility) }
  2849. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2850. (typ in [OPR_LOCAL,OPR_REFERENCE]) and (instr.operands[i].size=OS_32) then
  2851. instr.opsize:=S_FAR;
  2852. end;
  2853. {$endif i8086}
  2854. if (MemRefInfo(instr.opcode).ExistsSSEAVX) and
  2855. (MemRefInfo(instr.opcode).MemRefSize in MemRefSizeInfoVMems) then
  2856. begin
  2857. for i:=1 to operandnum do
  2858. begin
  2859. if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2860. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2861. (instr.operands[i].opr.ref.index = NR_NO) then
  2862. begin
  2863. instr.operands[i].opr.ref.index := instr.operands[i].opr.ref.base;
  2864. instr.operands[i].opr.ref.base := NR_NO;
  2865. end
  2866. else if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2867. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2868. (getregtype(instr.operands[i].opr.ref.index) = R_INTREGISTER) and
  2869. (getsubreg(instr.operands[i].opr.ref.index) = R_SUBADDR) then
  2870. begin
  2871. // exchange base- and index-register
  2872. // e.g. VGATHERDPD XMM0, [XMM1 + RAX], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1], XMM2
  2873. // e.g. VGATHERDPD XMM0, [XMM1 + RAX * 2], XMM2 =>> not supported
  2874. // e.g. VGATHERDPD XMM0, [XMM1 + RAX + 16], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1 + 16]
  2875. if instr.operands[i].opr.ref.scalefactor > 1 then Message(asmr_e_invalid_reference_syntax)
  2876. else
  2877. begin
  2878. t := instr.operands[i].opr.ref.base;
  2879. instr.operands[i].opr.ref.base := instr.operands[i].opr.ref.index;
  2880. instr.operands[i].opr.ref.index := t;
  2881. end;
  2882. end;
  2883. end;
  2884. end;
  2885. end;
  2886. Procedure tx86intreader.BuildConstant(constsize: byte);
  2887. var
  2888. asmsymtyp : tasmsymtype;
  2889. asmsym,
  2890. expr: string;
  2891. value,size : tcgint;
  2892. cse_out_flags : tconstsymbolexpressionoutputflags;
  2893. Begin
  2894. Repeat
  2895. Case actasmtoken of
  2896. AS_STRING:
  2897. Begin
  2898. { DD and DW cases }
  2899. if constsize <> 1 then
  2900. Begin
  2901. if Not PadZero(actasmpattern,constsize) then
  2902. Message(scan_f_string_exceeds_line);
  2903. end;
  2904. expr:=actasmpattern;
  2905. Consume(AS_STRING);
  2906. Case actasmtoken of
  2907. AS_COMMA:
  2908. Consume(AS_COMMA);
  2909. AS_END,
  2910. AS_SEPARATOR: ;
  2911. else
  2912. Message(asmr_e_invalid_string_expression);
  2913. end;
  2914. ConcatString(curlist,expr);
  2915. end;
  2916. AS_BYTE,
  2917. AS_WORD,
  2918. AS_DWORD,
  2919. AS_TBYTE,
  2920. AS_DQWORD,
  2921. AS_QWORD,
  2922. AS_OWORD,
  2923. AS_XMMWORD,
  2924. AS_YWORD,
  2925. AS_YMMWORD,
  2926. AS_PLUS,
  2927. AS_MINUS,
  2928. AS_LPAREN,
  2929. AS_NOT,
  2930. AS_INTNUM,
  2931. AS_OFFSET,
  2932. AS_LBRACKET,
  2933. {$ifdef i8086}
  2934. AS_SEG,
  2935. {$endif i8086}
  2936. AS_ID :
  2937. Begin
  2938. BuildConstSymbolExpression([cseif_referencelike],value,asmsym,asmsymtyp,size,cse_out_flags);
  2939. if asmsym<>'' then
  2940. begin
  2941. if not (cseof_isseg in cse_out_flags) and
  2942. {$ifdef i8086}
  2943. ((constsize<>2) and (constsize<>4))
  2944. {$else i8086}
  2945. (constsize<>sizeof(pint))
  2946. {$endif i8086}
  2947. then
  2948. begin
  2949. Message1(asmr_w_const32bit_for_address,asmsym);
  2950. constsize:=sizeof(pint);
  2951. end;
  2952. {$ifdef i8086}
  2953. if asmsym='@DATA' then
  2954. begin
  2955. if not (cseof_isseg in cse_out_flags) then
  2956. Message(asmr_e_CODE_or_DATA_without_SEG);
  2957. if constsize<2 then
  2958. Message1(asmr_e_const16bit_for_segment,asmsym);
  2959. if current_settings.x86memorymodel=mm_huge then
  2960. curlist.concat(Tai_const.Create_fardataseg)
  2961. else
  2962. curlist.concat(Tai_const.Create_dgroup);
  2963. if constsize>2 then
  2964. ConcatConstant(curlist,0,constsize-2);
  2965. end
  2966. else if asmsym='@CODE' then
  2967. begin
  2968. if not (cseof_isseg in cse_out_flags) then
  2969. Message(asmr_e_CODE_or_DATA_without_SEG);
  2970. if constsize<2 then
  2971. Message1(asmr_e_const16bit_for_segment,asmsym);
  2972. curlist.concat(Tai_const.Create_seg_name(current_procinfo.procdef.mangledname));
  2973. if constsize>2 then
  2974. ConcatConstant(curlist,0,constsize-2);
  2975. end
  2976. else if cseof_isseg in cse_out_flags then
  2977. begin
  2978. if constsize<2 then
  2979. Message1(asmr_e_const16bit_for_segment,asmsym);
  2980. curlist.concat(Tai_const.Create_seg_name(asmsym));
  2981. if constsize>2 then
  2982. ConcatConstant(curlist,0,constsize-2);
  2983. end
  2984. else
  2985. {$endif i8086}
  2986. ConcatConstSymbol(curlist,asmsym,'',asmsymtyp,value,constsize,cseof_hasofs in cse_out_flags);
  2987. end
  2988. else
  2989. ConcatConstant(curlist,value,constsize);
  2990. end;
  2991. AS_COMMA:
  2992. begin
  2993. Consume(AS_COMMA);
  2994. end;
  2995. AS_ALIGN,
  2996. AS_DB,
  2997. AS_DW,
  2998. AS_DD,
  2999. AS_DQ,
  3000. AS_OPCODE,
  3001. AS_END,
  3002. AS_SEPARATOR:
  3003. break;
  3004. else
  3005. begin
  3006. Message(asmr_e_syn_constant);
  3007. RecoverConsume(false);
  3008. end
  3009. end;
  3010. Until false;
  3011. end;
  3012. function tx86intreader.Assemble: tlinkedlist;
  3013. Var
  3014. hl : tasmlabel;
  3015. instr : Tx86Instruction;
  3016. tmpsym: tsym;
  3017. tmpsrsymtable: TSymtable;
  3018. Begin
  3019. Message1(asmr_d_start_reading,'intel');
  3020. inexpression:=FALSE;
  3021. firsttoken:=TRUE;
  3022. { sets up all opcode and register tables in uppercase
  3023. done in the construtor now
  3024. if not _asmsorted then
  3025. Begin
  3026. SetupTables;
  3027. _asmsorted:=TRUE;
  3028. end;
  3029. }
  3030. curlist:=TAsmList.Create;
  3031. { we might need to know which parameters are passed in registers }
  3032. if not parse_generic then
  3033. current_procinfo.generate_parameter_info;
  3034. { start tokenizer }
  3035. gettoken;
  3036. { main loop }
  3037. repeat
  3038. case actasmtoken of
  3039. AS_LLABEL:
  3040. Begin
  3041. if CreateLocalLabel(actasmpattern,hl,true) then
  3042. ConcatLabel(curlist,hl);
  3043. Consume(AS_LLABEL);
  3044. end;
  3045. AS_LABEL:
  3046. Begin
  3047. if SearchLabel(upper(actasmpattern),hl,true) then
  3048. begin
  3049. if hl.is_public then
  3050. ConcatPublic(curlist,actasmpattern_origcase);
  3051. ConcatLabel(curlist,hl);
  3052. end
  3053. else
  3054. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  3055. Consume(AS_LABEL);
  3056. end;
  3057. AS_DW :
  3058. Begin
  3059. inexpression:=true;
  3060. Consume(AS_DW);
  3061. BuildConstant(2);
  3062. inexpression:=false;
  3063. end;
  3064. AS_DB :
  3065. Begin
  3066. inexpression:=true;
  3067. Consume(AS_DB);
  3068. BuildConstant(1);
  3069. inexpression:=false;
  3070. end;
  3071. AS_DD :
  3072. Begin
  3073. inexpression:=true;
  3074. Consume(AS_DD);
  3075. BuildConstant(4);
  3076. inexpression:=false;
  3077. end;
  3078. {$ifdef cpu64bitaddr}
  3079. AS_DQ:
  3080. Begin
  3081. inexpression:=true;
  3082. Consume(AS_DQ);
  3083. BuildConstant(8);
  3084. inexpression:=false;
  3085. end;
  3086. {$endif cpu64bitaddr}
  3087. AS_PUBLIC:
  3088. Begin
  3089. Consume(AS_PUBLIC);
  3090. repeat
  3091. if actasmtoken=AS_ID then
  3092. begin
  3093. if (actasmpattern<>'') and (actasmpattern[1]='@') then
  3094. Message1(asmr_e_local_label_cannot_be_declared_public,actasmpattern)
  3095. else if SearchLabel(upper(actasmpattern),hl,false) then
  3096. begin
  3097. if not hl.is_public then
  3098. begin
  3099. hl.is_public:=true;
  3100. asmsearchsym(upper(actasmpattern),tmpsym,tmpsrsymtable);
  3101. if tlabelsym(tmpsym).defined then
  3102. Message1(asmr_e_public_must_be_used_before_label_definition,actasmpattern);
  3103. end;
  3104. end
  3105. else
  3106. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  3107. end;
  3108. Consume(AS_ID);
  3109. if actasmtoken=AS_COMMA then
  3110. begin
  3111. Consume(AS_COMMA);
  3112. if actasmtoken<>AS_ID then
  3113. Consume(AS_ID);
  3114. end;
  3115. until actasmtoken=AS_SEPARATOR;
  3116. end;
  3117. AS_ALIGN:
  3118. Begin
  3119. Consume(AS_ALIGN);
  3120. ConcatAlign(curlist,BuildConstExpression);
  3121. if actasmtoken<>AS_SEPARATOR then
  3122. Consume(AS_SEPARATOR);
  3123. end;
  3124. AS_OPCODE :
  3125. Begin
  3126. instr:=Tx86Instruction.Create(Tx86Operand);
  3127. BuildOpcode(instr);
  3128. with instr do
  3129. begin
  3130. CheckNonCommutativeOpcodes;
  3131. AddReferenceSizes;
  3132. SetInstructionOpsize;
  3133. CheckOperandSizes;
  3134. ConcatInstruction(curlist);
  3135. end;
  3136. instr.Free;
  3137. end;
  3138. AS_SEPARATOR :
  3139. Begin
  3140. Consume(AS_SEPARATOR);
  3141. end;
  3142. AS_TARGET_DIRECTIVE:
  3143. HandleTargetDirective;
  3144. AS_END :
  3145. break; { end assembly block }
  3146. else
  3147. Begin
  3148. Message(asmr_e_syntax_error);
  3149. RecoverConsume(false);
  3150. end;
  3151. end; { end case }
  3152. until false;
  3153. { check that all referenced local labels are defined }
  3154. checklocallabels;
  3155. { Return the list in an asmnode }
  3156. assemble:=curlist;
  3157. Message1(asmr_d_finish_reading,'intel');
  3158. end;
  3159. end.