rax86int.pas 100 KB

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