rautils.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. This unit implements some support routines for assembler parsing
  4. independent of the processor
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. **********************************************************************}
  17. Unit RAUtils;
  18. {$i fpcdefs.inc}
  19. Interface
  20. Uses
  21. cutils,cclasses,
  22. globtype,aasmbase,aasmtai,aasmdata,cpubase,cpuinfo,cgbase,cgutils,
  23. symconst,symbase,symtype,symdef,symsym;
  24. Const
  25. RPNMax = 10; { I think you only need 4, but just to be safe }
  26. OpMax = 25;
  27. {---------------------------------------------------------------------
  28. Local Label Management
  29. ---------------------------------------------------------------------}
  30. Type
  31. { Each local label has this structure associated with it }
  32. TLocalLabel = class(TFPHashObject)
  33. Emitted : boolean;
  34. constructor Create(AList:TFPHashObjectList;const n:string);
  35. function Gettasmlabel:tasmlabel;
  36. private
  37. lab : tasmlabel;
  38. end;
  39. TLocalLabelList = class(TFPHashObjectList)
  40. procedure CheckEmitted;
  41. end;
  42. var
  43. LocalLabelList : TLocalLabelList;
  44. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  45. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  46. {---------------------------------------------------------------------
  47. Instruction management
  48. ---------------------------------------------------------------------}
  49. type
  50. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_LOCAL,
  51. OPR_REFERENCE,OPR_REGISTER,OPR_REGLIST,OPR_COND,OPR_REGSET,OPR_SHIFTEROP);
  52. TOprRec = record
  53. case typ:TOprType of
  54. OPR_NONE : ();
  55. OPR_CONSTANT : (val:aint);
  56. OPR_SYMBOL : (symbol:tasmsymbol;symofs:aint);
  57. OPR_REFERENCE : (ref:treference);
  58. OPR_LOCAL : (localsym:tabstractnormalvarsym;localsymofs:aint;localindexreg:tregister;localscale:byte;localgetoffset,localforceref:boolean);
  59. OPR_REGISTER : (reg:tregister);
  60. {$ifdef m68k}
  61. OPR_REGLIST : (regset : tcpuregisterset);
  62. {$endif m68k}
  63. {$ifdef powerpc}
  64. OPR_COND : (cond : tasmcond);
  65. {$endif powerpc}
  66. {$ifdef POWERPC64}
  67. OPR_COND : (cond : tasmcond);
  68. {$endif POWERPC64}
  69. {$ifdef arm}
  70. OPR_REGSET : (regset : tcpuregisterset);
  71. OPR_SHIFTEROP : (shifterop : tshifterop);
  72. {$endif arm}
  73. end;
  74. TOperand = class
  75. typesize : aint;
  76. hastype, { if the operand has typecasted variable }
  77. hasvar : boolean; { if the operand is loaded with a variable }
  78. size : TCGSize;
  79. opr : TOprRec;
  80. constructor create;virtual;
  81. destructor destroy;override;
  82. Procedure SetSize(_size:longint;force:boolean);virtual;
  83. Procedure SetCorrectSize(opcode:tasmop);virtual;
  84. Function SetupResult:boolean;virtual;
  85. Function SetupSelf:boolean;
  86. Function SetupOldEBP:boolean;
  87. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  88. Procedure CheckOperand; virtual;
  89. Procedure InitRef;
  90. end;
  91. TCOperand = class of TOperand;
  92. TInstruction = class
  93. opcode : tasmop;
  94. condition : tasmcond;
  95. ops : byte;
  96. labeled : boolean;
  97. operands : array[1..max_operands] of toperand;
  98. constructor create(optype : tcoperand);virtual;
  99. destructor destroy;override;
  100. { converts the instruction to an instruction how it's used by the assembler writer
  101. and concats it to the passed list, the newly created item is returned }
  102. function ConcatInstruction(p:TAsmList) : tai;virtual;
  103. Procedure Swapoperands;
  104. end;
  105. {---------------------------------------------------------------------}
  106. { Expression parser types }
  107. {---------------------------------------------------------------------}
  108. TExprOperator = record
  109. ch: char; { operator }
  110. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  111. end;
  112. {**********************************************************************}
  113. { The following operators are supported: }
  114. { '+' : addition }
  115. { '-' : subtraction }
  116. { '*' : multiplication }
  117. { '/' : modulo division }
  118. { '^' : exclusive or }
  119. { '<' : shift left }
  120. { '>' : shift right }
  121. { '&' : bitwise and }
  122. { '|' : bitwise or }
  123. { '~' : bitwise complement }
  124. { '%' : modulo division }
  125. { nnn: longint numbers }
  126. { ( and ) parenthesis }
  127. {**********************************************************************}
  128. TExprParse = class
  129. public
  130. Constructor create;
  131. Destructor Destroy;override;
  132. Function Evaluate(Expr: String): aint;
  133. Function Priority(_Operator: Char): aint;
  134. private
  135. RPNStack : Array[1..RPNMax] of aint; { Stack For RPN calculator }
  136. RPNTop : aint;
  137. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  138. OpTop : aint;
  139. Procedure RPNPush(Num: aint);
  140. Function RPNPop: aint;
  141. Procedure RPNCalc(const token: String; prefix: boolean);
  142. Procedure OpPush(_Operator: char; prefix: boolean);
  143. { In reality returns TExprOperaotr }
  144. Procedure OpPop(var _Operator:TExprOperator);
  145. end;
  146. { Evaluate an expression string to a aint }
  147. Function CalculateExpression(const expression: string): aint;
  148. {---------------------------------------------------------------------}
  149. { String routines }
  150. {---------------------------------------------------------------------}
  151. Function ParseVal(const S:String;base:byte):aint;
  152. Function PadZero(Var s: String; n: byte): Boolean;
  153. Function EscapeToPascal(const s:string): string;
  154. {---------------------------------------------------------------------
  155. Symbol helper routines
  156. ---------------------------------------------------------------------}
  157. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:TSymtable);
  158. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint; var mangledname: string; needvmtofs: boolean):boolean;
  159. Function SearchType(const hs:string;var size:aint): Boolean;
  160. Function SearchRecordType(const s:string): boolean;
  161. Function SearchIConstant(const s:string; var l:aint): boolean;
  162. {---------------------------------------------------------------------
  163. Instruction generation routines
  164. ---------------------------------------------------------------------}
  165. Procedure ConcatPasString(p : TAsmList;s:string);
  166. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  167. Procedure ConcatConstant(p : TAsmList;value: aint; constsize:byte);
  168. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:aint);
  169. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  170. Procedure ConcatString(p : TAsmList;s:string);
  171. procedure ConcatAlign(p:TAsmList;l:aint);
  172. Procedure ConcatPublic(p:TAsmList;const s : string);
  173. Procedure ConcatLocal(p:TAsmList;const s : string);
  174. Implementation
  175. uses
  176. SysUtils,
  177. defutil,systems,verbose,globals,
  178. symtable,paramgr,
  179. aasmcpu,
  180. procinfo;
  181. {*************************************************************************
  182. TExprParse
  183. *************************************************************************}
  184. Constructor TExprParse.create;
  185. Begin
  186. end;
  187. Procedure TExprParse.RPNPush(Num : aint);
  188. { Add an operand to the top of the RPN stack }
  189. begin
  190. if RPNTop < RPNMax then
  191. begin
  192. Inc(RPNTop);
  193. RPNStack[RPNTop]:=Num;
  194. end
  195. else
  196. Message(asmr_e_expr_illegal);
  197. end;
  198. Function TExprParse.RPNPop : aint;
  199. { Get the operand at the top of the RPN stack }
  200. begin
  201. if RPNTop > 0 then
  202. begin
  203. RPNPop:=RPNStack[RPNTop];
  204. Dec(RPNTop);
  205. end
  206. else
  207. Message(asmr_e_expr_illegal);
  208. end;
  209. Procedure TExprParse.RPNCalc(const Token : String; prefix:boolean); { RPN Calculator }
  210. Var
  211. Temp : aint;
  212. n1,n2 : aint;
  213. LocalError : Integer;
  214. begin
  215. { Handle operators }
  216. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  217. Case Token[1] of
  218. '+' :
  219. Begin
  220. if not prefix then
  221. RPNPush(RPNPop + RPNPop);
  222. end;
  223. '-' :
  224. Begin
  225. if prefix then
  226. RPNPush(-(RPNPop))
  227. else
  228. begin
  229. n1:=RPNPop;
  230. n2:=RPNPop;
  231. RPNPush(n2 - n1);
  232. end;
  233. end;
  234. '*' : RPNPush(RPNPop * RPNPop);
  235. '&' :
  236. begin
  237. n1:=RPNPop;
  238. n2:=RPNPop;
  239. RPNPush(n2 and n1);
  240. end;
  241. '|' :
  242. begin
  243. n1:=RPNPop;
  244. n2:=RPNPop;
  245. RPNPush(n2 or n1);
  246. end;
  247. '~' : RPNPush(NOT RPNPop);
  248. '<' :
  249. begin
  250. n1:=RPNPop;
  251. n2:=RPNPop;
  252. RPNPush(n2 SHL n1);
  253. end;
  254. '>' :
  255. begin
  256. n1:=RPNPop;
  257. n2:=RPNPop;
  258. RPNPush(n2 SHR n1);
  259. end;
  260. '%' :
  261. begin
  262. Temp:=RPNPop;
  263. if Temp <> 0 then
  264. RPNPush(RPNPop mod Temp)
  265. else
  266. begin
  267. Message(asmr_e_expr_zero_divide);
  268. { push 1 for error recovery }
  269. RPNPush(1);
  270. end;
  271. end;
  272. '^' : RPNPush(RPNPop XOR RPNPop);
  273. '/' :
  274. begin
  275. Temp:=RPNPop;
  276. if Temp <> 0 then
  277. RPNPush(RPNPop div Temp)
  278. else
  279. begin
  280. Message(asmr_e_expr_zero_divide);
  281. { push 1 for error recovery }
  282. RPNPush(1);
  283. end;
  284. end;
  285. end
  286. else
  287. begin
  288. { Convert String to number and add to stack }
  289. Val(Token, Temp, LocalError);
  290. if LocalError = 0 then
  291. RPNPush(Temp)
  292. else
  293. begin
  294. Message(asmr_e_expr_illegal);
  295. { push 1 for error recovery }
  296. RPNPush(1);
  297. end;
  298. end;
  299. end;
  300. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  301. { Add an operator onto top of the stack }
  302. begin
  303. if OpTop < OpMax then
  304. begin
  305. Inc(OpTop);
  306. OpStack[OpTop].ch:=_Operator;
  307. OpStack[OpTop].is_prefix:=prefix;
  308. end
  309. else
  310. Message(asmr_e_expr_illegal);
  311. end;
  312. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  313. { Get operator at the top of the stack }
  314. begin
  315. if OpTop > 0 then
  316. begin
  317. _Operator:=OpStack[OpTop];
  318. Dec(OpTop);
  319. end
  320. else
  321. Message(asmr_e_expr_illegal);
  322. end;
  323. Function TExprParse.Priority(_Operator : Char) : aint;
  324. { Return priority of operator }
  325. { The greater the priority, the higher the precedence }
  326. begin
  327. Case _Operator OF
  328. '(' :
  329. Priority:=0;
  330. '+', '-' :
  331. Priority:=1;
  332. '*', '/','%','<','>' :
  333. Priority:=2;
  334. '|','&','^','~' :
  335. Priority:=0;
  336. else
  337. Message(asmr_e_expr_illegal);
  338. end;
  339. end;
  340. Function TExprParse.Evaluate(Expr : String):aint;
  341. Var
  342. I : longint;
  343. Token : String;
  344. opr : TExprOperator;
  345. begin
  346. Evaluate:=0;
  347. { Reset stacks }
  348. OpTop :=0;
  349. RPNTop:=0;
  350. Token :='';
  351. { nothing to do ? }
  352. if Expr='' then
  353. exit;
  354. For I:=1 to Length(Expr) DO
  355. begin
  356. if Expr[I] in ['0'..'9'] then
  357. begin { Build multi-digit numbers }
  358. Token:=Token + Expr[I];
  359. if I = Length(Expr) then { Send last one to calculator }
  360. RPNCalc(Token,false);
  361. end
  362. else
  363. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  364. begin
  365. if Token <> '' then
  366. begin { Send last built number to calc. }
  367. RPNCalc(Token,false);
  368. Token:='';
  369. end;
  370. Case Expr[I] OF
  371. '(' : OpPush('(',false);
  372. ')' : begin
  373. While (OpTop>0) and (OpStack[OpTop].ch <> '(') DO
  374. Begin
  375. OpPop(opr);
  376. RPNCalc(opr.ch,opr.is_prefix);
  377. end;
  378. OpPop(opr); { Pop off and ignore the '(' }
  379. end;
  380. '+','-','~' : Begin
  381. { workaround for -2147483648 }
  382. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  383. begin
  384. token:='-';
  385. expr[i]:='+';
  386. end;
  387. { if start of expression then surely a prefix }
  388. { or if previous char was also an operator }
  389. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  390. OpPush(Expr[I],true)
  391. else
  392. Begin
  393. { Evaluate all higher priority operators }
  394. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  395. Begin
  396. OpPop(opr);
  397. RPNCalc(opr.ch,opr.is_prefix);
  398. end;
  399. OpPush(Expr[I],false);
  400. End;
  401. end;
  402. '*', '/',
  403. '^','|','&',
  404. '%','<','>' : begin
  405. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  406. Begin
  407. OpPop(opr);
  408. RPNCalc(opr.ch,opr.is_prefix);
  409. end;
  410. OpPush(Expr[I],false);
  411. end;
  412. end; { Case }
  413. end
  414. else
  415. Message(asmr_e_expr_illegal); { Handle bad input error }
  416. end;
  417. { Pop off the remaining operators }
  418. While OpTop > 0 do
  419. Begin
  420. OpPop(opr);
  421. RPNCalc(opr.ch,opr.is_prefix);
  422. end;
  423. { The result is stored on the top of the stack }
  424. Evaluate:=RPNPop;
  425. end;
  426. Destructor TExprParse.Destroy;
  427. Begin
  428. end;
  429. Function CalculateExpression(const expression: string): aint;
  430. var
  431. expr: TExprParse;
  432. Begin
  433. expr:=TExprParse.create;
  434. CalculateExpression:=expr.Evaluate(expression);
  435. expr.Free;
  436. end;
  437. {*************************************************************************}
  438. { String conversions/utils }
  439. {*************************************************************************}
  440. Function EscapeToPascal(const s:string): string;
  441. { converts a C styled string - which contains escape }
  442. { characters to a pascal style string. }
  443. var
  444. i,len : aint;
  445. hs : string;
  446. temp : string;
  447. c : char;
  448. Begin
  449. hs:='';
  450. len:=0;
  451. i:=0;
  452. while (i<length(s)) and (len<255) do
  453. begin
  454. Inc(i);
  455. if (s[i]='\') and (i<length(s)) then
  456. Begin
  457. inc(i);
  458. case s[i] of
  459. '\' :
  460. c:='\';
  461. 'b':
  462. c:=#8;
  463. 'f':
  464. c:=#12;
  465. 'n':
  466. c:=#10;
  467. 'r':
  468. c:=#13;
  469. 't':
  470. c:=#9;
  471. '"':
  472. c:='"';
  473. '0'..'7':
  474. Begin
  475. temp:=s[i];
  476. temp:=temp+s[i+1];
  477. temp:=temp+s[i+2];
  478. inc(i,2);
  479. c:=chr(ParseVal(temp,8));
  480. end;
  481. 'x':
  482. Begin
  483. temp:=s[i+1];
  484. temp:=temp+s[i+2];
  485. inc(i,2);
  486. c:=chr(ParseVal(temp,16));
  487. end;
  488. else
  489. Begin
  490. Message1(asmr_e_escape_seq_ignored,s[i]);
  491. c:=s[i];
  492. end;
  493. end;
  494. end
  495. else
  496. c:=s[i];
  497. inc(len);
  498. hs[len]:=c;
  499. end;
  500. hs[0]:=chr(len);
  501. EscapeToPascal:=hs;
  502. end;
  503. Function ParseVal(const S:String;base:byte):aint;
  504. { Converts a decimal string to aint }
  505. var
  506. code : integer;
  507. errmsg : word;
  508. prefix : string[2];
  509. Begin
  510. case base of
  511. 2 :
  512. begin
  513. errmsg:=asmr_e_error_converting_binary;
  514. prefix:='%';
  515. end;
  516. 8 :
  517. begin
  518. errmsg:=asmr_e_error_converting_octal;
  519. prefix:='&';
  520. end;
  521. 10 :
  522. begin
  523. errmsg:=asmr_e_error_converting_decimal;
  524. prefix:='';
  525. end;
  526. 16 :
  527. begin
  528. errmsg:=asmr_e_error_converting_hexadecimal;
  529. prefix:='$';
  530. end;
  531. else
  532. internalerror(200501202);
  533. end;
  534. val(prefix+s,result,code);
  535. if code<>0 then
  536. begin
  537. val(prefix+s,aword(result),code);
  538. if code<>0 then
  539. begin
  540. Message1(errmsg,s);
  541. result:=0;
  542. end;
  543. end;
  544. end;
  545. Function PadZero(Var s: String; n: byte): Boolean;
  546. Begin
  547. PadZero:=TRUE;
  548. { Do some error checking first }
  549. if Length(s) = n then
  550. exit
  551. else
  552. if Length(s) > n then
  553. Begin
  554. PadZero:=FALSE;
  555. delete(s,n+1,length(s));
  556. exit;
  557. end
  558. else
  559. PadZero:=TRUE;
  560. { Fill it up with the specified character }
  561. fillchar(s[length(s)+1],n-1,#0);
  562. s[0]:=chr(n);
  563. end;
  564. {****************************************************************************
  565. TOperand
  566. ****************************************************************************}
  567. constructor TOperand.Create;
  568. begin
  569. size:=OS_NO;
  570. hastype:=false;
  571. hasvar:=false;
  572. FillChar(Opr,sizeof(Opr),0);
  573. end;
  574. destructor TOperand.destroy;
  575. begin
  576. end;
  577. Procedure TOperand.SetSize(_size:longint;force:boolean);
  578. begin
  579. if force or
  580. ((size = OS_NO) and (_size<=16)) then
  581. Begin
  582. case _size of
  583. 1 : size:=OS_8;
  584. 2 : size:=OS_16{ could be S_IS};
  585. 4 : size:=OS_32{ could be S_IL or S_FS};
  586. 8 : size:=OS_64{ could be S_D or S_FL};
  587. 10 : size:=OS_F80;
  588. 16 : size:=OS_128;
  589. end;
  590. end;
  591. end;
  592. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  593. begin
  594. end;
  595. function TOperand.SetupResult:boolean;
  596. begin
  597. SetupResult:=false;
  598. { replace by correct offset. }
  599. with current_procinfo.procdef do
  600. if (not is_void(returndef)) then
  601. begin
  602. if (m_tp7 in current_settings.modeswitches) and
  603. (not paramanager.ret_in_param(returndef,proccalloption)) then
  604. begin
  605. message(asmr_e_cannot_use_RESULT_here);
  606. exit;
  607. end;
  608. SetupResult:=setupvar('result',false)
  609. end
  610. else
  611. message(asmr_e_void_function);
  612. end;
  613. Function TOperand.SetupSelf:boolean;
  614. Begin
  615. SetupSelf:=false;
  616. if assigned(current_procinfo.procdef._class) then
  617. SetupSelf:=setupvar('self',false)
  618. else
  619. Message(asmr_e_cannot_use_SELF_outside_a_method);
  620. end;
  621. Function TOperand.SetupOldEBP:boolean;
  622. Begin
  623. SetupOldEBP:=false;
  624. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  625. SetupOldEBP:=setupvar('parentframe',false)
  626. else
  627. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  628. end;
  629. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  630. function symtable_has_localvarsyms(st:TSymtable):boolean;
  631. var
  632. sym : tsym;
  633. i : longint;
  634. begin
  635. result:=false;
  636. for i:=0 to st.SymList.Count-1 do
  637. begin
  638. sym:=tsym(st.SymList[i]);
  639. if sym.typ=localvarsym then
  640. begin
  641. result:=true;
  642. exit;
  643. end;
  644. end;
  645. end;
  646. procedure setconst(l:aint);
  647. begin
  648. { We return the address of the field, just like Delphi/TP }
  649. case opr.typ of
  650. OPR_NONE :
  651. begin
  652. opr.typ:=OPR_CONSTANT;
  653. opr.val:=l;
  654. end;
  655. OPR_CONSTANT :
  656. inc(opr.val,l);
  657. OPR_REFERENCE :
  658. inc(opr.ref.offset,l);
  659. OPR_LOCAL :
  660. inc(opr.localsymofs,l);
  661. else
  662. Message(asmr_e_invalid_operand_type);
  663. end;
  664. end;
  665. { search and sets up the correct fields in the Instr record }
  666. { for the NON-constant identifier passed to the routine. }
  667. { if not found returns FALSE. }
  668. var
  669. sym : tsym;
  670. srsymtable : TSymtable;
  671. harrdef : tarraydef;
  672. indexreg : tregister;
  673. l : aint;
  674. plist : ppropaccesslistitem;
  675. Begin
  676. SetupVar:=false;
  677. asmsearchsym(s,sym,srsymtable);
  678. if sym = nil then
  679. exit;
  680. if sym.typ=absolutevarsym then
  681. begin
  682. if (tabsolutevarsym(sym).abstyp=tovar) then
  683. begin
  684. { Only support simple loads }
  685. plist:=tabsolutevarsym(sym).ref.firstsym;
  686. if assigned(plist) and
  687. (plist^.sltype=sl_load) then
  688. sym:=plist^.sym
  689. else
  690. begin
  691. Message(asmr_e_unsupported_symbol_type);
  692. exit;
  693. end;
  694. end
  695. else
  696. begin
  697. Message(asmr_e_unsupported_symbol_type);
  698. exit;
  699. end;
  700. end;
  701. case sym.typ of
  702. fieldvarsym :
  703. begin
  704. setconst(tfieldvarsym(sym).fieldoffset);
  705. hasvar:=true;
  706. SetupVar:=true;
  707. end;
  708. staticvarsym,
  709. localvarsym,
  710. paravarsym :
  711. begin
  712. { we always assume in asm statements that }
  713. { that the variable is valid. }
  714. tabstractvarsym(sym).varstate:=vs_readwritten;
  715. inc(tabstractvarsym(sym).refs);
  716. { variable can't be placed in a register }
  717. tabstractvarsym(sym).varregable:=vr_none;
  718. { and anything may happen with its address }
  719. tabstractvarsym(sym).addr_taken:=true;
  720. case sym.typ of
  721. staticvarsym :
  722. begin
  723. initref;
  724. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(sym).mangledname);
  725. end;
  726. paravarsym,
  727. localvarsym :
  728. begin
  729. if opr.typ=OPR_REFERENCE then
  730. begin
  731. indexreg:=opr.ref.base;
  732. if opr.ref.index<>NR_NO then
  733. begin
  734. if indexreg=NR_NO then
  735. indexreg:=opr.ref.index
  736. else
  737. Message(asmr_e_multiple_index);
  738. end;
  739. end
  740. else
  741. indexreg:=NR_NO;
  742. opr.typ:=OPR_LOCAL;
  743. if assigned(current_procinfo.parent) and
  744. not(po_inline in current_procinfo.procdef.procoptions) and
  745. (sym.owner<>current_procinfo.procdef.localst) and
  746. (sym.owner<>current_procinfo.procdef.parast) and
  747. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  748. symtable_has_localvarsyms(current_procinfo.procdef.localst) then
  749. message1(asmr_e_local_para_unreachable,s);
  750. opr.localsym:=tabstractnormalvarsym(sym);
  751. opr.localsymofs:=0;
  752. opr.localindexreg:=indexreg;
  753. opr.localscale:=0;
  754. opr.localgetoffset:=GetOffset;
  755. if paramanager.push_addr_param(tabstractvarsym(sym).varspez,tabstractvarsym(sym).vardef,current_procinfo.procdef.proccalloption) then
  756. SetSize(sizeof(aint),false);
  757. end;
  758. end;
  759. case tabstractvarsym(sym).vardef.typ of
  760. orddef,
  761. enumdef,
  762. pointerdef,
  763. floatdef :
  764. SetSize(tabstractvarsym(sym).getsize,false);
  765. arraydef :
  766. begin
  767. { for arrays try to get the element size, take care of
  768. multiple indexes }
  769. harrdef:=tarraydef(tabstractvarsym(sym).vardef);
  770. while assigned(harrdef.elementdef) and
  771. (harrdef.elementdef.typ=arraydef) do
  772. harrdef:=tarraydef(harrdef.elementdef);
  773. if not is_packed_array(harrdef) then
  774. SetSize(harrdef.elesize,false)
  775. else
  776. begin
  777. if (harrdef.elepackedbitsize mod 8) = 0 then
  778. SetSize(harrdef.elepackedbitsize div 8,false);
  779. end;
  780. end;
  781. end;
  782. hasvar:=true;
  783. SetupVar:=true;
  784. Exit;
  785. end;
  786. constsym :
  787. begin
  788. if tconstsym(sym).consttyp=constord then
  789. begin
  790. setconst(tconstsym(sym).value.valueord.svalue);
  791. SetupVar:=true;
  792. Exit;
  793. end;
  794. end;
  795. typesym :
  796. begin
  797. if ttypesym(sym).typedef.typ in [recorddef,objectdef] then
  798. begin
  799. setconst(0);
  800. SetupVar:=TRUE;
  801. Exit;
  802. end;
  803. end;
  804. procsym :
  805. begin
  806. if opr.typ<>OPR_NONE then
  807. Message(asmr_e_invalid_operand_type);
  808. if Tprocsym(sym).ProcdefList.Count>1 then
  809. Message(asmr_w_calling_overload_func);
  810. l:=opr.ref.offset;
  811. opr.typ:=OPR_SYMBOL;
  812. opr.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname);
  813. opr.symofs:=l;
  814. hasvar:=true;
  815. SetupVar:=TRUE;
  816. Exit;
  817. end;
  818. else
  819. begin
  820. Message(asmr_e_unsupported_symbol_type);
  821. exit;
  822. end;
  823. end;
  824. end;
  825. procedure TOperand.InitRef;
  826. {*********************************************************************}
  827. { Description: This routine first check if the opcode is of }
  828. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  829. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  830. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  831. { to point to the default segment. }
  832. {*********************************************************************}
  833. var
  834. l : aint;
  835. hsymofs : aint;
  836. hsymbol : tasmsymbol;
  837. reg : tregister;
  838. Begin
  839. case opr.typ of
  840. OPR_REFERENCE :
  841. exit;
  842. OPR_CONSTANT :
  843. begin
  844. l:=opr.val;
  845. opr.typ:=OPR_REFERENCE;
  846. Fillchar(opr.ref,sizeof(treference),0);
  847. opr.Ref.Offset:=l;
  848. end;
  849. OPR_NONE :
  850. begin
  851. opr.typ:=OPR_REFERENCE;
  852. Fillchar(opr.ref,sizeof(treference),0);
  853. end;
  854. OPR_REGISTER :
  855. begin
  856. reg:=opr.reg;
  857. opr.typ:=OPR_REFERENCE;
  858. Fillchar(opr.ref,sizeof(treference),0);
  859. opr.Ref.base:=reg;
  860. end;
  861. OPR_SYMBOL :
  862. begin
  863. hsymbol:=opr.symbol;
  864. hsymofs:=opr.symofs;
  865. opr.typ:=OPR_REFERENCE;
  866. Fillchar(opr.ref,sizeof(treference),0);
  867. opr.ref.symbol:=hsymbol;
  868. opr.ref.offset:=hsymofs;
  869. end;
  870. else
  871. begin
  872. Message(asmr_e_invalid_operand_type);
  873. { Recover }
  874. opr.typ:=OPR_REFERENCE;
  875. Fillchar(opr.ref,sizeof(treference),0);
  876. end;
  877. end;
  878. end;
  879. Procedure TOperand.CheckOperand;
  880. {*********************************************************************}
  881. { Description: This routine checks if the operand is of }
  882. { valid. Does nothing by default. }
  883. {*********************************************************************}
  884. begin
  885. end;
  886. {****************************************************************************
  887. TInstruction
  888. ****************************************************************************}
  889. constructor TInstruction.create(optype : tcoperand);
  890. var
  891. i : longint;
  892. Begin
  893. { these field are set to 0 anyways by the constructor helper (FK)
  894. Opcode:=A_NONE;
  895. Condition:=C_NONE;
  896. Ops:=0;
  897. }
  898. for i:=1 to max_operands do
  899. Operands[i]:=optype.create;
  900. Labeled:=false;
  901. end;
  902. destructor TInstruction.destroy;
  903. var
  904. i : longint;
  905. Begin
  906. for i:=1 to max_operands do
  907. Operands[i].free;
  908. end;
  909. Procedure TInstruction.Swapoperands;
  910. Var
  911. p : toperand;
  912. Begin
  913. case Ops of
  914. 2 :
  915. begin
  916. p:=Operands[1];
  917. Operands[1]:=Operands[2];
  918. Operands[2]:=p;
  919. end;
  920. 3 :
  921. begin
  922. p:=Operands[1];
  923. Operands[1]:=Operands[3];
  924. Operands[3]:=p;
  925. end;
  926. end;
  927. end;
  928. function TInstruction.ConcatInstruction(p:TAsmList) : tai;
  929. var
  930. ai : taicpu;
  931. i : longint;
  932. begin
  933. for i:=1 to Ops do
  934. operands[i].CheckOperand;
  935. ai:=taicpu.op_none(opcode);
  936. ai.Ops:=Ops;
  937. ai.Allocate_oper(Ops);
  938. for i:=1 to Ops do
  939. with operands[i].opr do
  940. begin
  941. case typ of
  942. OPR_CONSTANT :
  943. ai.loadconst(i-1,val);
  944. OPR_REGISTER:
  945. ai.loadreg(i-1,reg);
  946. OPR_SYMBOL:
  947. ai.loadsymbol(i-1,symbol,symofs);
  948. OPR_LOCAL :
  949. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  950. localscale,localgetoffset,localforceref);
  951. OPR_REFERENCE:
  952. ai.loadref(i-1,ref);
  953. {$ifdef ARM}
  954. OPR_REGSET:
  955. ai.loadregset(i-1,regset);
  956. OPR_SHIFTEROP:
  957. ai.loadshifterop(i-1,shifterop);
  958. {$endif ARM}
  959. else
  960. internalerror(200501051);
  961. end;
  962. end;
  963. ai.SetCondition(condition);
  964. { Concat the opcode or give an error }
  965. if assigned(ai) then
  966. p.concat(ai)
  967. else
  968. Message(asmr_e_invalid_opcode_and_operand);
  969. result:=ai;
  970. end;
  971. {***************************************************************************
  972. TLocalLabel
  973. ***************************************************************************}
  974. constructor TLocalLabel.create(AList:TFPHashObjectList;const n:string);
  975. begin
  976. inherited Create(AList,n);
  977. lab:=nil;
  978. emitted:=false;
  979. end;
  980. function TLocalLabel.Gettasmlabel:tasmlabel;
  981. begin
  982. if not assigned(lab) then
  983. begin
  984. current_asmdata.getjumplabel(lab);
  985. { this label is forced to be used so it's always written }
  986. lab.increfs;
  987. end;
  988. Gettasmlabel:=lab;
  989. end;
  990. {***************************************************************************
  991. TLocalLabelList
  992. ***************************************************************************}
  993. procedure TLocalLabelList.CheckEmitted;
  994. var
  995. i : longint;
  996. lab : TLocalLabel;
  997. begin
  998. for i:=0 to LocalLabelList.Count-1 do
  999. begin
  1000. lab:=TLocalLabel(LocalLabelList[i]);
  1001. if not lab.emitted then
  1002. Message1(asmr_e_unknown_label_identifier,lab.name);
  1003. end;
  1004. end;
  1005. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1006. var
  1007. lab : TLocalLabel;
  1008. Begin
  1009. CreateLocalLabel:=true;
  1010. { Check if it already is defined }
  1011. lab:=TLocalLabel(LocalLabellist.Find(s));
  1012. if not assigned(lab) then
  1013. lab:=TLocalLabel.Create(LocalLabellist,s);
  1014. { set emitted flag and check for dup syms }
  1015. if emit then
  1016. begin
  1017. if lab.Emitted then
  1018. begin
  1019. Message1(asmr_e_dup_local_sym,lab.Name);
  1020. CreateLocalLabel:=false;
  1021. end;
  1022. lab.Emitted:=true;
  1023. end;
  1024. hl:=lab.Gettasmlabel;
  1025. end;
  1026. {****************************************************************************
  1027. Symbol table helper routines
  1028. ****************************************************************************}
  1029. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:TSymtable);
  1030. var
  1031. i : integer;
  1032. begin
  1033. i:=pos('.',s);
  1034. { allow unit.identifier }
  1035. if i>0 then
  1036. begin
  1037. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1038. if assigned(srsym) then
  1039. begin
  1040. if (srsym.typ=unitsym) and
  1041. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  1042. srsym.owner.iscurrentunit then
  1043. searchsym_in_module(tunitsym(srsym).module,Copy(s,i+1,255),srsym,srsymtable)
  1044. else
  1045. begin
  1046. srsym:=nil;
  1047. srsymtable:=nil;
  1048. end;
  1049. end;
  1050. end
  1051. else
  1052. searchsym(s,srsym,srsymtable);
  1053. end;
  1054. Function SearchType(const hs:string;var size:aint): Boolean;
  1055. var
  1056. srsym : tsym;
  1057. srsymtable : TSymtable;
  1058. begin
  1059. result:=false;
  1060. size:=0;
  1061. asmsearchsym(hs,srsym,srsymtable);
  1062. if assigned(srsym) and
  1063. (srsym.typ=typesym) then
  1064. begin
  1065. size:=ttypesym(srsym).typedef.size;
  1066. result:=true;
  1067. end;
  1068. end;
  1069. Function SearchRecordType(const s:string): boolean;
  1070. var
  1071. srsym : tsym;
  1072. srsymtable : TSymtable;
  1073. Begin
  1074. SearchRecordType:=false;
  1075. { Check the constants in symtable }
  1076. asmsearchsym(s,srsym,srsymtable);
  1077. if srsym <> nil then
  1078. Begin
  1079. case srsym.typ of
  1080. typesym :
  1081. begin
  1082. if ttypesym(srsym).typedef.typ in [recorddef,objectdef] then
  1083. begin
  1084. SearchRecordType:=true;
  1085. exit;
  1086. end;
  1087. end;
  1088. fieldvarsym :
  1089. begin
  1090. if (tfieldvarsym(srsym).vardef.typ in [recorddef,objectdef]) then
  1091. begin
  1092. SearchRecordType:=true;
  1093. exit;
  1094. end;
  1095. end;
  1096. end;
  1097. end;
  1098. end;
  1099. Function SearchIConstant(const s:string; var l:aint): boolean;
  1100. {**********************************************************************}
  1101. { Description: Searches for a CONSTANT of name s in either the local }
  1102. { symbol list, then in the global symbol list, and returns the value }
  1103. { of that constant in l. Returns TRUE if successfull, if not found, }
  1104. { or if the constant is not of correct type, then returns FALSE }
  1105. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1106. { respectively. }
  1107. {**********************************************************************}
  1108. var
  1109. srsym : tsym;
  1110. srsymtable : TSymtable;
  1111. Begin
  1112. SearchIConstant:=false;
  1113. { check for TRUE or FALSE reserved words first }
  1114. if s = 'TRUE' then
  1115. Begin
  1116. SearchIConstant:=TRUE;
  1117. l:=1;
  1118. exit;
  1119. end;
  1120. if s = 'FALSE' then
  1121. Begin
  1122. SearchIConstant:=TRUE;
  1123. l:=0;
  1124. exit;
  1125. end;
  1126. { Check the constants in symtable }
  1127. asmsearchsym(s,srsym,srsymtable);
  1128. if srsym <> nil then
  1129. Begin
  1130. case srsym.typ of
  1131. constsym :
  1132. begin
  1133. if tconstsym(srsym).consttyp=constord then
  1134. Begin
  1135. l:=tconstsym(srsym).value.valueord.svalue;
  1136. SearchIConstant:=TRUE;
  1137. exit;
  1138. end;
  1139. end;
  1140. enumsym:
  1141. Begin
  1142. l:=tenumsym(srsym).value;
  1143. SearchIConstant:=TRUE;
  1144. exit;
  1145. end;
  1146. end;
  1147. end;
  1148. end;
  1149. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint; var mangledname: string; needvmtofs: boolean):boolean;
  1150. { search and returns the offset and size of records/objects of the base }
  1151. { with field name setup in field. }
  1152. { returns FALSE if not found. }
  1153. { used when base is a variable or a typed constant name. }
  1154. var
  1155. st : TSymtable;
  1156. harrdef : tarraydef;
  1157. sym : tsym;
  1158. srsymtable : TSymtable;
  1159. i : longint;
  1160. base : string;
  1161. procdef: tprocdef;
  1162. Begin
  1163. GetRecordOffsetSize:=FALSE;
  1164. Offset:=0;
  1165. Size:=0;
  1166. mangledname:='';
  1167. i:=pos('.',s);
  1168. if i=0 then
  1169. i:=255;
  1170. base:=Copy(s,1,i-1);
  1171. delete(s,1,i);
  1172. if base='SELF' then
  1173. st:=current_procinfo.procdef._class.symtable
  1174. else
  1175. begin
  1176. asmsearchsym(base,sym,srsymtable);
  1177. st:=nil;
  1178. { we can start with a var,type,typedconst }
  1179. if assigned(sym) then
  1180. case sym.typ of
  1181. staticvarsym,
  1182. localvarsym,
  1183. paravarsym :
  1184. st:=Tabstractvarsym(sym).vardef.GetSymtable(gs_record);
  1185. typesym :
  1186. st:=Ttypesym(sym).typedef.GetSymtable(gs_record);
  1187. end
  1188. else
  1189. s:='';
  1190. end;
  1191. { now walk all recordsymtables }
  1192. while assigned(st) and (s<>'') do
  1193. begin
  1194. { load next field in base }
  1195. i:=pos('.',s);
  1196. if i=0 then
  1197. i:=255;
  1198. base:=Copy(s,1,i-1);
  1199. delete(s,1,i);
  1200. if st.symtabletype=ObjectSymtable then
  1201. sym:=search_class_member(tobjectdef(st.defowner),base)
  1202. else
  1203. sym:=tsym(st.Find(base));
  1204. if not assigned(sym) then
  1205. begin
  1206. GetRecordOffsetSize:=false;
  1207. exit;
  1208. end;
  1209. st:=nil;
  1210. case sym.typ of
  1211. fieldvarsym :
  1212. with Tfieldvarsym(sym) do
  1213. begin
  1214. inc(Offset,fieldoffset);
  1215. size:=getsize;
  1216. case vardef.typ of
  1217. arraydef :
  1218. begin
  1219. { for arrays try to get the element size, take care of
  1220. multiple indexes }
  1221. harrdef:=tarraydef(vardef);
  1222. while assigned(harrdef.elementdef) and
  1223. (harrdef.elementdef.typ=arraydef) do
  1224. harrdef:=tarraydef(harrdef.elementdef);
  1225. if not is_packed_array(harrdef) then
  1226. size:=harrdef.elesize
  1227. else
  1228. begin
  1229. if (harrdef.elepackedbitsize mod 8) <> 0 then
  1230. Message(asmr_e_packed_element);
  1231. size := (harrdef.elepackedbitsize + 7) div 8;
  1232. end;
  1233. end;
  1234. recorddef :
  1235. st:=trecorddef(vardef).symtable;
  1236. objectdef :
  1237. st:=tobjectdef(vardef).symtable;
  1238. end;
  1239. end;
  1240. procsym:
  1241. begin
  1242. st:=nil;
  1243. if Tprocsym(sym).ProcdefList.Count>1 then
  1244. Message(asmr_w_calling_overload_func);
  1245. procdef:=tprocdef(tprocsym(sym).ProcdefList[0]);
  1246. if (not needvmtofs) then
  1247. begin
  1248. mangledname:=procdef.mangledname;
  1249. end
  1250. else
  1251. begin
  1252. { can only get the vmtoffset of virtual methods }
  1253. if not(po_virtualmethod in procdef.procoptions) then
  1254. Message1(asmr_e_no_vmtoffset_possible,FullTypeName(procdef,nil))
  1255. else
  1256. begin
  1257. { size = sizeof(target_system_pointer) }
  1258. size:=sizeof(aint);
  1259. offset:=procdef._class.vmtmethodoffset(procdef.extnumber)
  1260. end;
  1261. end;
  1262. { if something comes after the procsym, it's invalid assembler syntax }
  1263. GetRecordOffsetSize:=(s='');
  1264. exit;
  1265. end;
  1266. end;
  1267. end;
  1268. { Support Field.Type as typecasting }
  1269. if (st=nil) and (s<>'') then
  1270. begin
  1271. asmsearchsym(s,sym,srsymtable);
  1272. if assigned(sym) and (sym.typ=typesym) then
  1273. begin
  1274. size:=ttypesym(sym).typedef.size;
  1275. s:=''
  1276. end;
  1277. end;
  1278. GetRecordOffsetSize:=(s='');
  1279. end;
  1280. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1281. var
  1282. sym : tsym;
  1283. srsymtable : TSymtable;
  1284. hs : string;
  1285. Begin
  1286. hl:=nil;
  1287. SearchLabel:=false;
  1288. { Check for pascal labels, which are case insensetive }
  1289. hs:=upper(s);
  1290. asmsearchsym(hs,sym,srsymtable);
  1291. if sym=nil then
  1292. exit;
  1293. case sym.typ of
  1294. labelsym :
  1295. begin
  1296. if not(assigned(tlabelsym(sym).asmblocklabel)) then
  1297. current_asmdata.getjumplabel(tlabelsym(sym).asmblocklabel);
  1298. hl:=tlabelsym(sym).asmblocklabel;
  1299. if emit then
  1300. tlabelsym(sym).defined:=true
  1301. else
  1302. tlabelsym(sym).used:=true;
  1303. SearchLabel:=true;
  1304. end;
  1305. end;
  1306. end;
  1307. {*************************************************************************}
  1308. { Instruction Generation Utilities }
  1309. {*************************************************************************}
  1310. Procedure ConcatString(p : TAsmList;s:string);
  1311. {*********************************************************************}
  1312. { PROCEDURE ConcatString(s:string); }
  1313. { Description: This routine adds the character chain pointed to in }
  1314. { s to the instruction linked list. }
  1315. {*********************************************************************}
  1316. Var
  1317. pc: PChar;
  1318. Begin
  1319. getmem(pc,length(s)+1);
  1320. p.concat(Tai_string.Create_pchar(strpcopy(pc,s),length(s)));
  1321. end;
  1322. Procedure ConcatPasString(p : TAsmList;s:string);
  1323. {*********************************************************************}
  1324. { PROCEDURE ConcatPasString(s:string); }
  1325. { Description: This routine adds the character chain pointed to in }
  1326. { s to the instruction linked list, contrary to ConcatString it }
  1327. { uses a pascal style string, so it conserves null characters. }
  1328. {*********************************************************************}
  1329. Begin
  1330. p.concat(Tai_string.Create(s));
  1331. end;
  1332. Procedure ConcatConstant(p: TAsmList; value: aint; constsize:byte);
  1333. {*********************************************************************}
  1334. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1335. { Description: This routine adds the value constant to the current }
  1336. { instruction linked list. }
  1337. { maxvalue -> indicates the size of the data to initialize: }
  1338. { $ff -> create a byte node. }
  1339. { $ffff -> create a word node. }
  1340. { $ffffffff -> create a dword node. }
  1341. {*********************************************************************}
  1342. var
  1343. rangelo,rangehi : int64;
  1344. Begin
  1345. case constsize of
  1346. 1 :
  1347. begin
  1348. p.concat(Tai_const.Create_8bit(byte(value)));
  1349. rangelo:=low(shortint);
  1350. rangehi:=high(byte);
  1351. end;
  1352. 2 :
  1353. begin
  1354. p.concat(Tai_const.Create_16bit(word(value)));
  1355. rangelo:=low(smallint);
  1356. rangehi:=high(word);
  1357. end;
  1358. 4 :
  1359. begin
  1360. p.concat(Tai_const.Create_32bit(longint(value)));
  1361. rangelo:=low(longint);
  1362. rangehi:=high(cardinal);
  1363. end;
  1364. 8 :
  1365. begin
  1366. p.concat(Tai_const.Create_64bit(int64(value)));
  1367. rangelo:=0;
  1368. rangehi:=0;
  1369. end;
  1370. else
  1371. internalerror(200405011);
  1372. end;
  1373. { check for out of bounds }
  1374. if (rangelo<>0) and
  1375. ((value>rangehi) or (value<rangelo)) then
  1376. Message(asmr_e_constant_out_of_bounds);
  1377. end;
  1378. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:aint);
  1379. begin
  1380. p.concat(Tai_const.Createname(sym,l));
  1381. end;
  1382. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  1383. {***********************************************************************}
  1384. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1385. { Description: This routine adds the value constant to the current }
  1386. { instruction linked list. }
  1387. { real_typ -> indicates the type of the real data to initialize: }
  1388. { s32real -> create a single node. }
  1389. { s64real -> create a double node. }
  1390. { s80real -> create an extended node. }
  1391. { s64bit -> create a comp node. }
  1392. { f32bit -> create a fixed node. (not used normally) }
  1393. {***********************************************************************}
  1394. Begin
  1395. case real_typ of
  1396. s32real : p.concat(Tai_real_32bit.Create(value));
  1397. s64real :
  1398. {$ifdef ARM}
  1399. if is_double_hilo_swapped then
  1400. p.concat(Tai_real_64bit.Create_hiloswapped(value))
  1401. else
  1402. {$endif ARM}
  1403. p.concat(Tai_real_64bit.Create(value));
  1404. s80real : p.concat(Tai_real_80bit.Create(value));
  1405. s64comp : p.concat(Tai_comp_64bit.Create(trunc(value)));
  1406. end;
  1407. end;
  1408. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  1409. {*********************************************************************}
  1410. { PROCEDURE ConcatLabel }
  1411. { Description: This routine either emits a label or a labeled }
  1412. { instruction to the linked list of instructions. }
  1413. {*********************************************************************}
  1414. begin
  1415. p.concat(Tai_label.Create(l));
  1416. end;
  1417. procedure ConcatAlign(p:TAsmList;l:aint);
  1418. {*********************************************************************}
  1419. { PROCEDURE ConcatPublic }
  1420. { Description: This routine emits an global definition to the }
  1421. { linked list of instructions.(used by AT&T styled asm) }
  1422. {*********************************************************************}
  1423. begin
  1424. p.concat(Tai_align.Create(l));
  1425. end;
  1426. procedure ConcatPublic(p:TAsmList;const s : string);
  1427. {*********************************************************************}
  1428. { PROCEDURE ConcatPublic }
  1429. { Description: This routine emits an global definition to the }
  1430. { linked list of instructions.(used by AT&T styled asm) }
  1431. {*********************************************************************}
  1432. begin
  1433. p.concat(Tai_symbol.Createname_global(s,AT_LABEL,0));
  1434. end;
  1435. procedure ConcatLocal(p:TAsmList;const s : string);
  1436. {*********************************************************************}
  1437. { PROCEDURE ConcatLocal }
  1438. { Description: This routine emits an local definition to the }
  1439. { linked list of instructions. }
  1440. {*********************************************************************}
  1441. begin
  1442. p.concat(Tai_symbol.Createname(s,AT_LABEL,0));
  1443. end;
  1444. end.