From ab17386857d6a3c00d0c8ec56027e4b1a259fcfc Mon Sep 17 00:00:00 2001 From: Katerina Barone-Adesi Date: Sat, 13 Jun 2015 09:59:13 +0100 Subject: [PATCH] Added -ssa, -anf, and -ast flags Marked pflua-expand as obsolete; replaced by shell script. --- src/pf.lua | 4 ++-- src/pf/anf.lua | 8 ++++++-- src/pf/ssa.lua | 9 +++++--- tools/pflua-compile | 50 ++++++++++++++++++++++++++++++++++++++------- tools/pflua-expand | 39 ++++------------------------------- 5 files changed, 61 insertions(+), 49 deletions(-) diff --git a/src/pf.lua b/src/pf.lua index 3c0df8e..f8d10d6 100644 --- a/src/pf.lua +++ b/src/pf.lua @@ -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 diff --git a/src/pf/anf.lua b/src/pf/anf.lua index a36f23c..01108b5 100644 --- a/src/pf/anf.lua +++ b/src/pf/anf.lua @@ -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() diff --git a/src/pf/ssa.lua b/src/pf/ssa.lua index 047d6c0..06791f0 100644 --- a/src/pf/ssa.lua +++ b/src/pf/ssa.lua @@ -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 @@ -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) diff --git a/tools/pflua-compile b/tools/pflua-compile index a62aca4..0870259 100755 --- a/tools/pflua-compile +++ b/tools/pflua-compile @@ -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] +Usage: pflua-compile [-O0] [pipeline] Options: - --bpf-asm Print libpcap-generated BPF asm code for the pflang - --bpf-lua Print Lua code compiled from BPF for the pflang - --lua Print Lua code compiled directly for the pflang (DEFAULT) - --match Print Lua code compiled from the pfmatch + Pipeline options: --bpf-asm, --bpf-lua, --lua, --match, --parse, --anf, --ssa + --bpf-asm Print libpcap-generated BPF asm code for + --bpf-lua Print Lua code compiled from BPF for + --lua Print Lua code compiled directly for (DEFAULT) + + --match Print Lua code compiled from the pfmatch + Example match-expression: 'match {tcp => drop}' + + --ast Emit the abstract syntax tree for + --anf Emit the A-Normal Form IR for + --ssa Emit the Single Static Assignment IR for -O0 Disable optimizations. (Optimizations are on by default) ]=] - print(content); + print(content) os.exit() end @@ -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 @@ -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 diff --git a/tools/pflua-expand b/tools/pflua-expand index 7eeca87..349b445 100755 --- a/tools/pflua-expand +++ b/tools/pflua-expand @@ -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] -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 "$@"