Skip to content

Commit

Permalink
Added -ssa, -anf, and -ast flags
Browse files Browse the repository at this point in the history
Marked pflua-expand as obsolete; replaced by shell script.
  • Loading branch information
Katerina Barone-Adesi committed Jul 6, 2015
1 parent 5e2c56b commit ab17386
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/pf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function compile_filter(filter_str, opts)
local expr = parse.parse(filter_str)
expr = expand.expand(expr, dlt)
if opts.optimize then expr = optimize.optimize(expr) end
expr = anf.convert_anf(expr)
expr = ssa.convert_ssa(expr)
expr = anf.convert_anf(expr, {optimize=opts.optimize})
expr = ssa.convert_ssa(expr, {optimize=opts.optimize})
if opts.source then return backend.emit_lua(expr) end
return backend.emit_and_load(expr, filter_str)
end
Expand Down
8 changes: 6 additions & 2 deletions src/pf/anf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,12 @@ local function renumber(expr)
return visit(expr)
end

function convert_anf(expr)
return renumber(inline_single_use_variables(cse(lower(expr))))
function convert_anf(expr, opts)
local defaults = {optimize = true}
local opts = utils.parse_opts(opts or {}, defaults)
local anf = lower(expr)
if opts.optimize then anf = inline_single_use_variables(cse(anf)) end
return renumber(anf)
end

function selftest()
Expand Down
9 changes: 6 additions & 3 deletions src/pf/ssa.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local relops = set('<', '<=', '=', '!=', '>=', '>')
--- Control := ['return', Bool|Call] | ['if', Bool, Label, Label] | ['goto',Label]
--- Bool := true | false | Comparison

local function print_ssa(ssa)
function print_ssa(ssa)
local function block_repr(block)
local bindings = { 'bindings' }
for _,binding in ipairs(block.bindings) do
Expand Down Expand Up @@ -300,8 +300,11 @@ local function compute_doms(ssa)
end
end

function convert_ssa(anf)
local ssa = optimize_ssa(lower(anf))
function convert_ssa(anf, opts)
local defaults = {optimize = true}
local opts = utils.parse_opts(opts or {}, defaults)
local ssa = lower(anf)
if opts.optimize then ssa = optimize_ssa(ssa) end
order_blocks(ssa)
add_predecessors(ssa)
compute_idoms(ssa)
Expand Down
50 changes: 43 additions & 7 deletions tools/pflua-compile
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@ local pf = require("pf")
local bpf = require("pf.bpf")
local match = require("pf.match")
local utils = require("pf.utils")
local anf = require("pf.anf")
local ssa = require("pf.ssa")

function usage()
local content = [=[
Usage: pflua-compile [-O0] [--bpf-asm | --bpf-lua | --lua] <expression>
Usage: pflua-compile [-O0] [pipeline] <expression>
Options:
--bpf-asm Print libpcap-generated BPF asm code for the pflang <expression>
--bpf-lua Print Lua code compiled from BPF for the pflang <expression>
--lua Print Lua code compiled directly for the pflang <expression> (DEFAULT)
--match Print Lua code compiled from the pfmatch <expression>
Pipeline options: --bpf-asm, --bpf-lua, --lua, --match, --parse, --anf, --ssa
--bpf-asm Print libpcap-generated BPF asm code for <pflang-expression>
--bpf-lua Print Lua code compiled from BPF for <pflang-expression>
--lua Print Lua code compiled directly for <pflang-expression> (DEFAULT)
--match Print Lua code compiled from the pfmatch <match-expression>
Example match-expression: 'match {tcp => drop}'
--ast Emit the abstract syntax tree for <pflang-expression>
--anf Emit the A-Normal Form IR for <pflang-expression>
--ssa Emit the Single Static Assignment IR for <pflang-expression>
-O0 Disable optimizations. (Optimizations are on by default) ]=]
print(content);
print(content)
os.exit()
end

Expand All @@ -36,7 +45,9 @@ if flags["--help"] or flags["-h"] then
end

-- No code-generation flag defined
if (not(flags["--bpf-asm"] or flags["--bpf-lua"] or flags["--lua"] or flags['--match'])) then
if (not(flags["--bpf-asm"] or flags["--bpf-lua"] or flags["--lua"]
or flags['--match']
or flags["--ast"] or flags["--anf"] or flags["--ssa"])) then
-- Default action
flags["--lua"] = true
end
Expand All @@ -49,14 +60,39 @@ local filter = arg[#arg]
if flags["--bpf-asm"] then
print(pf.compile_filter(filter, {libpcap=true, source=true,
optimize=optimize}))
os.exit(0)
end
if flags["--bpf-lua"] then
print(pf.compile_filter(filter, {bpf=true, source=true,
optimize=optimize}))
os.exit(0)
end
if flags["--lua"] then
print(pf.compile_filter(filter, {source=true, optimize=optimize}))
os.exit(0)
end
if flags["--match"] then
print(match.compile(filter, {source=true, optimize=optimize}))
os.exit(0)
end

local ast = pf.expand.expand(pf.parse.parse(filter), "EN10MB")
if optimize then
ast = pf.optimize.optimize(ast)
end
if flags["--ast"] then
utils.pp(ast)
os.exit(0)
end

local anf_expr = anf.convert_anf(ast, {optimize=optimize})
if flags["--anf"] then
utils.pp(anf_expr)
os.exit(0)
end

local ssa_expr = ssa.convert_ssa(anf_expr, {optimize=optimize})
if flags["--ssa"] then
ssa.print_ssa(ssa_expr)
os.exit(0)
end
39 changes: 4 additions & 35 deletions tools/pflua-expand
Original file line number Diff line number Diff line change
@@ -1,36 +1,5 @@
#!/usr/bin/env luajit
-- -*- lua -*-
#!/bin/bash
echo "This tool is obsolete and will be removed by the end of 2015.
In the meanwhile, the following is provided for compatibility." 1>&2

package.path = package.path .. ";../src/?.lua"

local pf = require("pf")
local utils = require("pf.utils")

local function usage()
local content = [=[
Usage: pflua-expand [-O0] <pflang-expression>
Options:
-O0 Disable optimizations; optimizations are on by default.]=]
print(content);
os.exit()
end

-- Print help
if #arg == 0 then
usage()
end

local flags = utils.set(...)

-- Print help
if flags["--help"] or flags["-h"] then
usage()
end

local filter = arg[#arg]
local expanded = pf.expand.expand(pf.parse.parse(filter), "EN10MB")
if flags["-O0"] then
utils.pp(expanded)
else
utils.pp(pf.optimize.optimize(expanded))
end
./pflua-compile --ast "$@"

0 comments on commit ab17386

Please sign in to comment.