ARM统一汇编语言的语法和解析器? [英] ARM Unified Assembler Language grammar and parser?

查看:1112
本文介绍了ARM统一汇编语言的语法和解析器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在描述一个公开的语法分析器或ARM公司统一汇编语言的 ARM体系结构参考手册A4.2


  

    

本文档使用统一汇编语言(UAL​​)的ARM。这种汇编语言的语法提供了一个规范的形式为所有ARM和Thumb指令。


    
    

UAL描述助记符的语法和各指令的操作数。


  

只要我感兴趣的code解析记忆,每个指令的操作数。例如你怎么可以对这些行定义一个语法?

  ADC {S} {c为C>} {< Q>} {<路>中}<氡>中<室&GT中,<类型> <&卢比GT;
IT {&所述; X> {&所述y与其所连接; {&所述; Z>}}} {&所述; Q>}&下; firstcond>
LDC {L}&所述c取代; &所述;协处理器>中&下; C​​RD>中并[d Rn中>中#+ / - &下; IMM]的计算值{!}


解决方案

如果你需要创建基于一个例子,基于语法的简单解析器,没有什么比ANTLR:

http://www.antlr.org/

ANTLR翻译语法规范到词法和语法分析器code。它更直观比Lexx和Yacc使用。下面的语法包括您在上面指定的内容的一部分,它很容易扩展到你想要做什么:

 语法armasm的;/ * *规则/
程序:(声明| NEWLINE)+;声明:?(ADC(REG',')章,章,章
    | IT firstcond
    | LDC协处理器','C preG(','章','IMM)? (!)? ) 新队;章:'R'INT;
协处理器:'P'INT;
çpreG:'CR'INT;
IMM:#(+| - )? INT;
firstcond:;?/ *令牌* /
ADC:ADC('S')? ;
IT:'IT';
LDC:LDC(L​​)?;INT:[0-9] +;
NEWLINE:'\\ r'?的'\\ n';
WS:[\\ t] + - >跳跃;

从ANTLR网站(OSX说明):

  $ CD的/ usr / local / lib目录
$ wget的http://antlr4.org/download/antlr-4.0-complete.jar
$出口CLASSPATH =:在/ usr / local / lib目录/ ANTLR-4.0-complete.jar:$ CLASSPATH
$别名antlr4 ='Java的罐子/usr/local/lib/antlr-4.0-complete.jar
$别名GRUN =java的org.antlr.v4.runtime.misc.TestRig

然后在语法文件运行:

  antlr4 armasm.g4
javac的*的.java
GRUN armasm的程序 - 树    ADCS R1,R2,R3
    IT?
    LDC P3,CR2,R1,#3
    <&EOF GT;

这产生了解析树分解成记号,规则和数据:


  

(程序(声明ADCS(REG R 1),(REG R 2),(REG R 3)\\ n)(声明IT(firstcond?)\\ n)(声明LDC(协处理器第3页)(C preG CR 2)(REG R 1),(IMM#! - 3)\\ n))


语法还不包括指令条件codeS,也不是在所有(我pssed时间$ P $)的IT指令的细节。 ANTLR产生词法和语法分析器,然后GRUN宏包装他们在一个试验台这样我就可以通过产生的code运行文本片段。生成的API是straightfoward在自己的应用程序中使用。

有关完整性,我在网上找了一个现有的语法,并没有发现之一。你最好的选择可能有拆开gasm并提取其解析器规范,但它不会是UAL语法,这将是GPL如果这对你很重要。如果你只需要处理的指令的子集,那么这是去一个好办法。

Is there a publicly available grammar or parser for ARM's Unified Assembler Language as described in ARM Architecture Reference Manual A4.2

This document uses the ARM Unified Assembler Language (UAL). This assembly language syntax provides a canonical form for all ARM and Thumb instructions.

UAL describes the syntax for the mnemonic and the operands of each instruction.

Simply I'm interested in the code for parsing mnemonic and the operands of each instruction. For example how you could define a grammar for these lines?

ADC{S}{<c>}{<q>} {<Rd>,} <Rn>, <Rm>, <type> <Rs>
IT{<x>{<y>{<z>}}}{<q>} <firstcond>
LDC{L}<c> <coproc>, <CRd>, [<Rn>, #+/-<imm>]{!}

解决方案

If you need to create a simple parser based on an example-based grammar, nothing beats ANTLR:

http://www.antlr.org/

ANTLR translates a grammar specification into lexer and parser code. It's much more intuitive to use than Lexx and Yacc. The grammar below covers part of what you specified above, and it's fairly easy to extend to do what you want:

grammar armasm;

/* Rules */
program: (statement | NEWLINE) +;

statement: (ADC (reg ',')? reg ',' reg ',' reg
    | IT firstcond
    | LDC coproc ',' cpreg (',' reg ','  imm )? ('!')? ) NEWLINE;

reg: 'r' INT;
coproc: 'p' INT;
cpreg: 'cr' INT;
imm: '#' ('+' | '-')? INT;
firstcond: '?';

/* Tokens */
ADC: 'ADC' ('S')? ; 
IT:   'IT';
LDC:  'LDC' ('L')?;

INT: [0-9]+;
NEWLINE: '\r'? '\n';
WS: [ \t]+ -> skip;

From the ANTLR site (OSX instructions):

$ cd /usr/local/lib
$ wget http://antlr4.org/download/antlr-4.0-complete.jar
$ export CLASSPATH=".:/usr/local/lib/antlr-4.0-complete.jar:$CLASSPATH"
$ alias antlr4='java -jar /usr/local/lib/antlr-4.0-complete.jar'
$ alias grun='java org.antlr.v4.runtime.misc.TestRig'

Then on the grammar file run:

antlr4 armasm.g4
javac *.java
grun armasm program -tree

    ADCS r1, r2, r3
    IT ?
    LDC p3, cr2, r1, #3 
    <EOF>

This yields the parse tree broken down into tokens, rules, and data:

(program (statement ADCS (reg r 1) , (reg r 2) , (reg r 3) \n) (statement IT (firstcond ?) \n) (statement LDC (coproc p 3) (cpreg cr 2) (reg r 1) , (imm # - 3) ! \n))

The grammar doesn't yet include the instruction condition codes, nor the details for the IT instruction at all (I'm pressed for time). ANTLR generates a lexer and parser, and then the grun macro wraps them in a test rig so I can run text snippets through the generated code. The generated API is straightfoward to use in your own applications.

For completeness, I looked online for an existing grammar and didn't find one. Your best bet there might be to take apart gasm and extract its parser spec, but it won't be UAL syntax and it will be GPL if that matters to you. If you only need to handle a subset of the instructions then this is a good way to go.

这篇关于ARM统一汇编语言的语法和解析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆