rautils.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  20. Interface
  21. Uses
  22. cutils,cobjects,
  23. globtype,aasm,cpubase,symconst;
  24. Const
  25. RPNMax = 10; { I think you only need 4, but just to be safe }
  26. OpMax = 25;
  27. maxoperands = 3; { Maximum operands for assembler instructions }
  28. {---------------------------------------------------------------------
  29. Local Label Management
  30. ---------------------------------------------------------------------}
  31. Type
  32. { Each local label has this structure associated with it }
  33. PLocalLabel = ^TLocalLabel;
  34. TLocalLabel = object(TNamedIndexObject)
  35. Emitted : boolean;
  36. constructor Init(const n:string);
  37. function Getpasmlabel:pasmlabel;
  38. private
  39. lab : pasmlabel;
  40. end;
  41. PLocalLabelList = ^TLocalLabelList;
  42. TLocalLabelList = Object(TDictionary)
  43. procedure CheckEmitted;
  44. end;
  45. var
  46. LocalLabelList : PLocalLabelList;
  47. function CreateLocalLabel(const s: string; var hl: pasmlabel; emit:boolean):boolean;
  48. Function SearchLabel(const s: string; var hl: pasmlabel;emit:boolean): boolean;
  49. {---------------------------------------------------------------------
  50. Instruction management
  51. ---------------------------------------------------------------------}
  52. type
  53. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_REFERENCE,OPR_REGISTER);
  54. TOprRec = record
  55. case typ:TOprType of
  56. OPR_NONE : ();
  57. OPR_CONSTANT : (val:longint);
  58. OPR_SYMBOL : (symbol:PAsmSymbol;symofs:longint);
  59. OPR_REFERENCE : (ref:treference);
  60. OPR_REGISTER : (reg:tregister);
  61. end;
  62. POperand = ^TOperand;
  63. TOperand = object
  64. size : topsize;
  65. hastype, { if the operand has typecasted variable }
  66. hasvar : boolean; { if the operand is loaded with a variable }
  67. opr : TOprRec;
  68. constructor init;
  69. destructor done;virtual;
  70. Procedure BuildOperand;virtual;
  71. Procedure SetSize(_size:longint;force:boolean);
  72. Procedure SetCorrectSize(opcode:tasmop);virtual;
  73. Function SetupResult:boolean;
  74. Function SetupSelf:boolean;
  75. Function SetupOldEBP:boolean;
  76. Function SetupVar(const hs:string;GetOffset : boolean): Boolean;
  77. Function SetupDirectVar(const hs:string): Boolean;
  78. Procedure InitRef;
  79. end;
  80. PInstruction = ^TInstruction;
  81. TInstruction = object
  82. opcode : tasmop;
  83. opsize : topsize;
  84. condition : tasmcond;
  85. ops : byte;
  86. operands : array[1..maxoperands] of POperand;
  87. constructor init;
  88. destructor done;virtual;
  89. Procedure InitOperands;virtual;
  90. Procedure BuildOpcode;virtual;
  91. procedure ConcatInstruction(p:PAasmoutput);virtual;
  92. Procedure SwapOperands;
  93. end;
  94. {---------------------------------------------------------------------}
  95. { Expression parser types }
  96. {---------------------------------------------------------------------}
  97. TExprOperator = record
  98. ch: char; { operator }
  99. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  100. end;
  101. String15 = String[15];
  102. {**********************************************************************}
  103. { The following operators are supported: }
  104. { '+' : addition }
  105. { '-' : subtraction }
  106. { '*' : multiplication }
  107. { '/' : modulo division }
  108. { '^' : exclusive or }
  109. { '<' : shift left }
  110. { '>' : shift right }
  111. { '&' : bitwise and }
  112. { '|' : bitwise or }
  113. { '~' : bitwise complement }
  114. { '%' : modulo division }
  115. { nnn: longint numbers }
  116. { ( and ) parenthesis }
  117. {**********************************************************************}
  118. TExprParse = Object
  119. public
  120. Constructor Init;
  121. Destructor Done;
  122. Function Evaluate(Expr: String): longint;
  123. Function Priority(_Operator: Char): longint; virtual;
  124. private
  125. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  126. RPNTop : longint;
  127. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  128. OpTop : longint;
  129. Procedure RPNPush(Num: Longint);
  130. Function RPNPop: Longint;
  131. Procedure RPNCalc(token: String15; prefix: boolean);
  132. Procedure OpPush(_Operator: char; prefix: boolean);
  133. { In reality returns TExprOperaotr }
  134. Procedure OpPop(var _Operator:TExprOperator);
  135. end;
  136. { Evaluate an expression string to a longint }
  137. Function CalculateExpression(const expression: string): longint;
  138. {---------------------------------------------------------------------}
  139. { String routines }
  140. {---------------------------------------------------------------------}
  141. Function ValDecimal(const S:String):longint;
  142. Function ValOctal(const S:String):longint;
  143. Function ValBinary(const S:String):longint;
  144. Function ValHexaDecimal(const S:String):longint;
  145. Function PadZero(Var s: String; n: byte): Boolean;
  146. Function EscapeToPascal(const s:string): string;
  147. {---------------------------------------------------------------------
  148. Symbol helper routines
  149. ---------------------------------------------------------------------}
  150. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  151. Function SearchType(const hs:string): Boolean;
  152. Function SearchRecordType(const s:string): boolean;
  153. Function SearchIConstant(const s:string; var l:longint): boolean;
  154. {---------------------------------------------------------------------
  155. Instruction generation routines
  156. ---------------------------------------------------------------------}
  157. Procedure ConcatPasString(p : paasmoutput;s:string);
  158. Procedure ConcatDirect(p : paasmoutput;s:string);
  159. Procedure ConcatLabel(p: paasmoutput;var l : pasmlabel);
  160. Procedure ConcatConstant(p : paasmoutput;value: longint; maxvalue: longint);
  161. Procedure ConcatConstSymbol(p : paasmoutput;const sym:string;l:longint);
  162. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  163. Procedure ConcatString(p : paasmoutput;s:string);
  164. procedure ConcatAlign(p:paasmoutput;l:longint);
  165. Procedure ConcatPublic(p:paasmoutput;const s : string);
  166. Procedure ConcatLocal(p:paasmoutput;const s : string);
  167. Procedure ConcatGlobalBss(const s : string;size : longint);
  168. Procedure ConcatLocalBss(const s : string;size : longint);
  169. Implementation
  170. uses
  171. {$ifdef delphi}
  172. sysutils,
  173. {$else}
  174. strings,
  175. {$endif}
  176. types,systems,verbose,globals,fmodule,
  177. symtable,cpuasm
  178. {$ifdef NEWCG}
  179. ,cgbase;
  180. {$else}
  181. ,hcodegen;
  182. {$endif}
  183. {*************************************************************************
  184. TExprParse
  185. *************************************************************************}
  186. Constructor TExprParse.Init;
  187. Begin
  188. end;
  189. Procedure TExprParse.RPNPush(Num : longint);
  190. { Add an operand to the top of the RPN stack }
  191. begin
  192. if RPNTop < RPNMax then
  193. begin
  194. Inc(RPNTop);
  195. RPNStack[RPNTop]:=Num;
  196. end
  197. else
  198. Message(asmr_e_expr_illegal);
  199. end;
  200. Function TExprParse.RPNPop : longint;
  201. { Get the operand at the top of the RPN stack }
  202. begin
  203. if RPNTop > 0 then
  204. begin
  205. RPNPop:=RPNStack[RPNTop];
  206. Dec(RPNTop);
  207. end
  208. else
  209. Message(asmr_e_expr_illegal);
  210. end;
  211. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  212. Var
  213. Temp : longint;
  214. n1,n2 : longint;
  215. LocalError : Integer;
  216. begin
  217. { Handle operators }
  218. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  219. Case Token[1] of
  220. '+' :
  221. Begin
  222. if not prefix then
  223. RPNPush(RPNPop + RPNPop);
  224. end;
  225. '-' :
  226. Begin
  227. if prefix then
  228. RPNPush(-(RPNPop))
  229. else
  230. begin
  231. n1:=RPNPop;
  232. n2:=RPNPop;
  233. RPNPush(n2 - n1);
  234. end;
  235. end;
  236. '*' : RPNPush(RPNPop * RPNPop);
  237. '&' :
  238. begin
  239. n1:=RPNPop;
  240. n2:=RPNPop;
  241. RPNPush(n2 and n1);
  242. end;
  243. '|' :
  244. begin
  245. n1:=RPNPop;
  246. n2:=RPNPop;
  247. RPNPush(n2 or n1);
  248. end;
  249. '~' : RPNPush(NOT RPNPop);
  250. '<' :
  251. begin
  252. n1:=RPNPop;
  253. n2:=RPNPop;
  254. RPNPush(n2 SHL n1);
  255. end;
  256. '>' :
  257. begin
  258. n1:=RPNPop;
  259. n2:=RPNPop;
  260. RPNPush(n2 SHR n1);
  261. end;
  262. '%' :
  263. begin
  264. Temp:=RPNPop;
  265. if Temp <> 0 then
  266. RPNPush(RPNPop mod Temp)
  267. else
  268. begin
  269. Message(asmr_e_expr_zero_divide);
  270. { push 1 for error recovery }
  271. RPNPush(1);
  272. end;
  273. end;
  274. '^' : RPNPush(RPNPop XOR RPNPop);
  275. '/' :
  276. begin
  277. Temp:=RPNPop;
  278. if Temp <> 0 then
  279. RPNPush(RPNPop div Temp)
  280. else
  281. begin
  282. Message(asmr_e_expr_zero_divide);
  283. { push 1 for error recovery }
  284. RPNPush(1);
  285. end;
  286. end;
  287. end
  288. else
  289. begin
  290. { Convert String to number and add to stack }
  291. if token='-2147483648' then
  292. begin
  293. temp:=$80000000;
  294. localerror:=0;
  295. end
  296. else
  297. Val(Token, Temp, LocalError);
  298. if LocalError = 0 then
  299. RPNPush(Temp)
  300. else
  301. begin
  302. Message(asmr_e_expr_illegal);
  303. { push 1 for error recovery }
  304. RPNPush(1);
  305. end;
  306. end;
  307. end;
  308. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  309. { Add an operator onto top of the stack }
  310. begin
  311. if OpTop < OpMax then
  312. begin
  313. Inc(OpTop);
  314. OpStack[OpTop].ch:=_Operator;
  315. OpStack[OpTop].is_prefix:=prefix;
  316. end
  317. else
  318. Message(asmr_e_expr_illegal);
  319. end;
  320. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  321. { Get operator at the top of the stack }
  322. begin
  323. if OpTop > 0 then
  324. begin
  325. _Operator:=OpStack[OpTop];
  326. Dec(OpTop);
  327. end
  328. else
  329. Message(asmr_e_expr_illegal);
  330. end;
  331. Function TExprParse.Priority(_Operator : Char) : longint;
  332. { Return priority of operator }
  333. { The greater the priority, the higher the precedence }
  334. begin
  335. Case _Operator OF
  336. '(' :
  337. Priority:=0;
  338. '+', '-' :
  339. Priority:=1;
  340. '*', '/','%','<','>' :
  341. Priority:=2;
  342. '|','&','^','~' :
  343. Priority:=0;
  344. else
  345. Message(asmr_e_expr_illegal);
  346. end;
  347. end;
  348. Function TExprParse.Evaluate(Expr : String):longint;
  349. Var
  350. I : LongInt;
  351. Token : String15;
  352. opr : TExprOperator;
  353. begin
  354. Evaluate:=0;
  355. { Reset stacks }
  356. OpTop :=0;
  357. RPNTop:=0;
  358. Token :='';
  359. { nothing to do ? }
  360. if Expr='' then
  361. exit;
  362. For I:=1 to Length(Expr) DO
  363. begin
  364. if Expr[I] in ['0'..'9'] then
  365. begin { Build multi-digit numbers }
  366. Token:=Token + Expr[I];
  367. if I = Length(Expr) then { Send last one to calculator }
  368. RPNCalc(Token,false);
  369. end
  370. else
  371. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  372. begin
  373. if Token <> '' then
  374. begin { Send last built number to calc. }
  375. RPNCalc(Token,false);
  376. Token:='';
  377. end;
  378. Case Expr[I] OF
  379. '(' : OpPush('(',false);
  380. ')' : begin
  381. While OpStack[OpTop].ch <> '(' DO
  382. Begin
  383. OpPop(opr);
  384. RPNCalc(opr.ch,opr.is_prefix);
  385. end;
  386. OpPop(opr); { Pop off and ignore the '(' }
  387. end;
  388. '+','-','~' : Begin
  389. { workaround for -2147483648 }
  390. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  391. begin
  392. token:='-';
  393. expr[i]:='+';
  394. end;
  395. { if start of expression then surely a prefix }
  396. { or if previous char was also an operator }
  397. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  398. OpPush(Expr[I],true)
  399. else
  400. Begin
  401. { Evaluate all higher priority operators }
  402. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  403. Begin
  404. OpPop(opr);
  405. RPNCalc(opr.ch,opr.is_prefix);
  406. end;
  407. OpPush(Expr[I],false);
  408. End;
  409. end;
  410. '*', '/',
  411. '^','|','&',
  412. '%','<','>' : begin
  413. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  414. Begin
  415. OpPop(opr);
  416. RPNCalc(opr.ch,opr.is_prefix);
  417. end;
  418. OpPush(Expr[I],false);
  419. end;
  420. end; { Case }
  421. end
  422. else
  423. Message(asmr_e_expr_illegal); { Handle bad input error }
  424. end;
  425. { Pop off the remaining operators }
  426. While OpTop > 0 do
  427. Begin
  428. OpPop(opr);
  429. RPNCalc(opr.ch,opr.is_prefix);
  430. end;
  431. { The result is stored on the top of the stack }
  432. Evaluate:=RPNPop;
  433. end;
  434. Destructor TExprParse.Done;
  435. Begin
  436. end;
  437. Function CalculateExpression(const expression: string): longint;
  438. var
  439. expr: TExprParse;
  440. Begin
  441. expr.Init;
  442. CalculateExpression:=expr.Evaluate(expression);
  443. expr.Done;
  444. end;
  445. {*************************************************************************}
  446. { String conversions/utils }
  447. {*************************************************************************}
  448. Function EscapeToPascal(const s:string): string;
  449. { converts a C styled string - which contains escape }
  450. { characters to a pascal style string. }
  451. var
  452. i,len : longint;
  453. hs : string;
  454. temp : string;
  455. c : char;
  456. Begin
  457. hs:='';
  458. len:=0;
  459. i:=0;
  460. while (i<length(s)) and (len<255) do
  461. begin
  462. Inc(i);
  463. if (s[i]='\') and (i<length(s)) then
  464. Begin
  465. inc(i);
  466. case s[i] of
  467. '\' :
  468. c:='\';
  469. 'b':
  470. c:=#8;
  471. 'f':
  472. c:=#12;
  473. 'n':
  474. c:=#10;
  475. 'r':
  476. c:=#13;
  477. 't':
  478. c:=#9;
  479. '"':
  480. c:='"';
  481. '0'..'7':
  482. Begin
  483. temp:=s[i];
  484. temp:=temp+s[i+1];
  485. temp:=temp+s[i+2];
  486. inc(i,2);
  487. c:=chr(ValOctal(temp));
  488. end;
  489. 'x':
  490. Begin
  491. temp:=s[i+1];
  492. temp:=temp+s[i+2];
  493. inc(i,2);
  494. c:=chr(ValHexaDecimal(temp));
  495. end;
  496. else
  497. Begin
  498. Message1(asmr_e_escape_seq_ignored,s[i]);
  499. c:=s[i];
  500. end;
  501. end;
  502. end
  503. else
  504. c:=s[i];
  505. inc(len);
  506. hs[len]:=c;
  507. end;
  508. hs[0]:=chr(len);
  509. EscapeToPascal:=hs;
  510. end;
  511. Function ValDecimal(const S:String):longint;
  512. { Converts a decimal string to longint }
  513. var
  514. vs,c : longint;
  515. Begin
  516. vs:=0;
  517. for c:=1 to length(s) do
  518. begin
  519. vs:=vs*10;
  520. if s[c] in ['0'..'9'] then
  521. inc(vs,ord(s[c])-ord('0'))
  522. else
  523. begin
  524. Message1(asmr_e_error_converting_decimal,s);
  525. ValDecimal:=0;
  526. exit;
  527. end;
  528. end;
  529. ValDecimal:=vs;
  530. end;
  531. Function ValOctal(const S:String):longint;
  532. { Converts an octal string to longint }
  533. var
  534. vs,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):longint;
  552. { Converts a binary string to longint }
  553. var
  554. vs,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):longint;
  572. { Converts a binary string to longint }
  573. var
  574. vs,c : longint;
  575. Begin
  576. vs:=0;
  577. for c:=1 to length(s) do
  578. begin
  579. vs:=vs shl 4;
  580. case s[c] of
  581. '0'..'9' :
  582. inc(vs,ord(s[c])-ord('0'));
  583. 'A'..'F' :
  584. inc(vs,ord(s[c])-ord('A')+10);
  585. 'a'..'f' :
  586. inc(vs,ord(s[c])-ord('a')+10);
  587. else
  588. begin
  589. Message1(asmr_e_error_converting_hexadecimal,s);
  590. ValHexadecimal:=0;
  591. exit;
  592. end;
  593. end;
  594. end;
  595. ValHexadecimal:=vs;
  596. end;
  597. Function PadZero(Var s: String; n: byte): Boolean;
  598. Begin
  599. PadZero:=TRUE;
  600. { Do some error checking first }
  601. if Length(s) = n then
  602. exit
  603. else
  604. if Length(s) > n then
  605. Begin
  606. PadZero:=FALSE;
  607. delete(s,n+1,length(s));
  608. exit;
  609. end
  610. else
  611. PadZero:=TRUE;
  612. { Fill it up with the specified character }
  613. fillchar(s[length(s)+1],n-1,#0);
  614. s[0]:=chr(n);
  615. end;
  616. {****************************************************************************
  617. TOperand
  618. ****************************************************************************}
  619. constructor TOperand.init;
  620. begin
  621. size:=S_NO;
  622. hastype:=false;
  623. hasvar:=false;
  624. FillChar(Opr,sizeof(Opr),0);
  625. end;
  626. destructor TOperand.done;
  627. begin
  628. end;
  629. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  630. begin
  631. end;
  632. Procedure TOperand.SetSize(_size:longint;force:boolean);
  633. begin
  634. if force or
  635. ((size = S_NO) and (_size<=extended_size)) then
  636. Begin
  637. case _size of
  638. 1 : size:=S_B;
  639. 2 : size:=S_W{ could be S_IS};
  640. 4 : size:=S_L{ could be S_IL or S_FS};
  641. 8 : size:=S_IQ{ could be S_D or S_FL};
  642. extended_size : size:=S_FX;
  643. end;
  644. end;
  645. end;
  646. Function TOperand.SetupResult:boolean;
  647. Begin
  648. SetupResult:=false;
  649. { replace by correct offset. }
  650. if assigned(procinfo^.returntype.def) and
  651. (procinfo^.returntype.def<>pdef(voiddef)) then
  652. begin
  653. opr.ref.offset:=procinfo^.return_offset;
  654. opr.ref.base:= procinfo^.framepointer;
  655. { always assume that the result is valid. }
  656. procinfo^.funcret_state:=vs_assigned;
  657. SetupResult:=true;
  658. end
  659. else
  660. Message(asmr_e_void_function);
  661. end;
  662. Function TOperand.SetupSelf:boolean;
  663. Begin
  664. SetupSelf:=false;
  665. if assigned(procinfo^._class) then
  666. Begin
  667. opr.typ:=OPR_REFERENCE;
  668. opr.ref.offset:=procinfo^.selfpointer_offset;
  669. opr.ref.base:=procinfo^.framepointer;
  670. opr.ref.options:=ref_selffixup;
  671. SetupSelf:=true;
  672. end
  673. else
  674. Message(asmr_e_cannot_use_SELF_outside_a_method);
  675. end;
  676. Function TOperand.SetupOldEBP:boolean;
  677. Begin
  678. SetupOldEBP:=false;
  679. if lexlevel>normal_function_level then
  680. Begin
  681. opr.typ:=OPR_REFERENCE;
  682. opr.ref.offset:=procinfo^.framepointer_offset;
  683. opr.ref.base:=procinfo^.framepointer;
  684. SetupOldEBP:=true;
  685. end
  686. else
  687. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  688. end;
  689. Function TOperand.SetupVar(const hs:string;GetOffset : boolean): Boolean;
  690. { search and sets up the correct fields in the Instr record }
  691. { for the NON-constant identifier passed to the routine. }
  692. { if not found returns FALSE. }
  693. var
  694. sym : psym;
  695. harrdef : parraydef;
  696. Begin
  697. SetupVar:=false;
  698. { are we in a routine ? }
  699. getsym(hs,false);
  700. sym:=srsym;
  701. if sym=nil then
  702. exit;
  703. case sym^.typ of
  704. varsym :
  705. begin
  706. { we always assume in asm statements that }
  707. { that the variable is valid. }
  708. pvarsym(sym)^.varstate:=vs_used;
  709. inc(pvarsym(sym)^.refs);
  710. case pvarsym(sym)^.owner^.symtabletype of
  711. objectsymtable :
  712. begin
  713. { this is not allowed, because we don't know if the self
  714. register is still free, and loading it first is also
  715. not possible, because this could break code }
  716. { Be TP/Delphi compatible in Delphi or TP modes }
  717. if (m_tp in aktmodeswitches) then
  718. begin
  719. opr.typ:=OPR_CONSTANT;
  720. opr.val:=pvarsym(sym)^.address;
  721. end
  722. { I do not agree here people using method vars should ensure
  723. that %esi is valid there }
  724. else
  725. begin
  726. opr.ref.base:=self_pointer;
  727. opr.ref.offset:=pvarsym(sym)^.address;
  728. end;
  729. hasvar:=true;
  730. SetupVar:=true;
  731. Exit;
  732. end;
  733. unitsymtable,
  734. globalsymtable,
  735. staticsymtable :
  736. opr.ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname);
  737. parasymtable :
  738. begin
  739. { if we only want the offset we don't have to care
  740. the base will be zeroed after ! }
  741. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) or
  742. { this below is wrong because there are two parast
  743. for global functions one of interface the second of
  744. implementation
  745. if (pvarsym(sym)^.owner=procinfo^.def^.parast) or }
  746. GetOffset then
  747. begin
  748. opr.ref.base:=procinfo^.framepointer;
  749. end
  750. else
  751. begin
  752. if (procinfo^.framepointer=R_ESP) and
  753. assigned(procinfo^.parent) and
  754. (lexlevel=pvarsym(sym)^.owner^.symtablelevel+1) and
  755. { same problem as above !!
  756. (procinfo^.parent^.sym^.definition^.parast=pvarsym(sym)^.owner) and }
  757. (lexlevel>normal_function_level) then
  758. opr.ref.base:=procinfo^.parent^.framepointer
  759. else
  760. message1(asmr_e_local_para_unreachable,hs);
  761. end;
  762. opr.ref.offset:=pvarsym(sym)^.address;
  763. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) then
  764. begin
  765. opr.ref.offsetfixup:=aktprocsym^.definition^.parast^.address_fixup;
  766. opr.ref.options:=ref_parafixup;
  767. end
  768. else
  769. begin
  770. opr.ref.offsetfixup:=0;
  771. opr.ref.options:=ref_none;
  772. end;
  773. if (pvarsym(sym)^.varspez=vs_var) or
  774. ((pvarsym(sym)^.varspez=vs_const) and
  775. push_addr_param(pvarsym(sym)^.vartype.def)) then
  776. SetSize(target_os.size_of_pointer,false);
  777. end;
  778. localsymtable :
  779. begin
  780. if (vo_is_external in pvarsym(sym)^.varoptions) then
  781. opr.ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname)
  782. else
  783. begin
  784. { if we only want the offset we don't have to care
  785. the base will be zeroed after ! }
  786. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) or
  787. {if (pvarsym(sym)^.owner=procinfo^.def^.localst) or}
  788. GetOffset then
  789. opr.ref.base:=procinfo^.framepointer
  790. else
  791. begin
  792. if (procinfo^.framepointer=R_ESP) and
  793. assigned(procinfo^.parent) and
  794. (lexlevel=pvarsym(sym)^.owner^.symtablelevel+1) and
  795. {(procinfo^.parent^.sym^.definition^.localst=pvarsym(sym)^.owner) and}
  796. (lexlevel>normal_function_level) then
  797. opr.ref.base:=procinfo^.parent^.framepointer
  798. else
  799. message1(asmr_e_local_para_unreachable,hs);
  800. end;
  801. opr.ref.offset:=-(pvarsym(sym)^.address);
  802. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) then
  803. begin
  804. opr.ref.offsetfixup:=aktprocsym^.definition^.localst^.address_fixup;
  805. opr.ref.options:=ref_localfixup;
  806. end
  807. else
  808. begin
  809. opr.ref.offsetfixup:=0;
  810. opr.ref.options:=ref_none;
  811. end;
  812. end;
  813. end;
  814. end;
  815. case pvarsym(sym)^.vartype.def^.deftype of
  816. orddef,
  817. enumdef,
  818. pointerdef,
  819. floatdef :
  820. SetSize(pvarsym(sym)^.getsize,false);
  821. arraydef :
  822. begin
  823. { for arrays try to get the element size, take care of
  824. multiple indexes }
  825. harrdef:=Parraydef(PVarsym(sym)^.vartype.def);
  826. while assigned(harrdef^.elementtype.def) and
  827. (harrdef^.elementtype.def^.deftype=arraydef) do
  828. harrdef:=parraydef(harrdef^.elementtype.def);
  829. SetSize(harrdef^.elesize,false);
  830. end;
  831. end;
  832. hasvar:=true;
  833. SetupVar:=true;
  834. Exit;
  835. end;
  836. typedconstsym :
  837. begin
  838. opr.ref.symbol:=newasmsymbol(ptypedconstsym(sym)^.mangledname);
  839. case ptypedconstsym(sym)^.typedconsttype.def^.deftype of
  840. orddef,
  841. enumdef,
  842. pointerdef,
  843. floatdef :
  844. SetSize(ptypedconstsym(sym)^.getsize,false);
  845. arraydef :
  846. begin
  847. { for arrays try to get the element size, take care of
  848. multiple indexes }
  849. harrdef:=Parraydef(PTypedConstSym(sym)^.typedconsttype.def);
  850. while assigned(harrdef^.elementtype.def) and
  851. (harrdef^.elementtype.def^.deftype=arraydef) do
  852. harrdef:=parraydef(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 pconstsym(sym)^.consttyp in [constint,constchar,constbool] then
  863. begin
  864. opr.typ:=OPR_CONSTANT;
  865. opr.val:=pconstsym(sym)^.value;
  866. SetupVar:=true;
  867. Exit;
  868. end;
  869. end;
  870. typesym :
  871. begin
  872. if ptypesym(sym)^.restype.def^.deftype in [recorddef,objectdef] then
  873. begin
  874. opr.typ:=OPR_CONSTANT;
  875. opr.val:=0;
  876. SetupVar:=TRUE;
  877. Exit;
  878. end;
  879. end;
  880. procsym :
  881. begin
  882. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  883. Message(asmr_w_calling_overload_func);
  884. opr.typ:=OPR_SYMBOL;
  885. opr.symbol:=newasmsymbol(pprocsym(sym)^.definition^.mangledname);
  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 : pasmsymbol;
  901. begin
  902. SetupDirectVar:=false;
  903. p:=getasmsymbol(hs);
  904. if assigned(p) then
  905. begin
  906. opr.ref.symbol:=p;
  907. hasvar:=true;
  908. SetupDirectVar:=true;
  909. end;
  910. end;
  911. procedure TOperand.InitRef;
  912. {*********************************************************************}
  913. { Description: This routine first check if the opcode is of }
  914. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  915. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  916. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  917. { to point to the default segment. }
  918. {*********************************************************************}
  919. Begin
  920. case opr.typ of
  921. OPR_REFERENCE:
  922. exit;
  923. OPR_NONE: ;
  924. else
  925. Message(asmr_e_invalid_operand_type);
  926. end;
  927. opr.typ := OPR_REFERENCE;
  928. reset_reference(opr.ref);
  929. end;
  930. procedure TOperand.BuildOperand;
  931. begin
  932. abstract;
  933. end;
  934. {****************************************************************************
  935. TInstruction
  936. ****************************************************************************}
  937. constructor TInstruction.init;
  938. Begin
  939. Opcode:=A_NONE;
  940. Opsize:=S_NO;
  941. Condition:=C_NONE;
  942. Ops:=0;
  943. InitOperands;
  944. end;
  945. destructor TInstruction.done;
  946. var
  947. i : longint;
  948. Begin
  949. for i:=1 to 3 do
  950. Dispose(Operands[i],Done);
  951. end;
  952. procedure TInstruction.InitOperands;
  953. var
  954. i : longint;
  955. begin
  956. for i:=1 to 3 do
  957. New(Operands[i],init);
  958. end;
  959. Procedure TInstruction.SwapOperands;
  960. Var
  961. p : POperand;
  962. Begin
  963. case Ops of
  964. 2 :
  965. begin
  966. p:=Operands[1];
  967. Operands[1]:=Operands[2];
  968. Operands[2]:=p;
  969. end;
  970. 3 :
  971. begin
  972. p:=Operands[1];
  973. Operands[1]:=Operands[3];
  974. Operands[3]:=p;
  975. end;
  976. end;
  977. end;
  978. procedure TInstruction.ConcatInstruction(p:PAasmOutput);
  979. begin
  980. abstract;
  981. end;
  982. procedure TInstruction.BuildOpcode;
  983. begin
  984. abstract;
  985. end;
  986. {***************************************************************************
  987. TLocalLabel
  988. ***************************************************************************}
  989. constructor TLocalLabel.Init(const n:string);
  990. begin
  991. inherited InitName(n);
  992. lab:=nil;
  993. emitted:=false;
  994. end;
  995. function TLocalLabel.Getpasmlabel:pasmlabel;
  996. begin
  997. if not assigned(lab) then
  998. begin
  999. getlabel(lab);
  1000. { this label is forced to be used so it's always written }
  1001. inc(lab^.refs);
  1002. end;
  1003. Getpasmlabel:=lab;
  1004. end;
  1005. {***************************************************************************
  1006. TLocalLabelList
  1007. ***************************************************************************}
  1008. procedure LocalLabelEmitted(p:PNamedIndexObject);
  1009. begin
  1010. if not PLocalLabel(p)^.emitted then
  1011. Message1(asmr_e_unknown_label_identifier,p^.name);
  1012. end;
  1013. procedure TLocalLabelList.CheckEmitted;
  1014. begin
  1015. ForEach({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted)
  1016. end;
  1017. function CreateLocalLabel(const s: string; var hl: pasmlabel; emit:boolean):boolean;
  1018. var
  1019. lab : PLocalLabel;
  1020. Begin
  1021. CreateLocalLabel:=true;
  1022. { Check if it already is defined }
  1023. lab:=PLocalLabel(LocalLabelList^.Search(s));
  1024. if not assigned(lab) then
  1025. begin
  1026. new(lab,init(s));
  1027. LocalLabelList^.Insert(lab);
  1028. end;
  1029. { set emitted flag and check for dup syms }
  1030. if emit then
  1031. begin
  1032. if lab^.Emitted then
  1033. begin
  1034. Message1(asmr_e_dup_local_sym,lab^.Name);
  1035. CreateLocalLabel:=false;
  1036. end;
  1037. lab^.Emitted:=true;
  1038. end;
  1039. hl:=lab^.Getpasmlabel;
  1040. end;
  1041. {****************************************************************************
  1042. Symbol table helper routines
  1043. ****************************************************************************}
  1044. Function SearchType(const hs:string): Boolean;
  1045. begin
  1046. getsym(hs,false);
  1047. SearchType:=assigned(srsym) and
  1048. (srsym^.typ=typesym);
  1049. end;
  1050. Function SearchRecordType(const s:string): boolean;
  1051. Begin
  1052. SearchRecordType:=false;
  1053. { Check the constants in symtable }
  1054. getsym(s,false);
  1055. if srsym <> nil then
  1056. Begin
  1057. case srsym^.typ of
  1058. typesym :
  1059. begin
  1060. if ptypesym(srsym)^.restype.def^.deftype in [recorddef,objectdef] then
  1061. begin
  1062. SearchRecordType:=true;
  1063. exit;
  1064. end;
  1065. end;
  1066. end;
  1067. end;
  1068. end;
  1069. Function SearchIConstant(const s:string; var l:longint): boolean;
  1070. {**********************************************************************}
  1071. { Description: Searches for a CONSTANT of name s in either the local }
  1072. { symbol list, then in the global symbol list, and returns the value }
  1073. { of that constant in l. Returns TRUE if successfull, if not found, }
  1074. { or if the constant is not of correct type, then returns FALSE }
  1075. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1076. { respectively. }
  1077. {**********************************************************************}
  1078. Begin
  1079. SearchIConstant:=false;
  1080. { check for TRUE or FALSE reserved words first }
  1081. if s = 'TRUE' then
  1082. Begin
  1083. SearchIConstant:=TRUE;
  1084. l:=1;
  1085. exit;
  1086. end;
  1087. if s = 'FALSE' then
  1088. Begin
  1089. SearchIConstant:=TRUE;
  1090. l:=0;
  1091. exit;
  1092. end;
  1093. { Check the constants in symtable }
  1094. getsym(s,false);
  1095. if srsym <> nil then
  1096. Begin
  1097. case srsym^.typ of
  1098. constsym :
  1099. begin
  1100. if (pconstsym(srsym)^.consttyp in [constord,constint,constchar,constbool]) then
  1101. Begin
  1102. l:=pconstsym(srsym)^.value;
  1103. SearchIConstant:=TRUE;
  1104. exit;
  1105. end;
  1106. end;
  1107. enumsym:
  1108. Begin
  1109. l:=penumsym(srsym)^.value;
  1110. SearchIConstant:=TRUE;
  1111. exit;
  1112. end;
  1113. end;
  1114. end;
  1115. end;
  1116. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1117. { search and returns the offset and size of records/objects of the base }
  1118. { with field name setup in field. }
  1119. { returns FALSE if not found. }
  1120. { used when base is a variable or a typed constant name. }
  1121. var
  1122. st : psymtable;
  1123. harrdef : parraydef;
  1124. sym : psym;
  1125. i : longint;
  1126. base : string;
  1127. Begin
  1128. GetRecordOffsetSize:=FALSE;
  1129. Offset:=0;
  1130. Size:=0;
  1131. i:=pos('.',s);
  1132. if i=0 then
  1133. i:=255;
  1134. base:=Copy(s,1,i-1);
  1135. delete(s,1,i);
  1136. if base='SELF' then
  1137. st:=procinfo^._class^.symtable
  1138. else
  1139. begin
  1140. getsym(base,false);
  1141. sym:=srsym;
  1142. st:=nil;
  1143. { we can start with a var,type,typedconst }
  1144. case sym^.typ of
  1145. varsym :
  1146. begin
  1147. case pvarsym(sym)^.vartype.def^.deftype of
  1148. recorddef :
  1149. st:=precorddef(pvarsym(sym)^.vartype.def)^.symtable;
  1150. objectdef :
  1151. st:=pobjectdef(pvarsym(sym)^.vartype.def)^.symtable;
  1152. end;
  1153. end;
  1154. typesym :
  1155. begin
  1156. case ptypesym(sym)^.restype.def^.deftype of
  1157. recorddef :
  1158. st:=precorddef(ptypesym(sym)^.restype.def)^.symtable;
  1159. objectdef :
  1160. st:=pobjectdef(ptypesym(sym)^.restype.def)^.symtable;
  1161. end;
  1162. end;
  1163. typedconstsym :
  1164. begin
  1165. case ptypedconstsym(sym)^.typedconsttype.def^.deftype of
  1166. recorddef :
  1167. st:=precorddef(ptypedconstsym(sym)^.typedconsttype.def)^.symtable;
  1168. objectdef :
  1169. st:=pobjectdef(ptypedconstsym(sym)^.typedconsttype.def)^.symtable;
  1170. end;
  1171. end;
  1172. end;
  1173. end;
  1174. { now walk all recordsymtables }
  1175. while assigned(st) and (s<>'') do
  1176. begin
  1177. { load next field in base }
  1178. i:=pos('.',s);
  1179. if i=0 then
  1180. i:=255;
  1181. base:=Copy(s,1,i-1);
  1182. delete(s,1,i);
  1183. if st^.symtabletype=objectsymtable then
  1184. sym:=search_class_member(pobjectdef(st^.defowner),base)
  1185. else
  1186. sym:=st^.search(base);
  1187. if not assigned(sym) then
  1188. begin
  1189. GetRecordOffsetSize:=false;
  1190. exit;
  1191. end;
  1192. st:=nil;
  1193. case sym^.typ of
  1194. varsym :
  1195. begin
  1196. inc(Offset,pvarsym(sym)^.address);
  1197. Size:=PVarsym(sym)^.getsize;
  1198. case pvarsym(sym)^.vartype.def^.deftype of
  1199. arraydef :
  1200. begin
  1201. { for arrays try to get the element size, take care of
  1202. multiple indexes }
  1203. harrdef:=Parraydef(PVarsym(sym)^.vartype.def);
  1204. while assigned(harrdef^.elementtype.def) and
  1205. (harrdef^.elementtype.def^.deftype=arraydef) do
  1206. harrdef:=parraydef(harrdef^.elementtype.def);
  1207. size:=harrdef^.elesize;
  1208. end;
  1209. recorddef :
  1210. st:=precorddef(pvarsym(sym)^.vartype.def)^.symtable;
  1211. objectdef :
  1212. st:=pobjectdef(pvarsym(sym)^.vartype.def)^.symtable;
  1213. end;
  1214. end;
  1215. end;
  1216. end;
  1217. GetRecordOffsetSize:=(s='');
  1218. end;
  1219. Function SearchLabel(const s: string; var hl: pasmlabel;emit:boolean): boolean;
  1220. var
  1221. sym : psym;
  1222. hs : string;
  1223. Begin
  1224. hl:=nil;
  1225. SearchLabel:=false;
  1226. { Check for pascal labels, which are case insensetive }
  1227. hs:=upper(s);
  1228. getsym(hs,false);
  1229. sym:=srsym;
  1230. if sym=nil then
  1231. exit;
  1232. case sym^.typ of
  1233. labelsym :
  1234. begin
  1235. hl:=plabelsym(sym)^.lab;
  1236. if emit then
  1237. plabelsym(sym)^.defined:=true
  1238. else
  1239. plabelsym(sym)^.used:=true;
  1240. SearchLabel:=true;
  1241. exit;
  1242. end;
  1243. end;
  1244. end;
  1245. {*************************************************************************}
  1246. { Instruction Generation Utilities }
  1247. {*************************************************************************}
  1248. Procedure ConcatString(p : paasmoutput;s:string);
  1249. {*********************************************************************}
  1250. { PROCEDURE ConcatString(s:string); }
  1251. { Description: This routine adds the character chain pointed to in }
  1252. { s to the instruction linked list. }
  1253. {*********************************************************************}
  1254. Var
  1255. pc: PChar;
  1256. Begin
  1257. getmem(pc,length(s)+1);
  1258. p^.concat(new(pai_string,init_length_pchar(strpcopy(pc,s),length(s))));
  1259. end;
  1260. Procedure ConcatPasString(p : paasmoutput;s:string);
  1261. {*********************************************************************}
  1262. { PROCEDURE ConcatPasString(s:string); }
  1263. { Description: This routine adds the character chain pointed to in }
  1264. { s to the instruction linked list, contrary to ConcatString it }
  1265. { uses a pascal style string, so it conserves null characters. }
  1266. {*********************************************************************}
  1267. Begin
  1268. p^.concat(new(pai_string,init(s)));
  1269. end;
  1270. Procedure ConcatDirect(p : paasmoutput;s:string);
  1271. {*********************************************************************}
  1272. { PROCEDURE ConcatDirect(s:string) }
  1273. { Description: This routine output the string directly to the asm }
  1274. { output, it is only sed when writing special labels in AT&T mode, }
  1275. { and should not be used without due consideration, since it may }
  1276. { cause problems. }
  1277. {*********************************************************************}
  1278. Var
  1279. pc: PChar;
  1280. Begin
  1281. getmem(pc,length(s)+1);
  1282. p^.concat(new(pai_direct,init(strpcopy(pc,s))));
  1283. end;
  1284. Procedure ConcatConstant(p: paasmoutput; value: longint; maxvalue: longint);
  1285. {*********************************************************************}
  1286. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1287. { Description: This routine adds the value constant to the current }
  1288. { instruction linked list. }
  1289. { maxvalue -> indicates the size of the data to initialize: }
  1290. { $ff -> create a byte node. }
  1291. { $ffff -> create a word node. }
  1292. { $ffffffff -> create a dword node. }
  1293. {*********************************************************************}
  1294. Begin
  1295. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1296. Begin
  1297. Message(asmr_e_constant_out_of_bounds);
  1298. { assuming a value of maxvalue }
  1299. value:=maxvalue;
  1300. end;
  1301. if maxvalue = $ff then
  1302. p^.concat(new(pai_const,init_8bit(byte(value))))
  1303. else
  1304. if maxvalue = $ffff then
  1305. p^.concat(new(pai_const,init_16bit(word(value))))
  1306. else
  1307. if maxvalue = longint($ffffffff) then
  1308. p^.concat(new(pai_const,init_32bit(longint(value))));
  1309. end;
  1310. Procedure ConcatConstSymbol(p : paasmoutput;const sym:string;l:longint);
  1311. begin
  1312. p^.concat(new(pai_const_symbol,initname_offset(sym,l)));
  1313. end;
  1314. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  1315. {***********************************************************************}
  1316. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1317. { Description: This routine adds the value constant to the current }
  1318. { instruction linked list. }
  1319. { real_typ -> indicates the type of the real data to initialize: }
  1320. { s32real -> create a single node. }
  1321. { s64real -> create a double node. }
  1322. { s80real -> create an extended node. }
  1323. { s64bit -> create a comp node. }
  1324. { f32bit -> create a fixed node. (not used normally) }
  1325. {***********************************************************************}
  1326. Begin
  1327. case real_typ of
  1328. s32real : p^.concat(new(pai_real_32bit,init(value)));
  1329. s64real : p^.concat(new(pai_real_64bit,init(value)));
  1330. s80real : p^.concat(new(pai_real_80bit,init(value)));
  1331. s64comp : p^.concat(new(pai_comp_64bit,init(value)));
  1332. f32bit : p^.concat(new(pai_const,init_32bit(trunc(value*$10000))));
  1333. end;
  1334. end;
  1335. Procedure ConcatLabel(p: paasmoutput;var l : pasmlabel);
  1336. {*********************************************************************}
  1337. { PROCEDURE ConcatLabel }
  1338. { Description: This routine either emits a label or a labeled }
  1339. { instruction to the linked list of instructions. }
  1340. {*********************************************************************}
  1341. begin
  1342. p^.concat(new(pai_label,init(l)));
  1343. end;
  1344. procedure ConcatAlign(p:paasmoutput;l:longint);
  1345. {*********************************************************************}
  1346. { PROCEDURE ConcatPublic }
  1347. { Description: This routine emits an global definition to the }
  1348. { linked list of instructions.(used by AT&T styled asm) }
  1349. {*********************************************************************}
  1350. begin
  1351. p^.concat(new(pai_align,init(l)));
  1352. end;
  1353. procedure ConcatPublic(p:paasmoutput;const s : string);
  1354. {*********************************************************************}
  1355. { PROCEDURE ConcatPublic }
  1356. { Description: This routine emits an global definition to the }
  1357. { linked list of instructions.(used by AT&T styled asm) }
  1358. {*********************************************************************}
  1359. begin
  1360. p^.concat(new(pai_symbol,initname_global(s,0)));
  1361. end;
  1362. procedure ConcatLocal(p:paasmoutput;const s : string);
  1363. {*********************************************************************}
  1364. { PROCEDURE ConcatLocal }
  1365. { Description: This routine emits an local definition to the }
  1366. { linked list of instructions. }
  1367. {*********************************************************************}
  1368. begin
  1369. p^.concat(new(pai_symbol,initname(s,0)));
  1370. end;
  1371. Procedure ConcatGlobalBss(const s : string;size : longint);
  1372. {*********************************************************************}
  1373. { PROCEDURE ConcatGlobalBss }
  1374. { Description: This routine emits an global datablock to the }
  1375. { linked list of instructions. }
  1376. {*********************************************************************}
  1377. begin
  1378. bsssegment^.concat(new(pai_datablock,init_global(s,size)));
  1379. end;
  1380. Procedure ConcatLocalBss(const s : string;size : longint);
  1381. {*********************************************************************}
  1382. { PROCEDURE ConcatLocalBss }
  1383. { Description: This routine emits a local datablcok to the }
  1384. { linked list of instructions. }
  1385. {*********************************************************************}
  1386. begin
  1387. bsssegment^.concat(new(pai_datablock,init(s,size)));
  1388. end;
  1389. end.
  1390. {
  1391. $Log$
  1392. Revision 1.6 2000-09-24 21:19:51 peter
  1393. * delphi compile fixes
  1394. Revision 1.5 2000/09/24 15:06:26 peter
  1395. * use defines.inc
  1396. Revision 1.4 2000/08/27 16:11:52 peter
  1397. * moved some util functions from globals,cobjects to cutils
  1398. * splitted files into finput,fmodule
  1399. Revision 1.3 2000/08/06 10:42:29 peter
  1400. * merged patches name generation in lib and asm constant eval
  1401. Revision 1.2 2000/07/13 11:32:48 michael
  1402. + removed logs
  1403. }