rautils.pas 47 KB

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