[Firm] Queries about writing a backend

David Given dg at cowlark.com
Thu Jul 9 22:38:12 CEST 2015


Hello,

I'm interested in the possibility of using libfirm as a compiler for a
CPU architecture I'm working with. I have a few questions.

Firstly, though, playing with the latest code from git, I see that
TEMPLATE is broken:

$ cat test.c
int add(int a, int b, int c) { return a+b+c; }

$ build/debug/cparser -S test.c -bisa=TEMPLATE
Segmentation fault

(Would I be better off working against 1.21? If so, where do I get it?
The download link just points at the git repository.)

Anyway, my questions:

a) How well does the code generator handle situations where there are
multiple ways to lower trees of nodes into instructions? For example, my
CPU architecture has both 2op and 3op forms of some intructions; 2op
forms are encoded as 16-bit instructions, the 3op forms as 32-bit.
However, 2op instructions can only use a limited set of registers. 3op
instructions can use all registers. So, deciding whether to pick a 2op
or a 3op instruction is non-trivial, as it can have a major effect on
the other instructions in the block.

b) Can I have overlapping register classes? (My registers have three
different orthogonal properties which I might want to select for.)

c) Instructions with side effects? e.g. post or pre increment. Is this
just a matter of writing brute force code in *_transform.c to see if I
have the appropriate graph? I notice that the existing backends don't
appear to generate these instructions. (My most complicated instruction
is a
add-constant-to-register-then-compare-with-another-constant-and-compare-and-branch,
all in a single 32-bit instruction. It would be awesome to be able to
support this, but as the possible branch displacement is very small it's
probably hard. I assume that Firm doesn't track instruction positions.)

Here's my stack trace, produced by valgrind:

==15285== Command: build/debug/cparser -S test.c -bisa=TEMPLATE
==15285==
==15285== Source and destination overlap in strcpy(0x9f1b00, 0x9f1b00)
==15285==    at 0x4C2C326: strcpy (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15285==    by 0x41E6E5: setup_isa (target.c:214)
==15285==    by 0x41E7C7: setup_firm_isa (target.c:250)
==15285==    by 0x41EB62: target_setup (target.c:336)
==15285==    by 0x402D53: main (main.c:330)
==15285==
==15285== Invalid read of size 1
==15285==    at 0x4896CE: arch_irn_is_ignore (bearch.h:300)
==15285==    by 0x489BBF: normal_tree_cost (beschednormal.c:116)
==15285==    by 0x489EBE: normal_cost_walker (beschednormal.c:166)
==15285==    by 0x4C9686: irg_walk_2_pre (irgwalk.c:39)
==15285==    by 0x4C972F: irg_walk_2_pre (irgwalk.c:48)
==15285==    by 0x4C96CB: irg_walk_2_pre (irgwalk.c:44)
==15285==    by 0x4C9A43: irg_walk_2 (irgwalk.c:107)
==15285==    by 0x4C9AB2: irg_walk_core (irgwalk.c:115)
==15285==    by 0x4C9B11: irg_walk (irgwalk.c:125)
==15285==    by 0x4C9BA5: irg_walk_graph (irgwalk.c:133)
==15285==    by 0x48A855: sched_normal (beschednormal.c:317)
==15285==    by 0x488F23: be_schedule_graph (besched.c:160)
==15285==  Address 0x19 is not stack'd, malloc'd or (recently) free'd

gdb says that arch_get_irn_register_req() is returning NULL on this node
(unfortunately I can't call dump_ir_block_graph() on it because of an
invalid string):

$1 = {kind = k_ir_node, node_idx = 5, op = 0xa197f0, mode = 0xa13d70,
  in = 0xa587b0, irg = 0xa2b320, visited = 480, link = 0x0, node_nr =
89, o = {
    out = 0x0, n_outs = 0}, dbi = 0x0, loop = 0x0, backend_info = 0x0,
  edge_info = {{outs_head = {next = 0xa4b6f0, prev = 0xa4b5d0},
      edges_built = 1, out_count = 2}, {outs_head = {next = 0xa58768,
        prev = 0xa58768}, edges_built = 0, out_count = 0}}, attr =
{block = {
      block_visited = 0, is_matured = 0, dynamic_ins = 0, marked = 0,
      graph_arr = 0x44525241, dom = {idom = 0x2, next = 0x2, first = 0x0,
        tree_pre_num = 10847152, max_subtree_pre_num = 0, pre_num =
10847648,
        dom_depth = 0}, pdom = {idom = 0x600000004, next = 0xa197f0,
        first = 0xa13830, tree_pre_num = 10848384, max_subtree_pre_num = 0,
        pre_num = 10662688, dom_depth = 0}, backedge = 0x1e0, entity = 0x0,
      phis = 0x5a, execfreq = 0}, cmp = {relation = ir_relation_false},
    cond = {jmp_pred = COND_JMP_PRED_NONE}, con = {tarval = 0x0}, entc = {
      entity = 0x0}, typec = {type = 0x0}, sel = {type = 0x0}, member = {
      entity = 0x0}, call = {exc = {pin_state = 0, throws_exception = 0},
      type = 0x0, callee_arr = 0x44525241}, builtin = {exc = {pin_state
= 0,
        throws_exception = 0}, kind = ir_bk_trap, type = 0x0}, alloc = {
      alignment = 0}, load = {exc = {pin_state = 0, throws_exception = 0},
      volatility = volatility_non_volatile, unaligned = align_is_aligned,
      mode = 0x0, type = 0x44525241}, store = {exc = {pin_state = 0,
        throws_exception = 0}, type = 0x0,
      volatility = volatility_is_volatile, unaligned = align_is_aligned},
    phi = {next = 0x0, u = {backedge = 0x0, pos = 0}, loop = 1}, proj = {
      num = 0}, confirm = {relation = ir_relation_false}, except = {
      pin_state = 0, throws_exception = 0}, copyb = {type = 0x0,
      volatility = volatility_non_volatile}, div = {exc = {pin_state = 0,
        throws_exception = 0}, resmode = 0x0, no_remainder = 65 'A'},
mod = {
      exc = {pin_state = 0, throws_exception = 0}, resmode = 0x0}, assem = {
      exc = {pin_state = 0, throws_exception = 0}, text = 0x0,
      input_constraints = 0x44525241, output_constraints = 0x2,
      clobbers = 0x2}, switcha = {n_outs = 0, table = 0x0}}}

-- 
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│ "There is nothing in the world so dangerous --- and I mean *nothing*
│ --- as a children's story that happens to be true." --- Master Li Kao,
│ _The Bridge of Birds_

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 828 bytes
Desc: OpenPGP digital signature
URL: <http://lists.ira.uni-karlsruhe.de/pipermail/firm/attachments/20150709/e3ef1d41/attachment.sig>


More information about the Firm mailing list