1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
|
{
Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
Does the parsing for the ARM GNU AS styled inline assembler.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
Unit raarmgas;
{$i fpcdefs.inc}
Interface
uses
raatt,raarm,
cpubase;
type
tarmattreader = class(tattreader)
actoppostfix : TOpPostfix;
actwideformat : boolean;
function is_asmopcode(const s: string):boolean;override;
function is_register(const s:string):boolean;override;
procedure handleopcode;override;
procedure BuildReference(oper : tarmoperand);
procedure BuildOperand(oper : tarmoperand);
function TryBuildShifterOp(oper : tarmoperand) : boolean;
procedure BuildOpCode(instr : tarminstruction);
procedure ReadSym(oper : tarmoperand);
procedure ConvertCalljmp(instr : tarminstruction);
end;
Implementation
uses
{ helpers }
cutils,
{ global }
globtype,globals,verbose,
systems,
{ aasm }
cpuinfo,aasmbase,aasmtai,aasmdata,aasmcpu,
{ symtable }
symconst,symbase,symtype,symsym,symtable,
{ parser }
scanner,
procinfo,
itcpugas,
rabase,rautils,
cgbase,cgobj
;
function tarmattreader.is_register(const s:string):boolean;
type
treg2str = record
name : string[2];
reg : tregister;
end;
const
extraregs : array[0..19] of treg2str = (
(name: 'A1'; reg : NR_R0),
(name: 'A2'; reg : NR_R1),
(name: 'A3'; reg : NR_R2),
(name: 'A4'; reg : NR_R3),
(name: 'V1'; reg : NR_R4),
(name: 'V2'; reg : NR_R5),
(name: 'V3'; reg : NR_R6),
(name: 'V4'; reg : NR_R7),
(name: 'V5'; reg : NR_R8),
(name: 'V6'; reg : NR_R9),
(name: 'V7'; reg : NR_R10),
(name: 'V8'; reg : NR_R11),
(name: 'WR'; reg : NR_R7),
(name: 'SB'; reg : NR_R9),
(name: 'SL'; reg : NR_R10),
(name: 'FP'; reg : NR_R11),
(name: 'IP'; reg : NR_R12),
(name: 'SP'; reg : NR_R13),
(name: 'LR'; reg : NR_R14),
(name: 'PC'; reg : NR_R15));
var
i : longint;
begin
result:=inherited is_register(s);
{ reg found?
possible aliases are always 2 char
}
if result or (length(s)<>2) then
exit;
for i:=low(extraregs) to high(extraregs) do
begin
if s=extraregs[i].name then
begin
actasmregister:=extraregs[i].reg;
result:=true;
actasmtoken:=AS_REGISTER;
exit;
end;
end;
end;
procedure tarmattreader.ReadSym(oper : tarmoperand);
var
tempstr, mangledname : string;
typesize,l,k : longint;
begin
tempstr:=actasmpattern;
Consume(AS_ID);
{ typecasting? }
if (actasmtoken=AS_LPAREN) and
SearchType(tempstr,typesize) then
begin
oper.hastype:=true;
Consume(AS_LPAREN);
BuildOperand(oper);
Consume(AS_RPAREN);
if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
oper.SetSize(typesize,true);
end
else
if not oper.SetupVar(tempstr,false) then
Message1(sym_e_unknown_id,tempstr);
{ record.field ? }
if actasmtoken=AS_DOT then
begin
BuildRecordOffsetSize(tempstr,l,k,mangledname,false);
if (mangledname<>'') then
Message(asmr_e_invalid_reference_syntax);
inc(oper.opr.ref.offset,l);
end;
end;
Procedure tarmattreader.BuildReference(oper : tarmoperand);
procedure do_error;
begin
Message(asmr_e_invalid_reference_syntax);
RecoverConsume(false);
end;
procedure test_end(require_rbracket : boolean);
begin
if require_rbracket then begin
if not(actasmtoken=AS_RBRACKET) then
begin
do_error;
exit;
end
else
Consume(AS_RBRACKET);
if (actasmtoken=AS_NOT) then
begin
oper.opr.ref.addressmode:=AM_PREINDEXED;
Consume(AS_NOT);
end;
end;
if not(actasmtoken in [AS_SEPARATOR,AS_end]) then
do_error
else
begin
{$IFDEF debugasmreader}
writeln('TEST_end_FINAL_OK. Created the following ref:');
writeln('oper.opr.ref.shiftimm=',oper.opr.ref.shiftimm);
writeln('oper.opr.ref.shiftmode=',ord(oper.opr.ref.shiftmode));
writeln('oper.opr.ref.index=',ord(oper.opr.ref.index));
writeln('oper.opr.ref.base=',ord(oper.opr.ref.base));
writeln('oper.opr.ref.signindex=',ord(oper.opr.ref.signindex));
writeln('oper.opr.ref.addressmode=',ord(oper.opr.ref.addressmode));
writeln;
{$endIF debugasmreader}
end;
end;
function is_shifter_ref_operation(var a : tshiftmode) : boolean;
begin
a := SM_NONE;
if (actasmpattern='LSL') then
a := SM_LSL
else if (actasmpattern='LSR') then
a := SM_LSR
else if (actasmpattern='ASR') then
a := SM_ASR
else if (actasmpattern='ROR') then
a := SM_ROR
else if (actasmpattern='RRX') then
a := SM_RRX;
is_shifter_ref_operation := not(a=SM_NONE);
end;
procedure read_index_shift(require_rbracket : boolean);
begin
case actasmtoken of
AS_COMMA :
begin
Consume(AS_COMMA);
if not(actasmtoken=AS_ID) then
do_error;
if is_shifter_ref_operation(oper.opr.ref.shiftmode) then
begin
Consume(AS_ID);
if not(oper.opr.ref.shiftmode=SM_RRX) then
begin
if not(actasmtoken=AS_HASH) then
do_error;
Consume(AS_HASH);
oper.opr.ref.shiftimm := BuildConstExpression(false,true);
if (oper.opr.ref.shiftimm<0) or (oper.opr.ref.shiftimm>32) then
do_error;
test_end(require_rbracket);
end;
end
else
begin
do_error;
exit;
end;
end;
AS_RBRACKET :
if require_rbracket then
test_end(require_rbracket)
else
begin
do_error;
exit;
end;
AS_SEPARATOR,AS_END :
if not require_rbracket then
test_end(false)
else
do_error;
else
begin
do_error;
exit;
end;
end;
end;
procedure read_index(require_rbracket : boolean);
var
recname : string;
o_int,s_int : aint;
begin
case actasmtoken of
AS_REGISTER :
begin
oper.opr.ref.index:=actasmregister;
Consume(AS_REGISTER);
read_index_shift(require_rbracket);
exit;
end;
AS_PLUS,AS_MINUS :
begin
if actasmtoken=AS_PLUS then
begin
Consume(AS_PLUS);
end
else
begin
oper.opr.ref.signindex := -1;
Consume(AS_MINUS);
end;
if actasmtoken=AS_REGISTER then
begin
oper.opr.ref.index:=actasmregister;
Consume(AS_REGISTER);
read_index_shift(require_rbracket);
exit;
end
else
begin
do_error;
exit;
end;
test_end(require_rbracket);
exit;
end;
AS_HASH : // constant
begin
Consume(AS_HASH);
o_int := BuildConstExpression(false,true);
if (o_int>4095) or (o_int<-4095) then
begin
Message(asmr_e_constant_out_of_bounds);
RecoverConsume(false);
exit;
end
else
begin
inc(oper.opr.ref.offset,o_int);
test_end(require_rbracket);
exit;
end;
end;
AS_ID :
begin
recname := actasmpattern;
Consume(AS_ID);
BuildRecordOffsetSize(recname,o_int,s_int,recname,false);
if (o_int>4095)or(o_int<-4095) then
begin
Message(asmr_e_constant_out_of_bounds);
RecoverConsume(false);
exit;
end
else
begin
inc(oper.opr.ref.offset,o_int);
test_end(require_rbracket);
exit;
end;
end;
AS_AT:
begin
do_error;
exit;
end;
AS_DOT : // local label
begin
oper.opr.ref.signindex := BuildConstExpression(true,false);
test_end(require_rbracket);
exit;
end;
AS_RBRACKET :
begin
if require_rbracket then
begin
test_end(require_rbracket);
exit;
end
else
begin
do_error; // unexpected rbracket
exit;
end;
end;
AS_SEPARATOR,AS_end :
begin
if not require_rbracket then
begin
test_end(false);
exit;
end
else
begin
do_error;
exit;
end;
end;
else
begin
// unexpected token
do_error;
exit;
end;
end; // case
end;
procedure try_prepostindexed;
begin
Consume(AS_RBRACKET);
case actasmtoken of
AS_COMMA :
begin // post-indexed
Consume(AS_COMMA);
oper.opr.ref.addressmode:=AM_POSTINDEXED;
read_index(false);
exit;
end;
AS_NOT :
begin // pre-indexed
Consume(AS_NOT);
oper.opr.ref.addressmode:=AM_PREINDEXED;
test_end(false);
exit;
end;
else
begin
test_end(false);
exit;
end;
end; // case
end;
var
lab : TASMLABEL;
begin
Consume(AS_LBRACKET);
oper.opr.ref.addressmode:=AM_OFFSET; // assume "neither PRE nor POST inc"
if actasmtoken=AS_REGISTER then
begin
oper.opr.ref.base:=actasmregister;
Consume(AS_REGISTER);
case actasmtoken of
AS_RBRACKET :
begin
try_prepostindexed;
exit;
end;
AS_COMMA :
begin
Consume(AS_COMMA);
read_index(true);
exit;
end;
else
begin
Message(asmr_e_invalid_reference_syntax);
RecoverConsume(false);
end;
end;
end
else
{
if base isn't a register, r15=PC is implied base, so it must be a local label.
pascal constants don't make sense, because implied r15
record offsets probably don't make sense, too (a record offset of code?)
TODO: However, we could make the Stackpointer implied.
}
Begin
case actasmtoken of
AS_ID :
begin
if is_locallabel(actasmpattern) then
begin
CreateLocalLabel(actasmpattern,lab,false);
oper.opr.ref.symbol := lab;
Consume(AS_ID);
test_end(true);
exit;
end
else
begin
// TODO: Stackpointer implied,
Message(asmr_e_invalid_reference_syntax);
RecoverConsume(false);
exit;
end;
end;
else
begin // elsecase
Message(asmr_e_invalid_reference_syntax);
RecoverConsume(false);
exit;
end;
end;
end;
end;
function tarmattreader.TryBuildShifterOp(oper : tarmoperand) : boolean;
procedure handlepara(sm : tshiftmode);
begin
consume(AS_ID);
fillchar(oper.opr,sizeof(oper.opr),0);
oper.opr.typ:=OPR_SHIFTEROP;
oper.opr.shifterop.shiftmode:=sm;
if sm<>SM_RRX then
begin
case actasmtoken of
AS_REGISTER:
begin
oper.opr.shifterop.rs:=actasmregister;
consume(AS_REGISTER);
end;
AS_HASH:
begin
consume(AS_HASH);
oper.opr.shifterop.shiftimm:=BuildConstExpression(false,false);
end;
else
Message(asmr_e_illegal_shifterop_syntax);
end;
end;
end;
begin
result:=true;
if (actasmtoken=AS_ID) then
begin
if (actasmpattern='LSL') then
handlepara(SM_LSL)
else if (actasmpattern='LSR') then
handlepara(SM_LSR)
else if (actasmpattern='ASR') then
handlepara(SM_ASR)
else if (actasmpattern='ROR') then
handlepara(SM_ROR)
else if (actasmpattern='RRX') then
handlepara(SM_ROR)
else
result:=false;
end
else
result:=false;
end;
Procedure tarmattreader.BuildOperand(oper : tarmoperand);
var
expr : string;
typesize,l : longint;
procedure AddLabelOperand(hl:tasmlabel);
begin
if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
is_calljmp(actopcode) then
begin
oper.opr.typ:=OPR_SYMBOL;
oper.opr.symbol:=hl;
end
else
begin
oper.InitRef;
oper.opr.ref.symbol:=hl;
end;
end;
procedure MaybeRecordOffset;
var
mangledname: string;
hasdot : boolean;
l,
toffset,
tsize : longint;
begin
if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
exit;
l:=0;
hasdot:=(actasmtoken=AS_DOT);
if hasdot then
begin
if expr<>'' then
begin
BuildRecordOffsetSize(expr,toffset,tsize,mangledname,false);
if (oper.opr.typ<>OPR_CONSTANT) and
(mangledname<>'') then
Message(asmr_e_wrong_sym_type);
inc(l,toffset);
oper.SetSize(tsize,true);
end;
end;
if actasmtoken in [AS_PLUS,AS_MINUS] then
inc(l,BuildConstExpression(true,false));
case oper.opr.typ of
OPR_LOCAL :
begin
{ don't allow direct access to fields of parameters, because that
will generate buggy code. Allow it only for explicit typecasting }
if hasdot and
(not oper.hastype) and
(tabstractnormalvarsym(oper.opr.localsym).owner.symtabletype=parasymtable) and
(current_procinfo.procdef.proccalloption<>pocall_register) then
Message(asmr_e_cannot_access_field_directly_for_parameters);
inc(oper.opr.localsymofs,l)
end;
OPR_CONSTANT :
inc(oper.opr.val,l);
OPR_REFERENCE :
if (mangledname<>'') then
begin
if (oper.opr.val<>0) then
Message(asmr_e_wrong_sym_type);
oper.opr.typ:=OPR_SYMBOL;
oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname);
end
else
inc(oper.opr.val,l);
OPR_SYMBOL:
Message(asmr_e_invalid_symbol_ref);
else
internalerror(200309221);
end;
end;
function MaybeBuildReference:boolean;
{ Try to create a reference, if not a reference is found then false
is returned }
begin
MaybeBuildReference:=true;
case actasmtoken of
AS_INTNUM,
AS_MINUS,
AS_PLUS:
Begin
oper.opr.ref.offset:=BuildConstExpression(True,False);
if actasmtoken<>AS_LPAREN then
Message(asmr_e_invalid_reference_syntax)
else
BuildReference(oper);
end;
AS_LPAREN:
BuildReference(oper);
AS_ID: { only a variable is allowed ... }
Begin
ReadSym(oper);
case actasmtoken of
AS_end,
AS_SEPARATOR,
AS_COMMA: ;
AS_LPAREN:
BuildReference(oper);
else
Begin
Message(asmr_e_invalid_reference_syntax);
Consume(actasmtoken);
end;
end; {end case }
end;
else
MaybeBuildReference:=false;
end; { end case }
end;
function is_ConditionCode(hs: string): boolean;
var icond: tasmcond;
begin
is_ConditionCode := false;
case actopcode of
A_IT,A_ITE,A_ITT,
A_ITEE,A_ITTE,A_ITET,A_ITTT,
A_ITEEE,A_ITTEE,A_ITETE,A_ITTTE,A_ITEET,A_ITTET,A_ITETT,A_ITTTT:
begin
{ search for condition, conditions are always 2 chars }
if length(hs)>1 then
begin
for icond:=low(tasmcond) to high(tasmcond) do
begin
if copy(hs,1,2)=uppercond2str[icond] then
begin
//actcondition:=icond;
oper.opr.typ := OPR_COND;
oper.opr.cc := icond;
exit(true);
end;
end;
end;
end;
end;
end;
function is_modeflag(hs : string): boolean;
var
i: longint;
flags: tcpumodeflags;
begin
is_modeflag := false;
flags:=[];
hs:=lower(hs);
if (actopcode in [A_CPSID,A_CPSIE]) and (length(hs) >= 1) then
begin
for i:=1 to length(hs) do
begin
case hs[i] of
'a':
Include(flags,mfA);
'f':
Include(flags,mfF);
'i':
Include(flags,mfI);
else
exit;
end;
end;
oper.opr.typ := OPR_MODEFLAGS;
oper.opr.flags := flags;
exit(true);
end;
end;
var
tempreg : tregister;
ireg : tsuperregister;
regtype: tregistertype;
subreg: tsubregister;
hl : tasmlabel;
{ofs : longint;}
registerset : tcpuregisterset;
Begin
expr:='';
case actasmtoken of
AS_LBRACKET: { Memory reference or constant expression }
Begin
oper.InitRef;
BuildReference(oper);
end;
AS_HASH: { Constant expression }
Begin
Consume(AS_HASH);
BuildConstantOperand(oper);
end;
(*
AS_INTNUM,
AS_MINUS,
AS_PLUS:
Begin
{ Constant memory offset }
{ This must absolutely be followed by ( }
oper.InitRef;
oper.opr.ref.offset:=BuildConstExpression(True,False);
if actasmtoken<>AS_LPAREN then
begin
ofs:=oper.opr.ref.offset;
BuildConstantOperand(oper);
inc(oper.opr.val,ofs);
end
else
BuildReference(oper);
end;
*)
AS_ID: { A constant expression, or a Variable ref. }
Begin
if is_modeflag(actasmpattern) then
begin
consume(AS_ID);
end
else
{ Condition code? }
if is_conditioncode(actasmpattern) then
begin
consume(AS_ID);
end
else
{ Local Label ? }
if is_locallabel(actasmpattern) then
begin
CreateLocalLabel(actasmpattern,hl,false);
Consume(AS_ID);
AddLabelOperand(hl);
end
else
{ Check for label }
if SearchLabel(actasmpattern,hl,false) then
begin
Consume(AS_ID);
AddLabelOperand(hl);
end
else
{ probably a variable or normal expression }
{ or a procedure (such as in CALL ID) }
Begin
{ is it a constant ? }
if SearchIConstant(actasmpattern,l) then
Begin
if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
Message(asmr_e_invalid_operand_type);
BuildConstantOperand(oper);
end
else
begin
expr:=actasmpattern;
Consume(AS_ID);
{ typecasting? }
if (actasmtoken=AS_LPAREN) and
SearchType(expr,typesize) then
begin
oper.hastype:=true;
Consume(AS_LPAREN);
BuildOperand(oper);
Consume(AS_RPAREN);
if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
oper.SetSize(typesize,true);
end
else
begin
if not(oper.SetupVar(expr,false)) then
Begin
{ look for special symbols ... }
if expr= '__HIGH' then
begin
consume(AS_LPAREN);
if not oper.setupvar('high'+actasmpattern,false) then
Message1(sym_e_unknown_id,'high'+actasmpattern);
consume(AS_ID);
consume(AS_RPAREN);
end
else
if expr = '__RESULT' then
oper.SetUpResult
else
if expr = '__SELF' then
oper.SetupSelf
else
if expr = '__OLDEBP' then
oper.SetupOldEBP
else
Message1(sym_e_unknown_id,expr);
end;
end;
end;
if actasmtoken=AS_DOT then
MaybeRecordOffset;
{ add a constant expression? }
if (actasmtoken=AS_PLUS) then
begin
l:=BuildConstExpression(true,false);
case oper.opr.typ of
OPR_CONSTANT :
inc(oper.opr.val,l);
OPR_LOCAL :
inc(oper.opr.localsymofs,l);
OPR_REFERENCE :
inc(oper.opr.ref.offset,l);
else
internalerror(200309202);
end;
end
end;
{ Do we have a indexing reference, then parse it also }
if actasmtoken=AS_LPAREN then
BuildReference(oper);
end;
{ Register, a variable reference or a constant reference }
AS_REGISTER:
Begin
{ save the type of register used. }
tempreg:=actasmregister;
Consume(AS_REGISTER);
if (actasmtoken in [AS_end,AS_SEPARATOR,AS_COMMA]) then
Begin
if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
Message(asmr_e_invalid_operand_type);
oper.opr.typ:=OPR_REGISTER;
oper.opr.reg:=tempreg;
end
else if (actasmtoken=AS_NOT) and (actopcode in [A_LDM,A_STM,A_FLDM,A_FSTM]) then
begin
consume(AS_NOT);
oper.opr.typ:=OPR_REFERENCE;
oper.opr.ref.addressmode:=AM_PREINDEXED;
oper.opr.ref.index:=tempreg;
end
else
Message(asmr_e_syn_operand);
end;
{ Registerset }
AS_LSBRACKET:
begin
consume(AS_LSBRACKET);
registerset:=[];
regtype:=R_INVALIDREGISTER;
subreg:=R_SUBNONE;
while true do
begin
if actasmtoken=AS_REGISTER then
begin
include(registerset,getsupreg(actasmregister));
if regtype<>R_INVALIDREGISTER then
begin
if (getregtype(actasmregister)<>regtype) or
(getsubreg(actasmregister)<>subreg) then
Message(asmr_e_mixing_regtypes);
end
else
begin
regtype:=getregtype(actasmregister);
subreg:=getsubreg(actasmregister);
end;
tempreg:=actasmregister;
consume(AS_REGISTER);
if actasmtoken=AS_MINUS then
begin
consume(AS_MINUS);
for ireg:=getsupreg(tempreg) to getsupreg(actasmregister) do
include(registerset,ireg);
consume(AS_REGISTER);
end;
end
else
consume(AS_REGISTER);
if actasmtoken=AS_COMMA then
consume(AS_COMMA)
else
break;
end;
consume(AS_RSBRACKET);
oper.opr.typ:=OPR_REGSET;
oper.opr.regtype:=regtype;
oper.opr.subreg:=subreg;
oper.opr.regset:=registerset;
if (registerset=[]) then
Message(asmr_e_empty_regset);
end;
AS_end,
AS_SEPARATOR,
AS_COMMA: ;
else
Begin
Message(asmr_e_syn_operand);
Consume(actasmtoken);
end;
end; { end case }
end;
{*****************************************************************************
tarmattreader
*****************************************************************************}
procedure tarmattreader.BuildOpCode(instr : tarminstruction);
var
operandnum : longint;
Begin
{ opcode }
if (actasmtoken<>AS_OPCODE) then
Begin
Message(asmr_e_invalid_or_missing_opcode);
RecoverConsume(true);
exit;
end;
{ Fill the instr object with the current state }
with instr do
begin
Opcode:=ActOpcode;
condition:=ActCondition;
oppostfix:=actoppostfix;
wideformat:=actwideformat;
end;
{ We are reading operands, so opcode will be an AS_ID }
operandnum:=1;
Consume(AS_OPCODE);
{ Zero operand opcode ? }
if actasmtoken in [AS_SEPARATOR,AS_end] then
begin
operandnum:=0;
exit;
end;
{ Read the operands }
repeat
case actasmtoken of
AS_COMMA: { Operand delimiter }
Begin
if ((instr.opcode in [A_MOV, A_MVN, A_CMP, A_CMN, A_TST, A_TEQ]) and (operandnum=2)) or
((operandnum=3) and not(instr.opcode in [A_UMLAL,A_UMULL,A_SMLAL,A_SMULL,A_MLA])) then
begin
Consume(AS_COMMA);
if not(TryBuildShifterOp(instr.Operands[operandnum+1] as tarmoperand)) then
Message(asmr_e_illegal_shifterop_syntax);
Inc(operandnum);
end
else
begin
if operandnum>Max_Operands then
Message(asmr_e_too_many_operands)
else
Inc(operandnum);
Consume(AS_COMMA);
end;
end;
AS_SEPARATOR,
AS_end : { End of asm operands for this opcode }
begin
break;
end;
else
BuildOperand(instr.Operands[operandnum] as tarmoperand);
end; { end case }
until false;
instr.Ops:=operandnum;
end;
function tarmattreader.is_asmopcode(const s: string):boolean;
const
{ sorted by length so longer postfixes will match first }
postfix2strsorted : array[1..31] of string[3] = (
'IAD','DBD','FDD','EAD',
'IAS','DBS','FDS','EAS',
'IAX','DBX','FDX','EAX',
'EP','SB','BT','SH',
'IA','IB','DA','DB','FD','FA','ED','EA',
'B','D','E','P','T','H','S');
postfixsorted : array[1..31] of TOpPostfix = (
PF_IAD,PF_DBD,PF_FDD,PF_EAD,
PF_IAS,PF_DBS,PF_FDS,PF_EAS,
PF_IAX,PF_DBX,PF_FDX,PF_EAX,
PF_EP,PF_SB,PF_BT,PF_SH,
PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA,
PF_B,PF_D,PF_E,PF_P,PF_T,PF_H,PF_S);
var
j : longint;
hs : string;
maxlen : longint;
icond : tasmcond;
Begin
{ making s a value parameter would break other assembler readers }
hs:=s;
is_asmopcode:=false;
{ clear op code }
actopcode:=A_None;
actcondition:=C_None;
{ first, handle B else BLS is read wrong }
if ((hs[1]='B') and (length(hs)=3)) then
begin
for icond:=low(tasmcond) to high(tasmcond) do
begin
if copy(hs,2,3)=uppercond2str[icond] then
begin
actopcode:=A_B;
actasmtoken:=AS_OPCODE;
actcondition:=icond;
is_asmopcode:=true;
exit;
end;
end;
end;
maxlen:=max(length(hs),5);
actopcode:=A_NONE;
for j:=maxlen downto 1 do
begin
actopcode:=tasmop(PtrUInt(iasmops.Find(copy(hs,1,j))));
if actopcode<>A_NONE then
begin
actasmtoken:=AS_OPCODE;
{ strip op code }
delete(hs,1,j);
break;
end;
end;
if actopcode=A_NONE then
exit;
{ search for condition, conditions are always 2 chars }
if length(hs)>1 then
begin
for icond:=low(tasmcond) to high(tasmcond) do
begin
if copy(hs,1,2)=uppercond2str[icond] then
begin
actcondition:=icond;
{ strip condition }
delete(hs,1,2);
break;
end;
end;
end;
{ check for postfix }
if length(hs)>0 then
begin
for j:=low(postfixsorted) to high(postfixsorted) do
begin
if copy(hs,1,length(postfix2strsorted[j]))=postfix2strsorted[j] then
begin
actoppostfix:=postfixsorted[j];
{ strip postfix }
delete(hs,1,length(postfix2strsorted[j]));
break;
end;
end;
end;
{ check for format postfix }
if length(hs)>0 then
begin
if upcase(copy(hs,1,2)) = '.W' then
begin
actwideformat:=true;
delete(hs,1,2);
end;
end;
{ if we stripped all postfixes, it's a valid opcode }
is_asmopcode:=length(hs)=0;
end;
procedure tarmattreader.ConvertCalljmp(instr : tarminstruction);
var
newopr : toprrec;
begin
if instr.Operands[1].opr.typ=OPR_REFERENCE then
begin
newopr.typ:=OPR_SYMBOL;
newopr.symbol:=instr.Operands[1].opr.ref.symbol;
newopr.symofs:=instr.Operands[1].opr.ref.offset;
if (instr.Operands[1].opr.ref.base<>NR_NO) or
(instr.Operands[1].opr.ref.index<>NR_NO) then
Message(asmr_e_syn_operand);
instr.Operands[1].opr:=newopr;
end;
end;
procedure tarmattreader.handleopcode;
var
instr : tarminstruction;
begin
instr:=TarmInstruction.Create(TarmOperand);
BuildOpcode(instr);
if is_calljmp(instr.opcode) then
ConvertCalljmp(instr);
{
instr.AddReferenceSizes;
instr.SetInstructionOpsize;
instr.CheckOperandSizes;
}
instr.ConcatInstruction(curlist);
instr.Free;
actoppostfix:=PF_None;
actwideformat:=false;
end;
{*****************************************************************************
Initialize
*****************************************************************************}
const
asmmode_arm_att_info : tasmmodeinfo =
(
id : asmmode_arm_gas;
idtxt : 'GAS';
casmreader : tarmattreader;
);
asmmode_arm_standard_info : tasmmodeinfo =
(
id : asmmode_standard;
idtxt : 'STANDARD';
casmreader : tarmattreader;
);
initialization
RegisterAsmMode(asmmode_arm_att_info);
RegisterAsmMode(asmmode_arm_standard_info);
end.
|