rax86int.pas 89 KB

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