rautils.pas 48 KB

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