Eliminate functions from a module by inlining functions wherever they are called.
(vl-module-expand-functions x ss) → new-x
This is the top-level routine to eliminates functions from a module. We walk over the expressions in the module. For each function call, we find the called function and process its body into a template consisting of a series of variable declarations and assignments. We then add the contents of that template to the module, with names mangled so as not to conflict with existing names.
We used to preprocess all of the function declarations in the module into templates before walking over the expressions. This could be a performance win because each function is only turned into a template once. However, this couldn't account for functions that were declared in the global scope or imported from packages. For the moment we will just expand each function however many times it is called. If this negatively impacts performance, consider memoizing vl-fnname->template.
Function:
(defun vl-module-expand-functions (x ss) (declare (xargs :guard (and (vl-module-p x) (vl-scopestack-p ss)))) (let ((__function__ 'vl-module-expand-functions)) (declare (ignorable __function__)) (b* ((x (vl-module-fix x)) ((vl-module x) x) ((when (vl-module->hands-offp x)) x) (ss (vl-scopestack-push x ss)) (nf (vl-starting-namefactory x)) (warnings x.warnings) (vacc nil) (aacc nil) ((mv okp1 warnings nf vacc aacc ports portdecls vardecls paramdecls) (vl-module-expand-calls-in-decls x.ports x.portdecls x.vardecls x.paramdecls ss nf vacc aacc warnings)) ((mv okp2 warnings nf vacc aacc assigns modinsts gateinsts alwayses initials) (vl-module-expand-calls-in-nondecls x.assigns x.modinsts x.gateinsts x.alwayses x.initials ss nf vacc aacc warnings)) (- (vl-free-namefactory nf)) (okp (and okp1 okp2)) ((unless okp) (change-vl-module x :warnings warnings)) (x-prime (change-vl-module x :ports ports :portdecls portdecls :vardecls (append-without-guard vacc vardecls) :paramdecls paramdecls :fundecls nil :assigns (append-without-guard aacc assigns) :modinsts modinsts :gateinsts gateinsts :alwayses alwayses :initials initials :warnings warnings)) (allexprs (vl-module-allexprs x-prime)) ((when (vl-exprlist-has-funcalls allexprs)) (b* ((w (make-vl-warning :type :vl-programming-error :msg "In module ~m0, there are still function calls after ~ successfully expanding functions? Found calls to ~&1." :args (list x.name (mergesort (vl-exprlist-funnames allexprs))) :fn 'vl-module-expand-functions :fatalp t))) (change-vl-module x-prime :warnings (cons w warnings))))) x-prime)))
Theorem:
(defthm vl-module-p-of-vl-module-expand-functions (b* ((new-x (vl-module-expand-functions x ss))) (vl-module-p new-x)) :rule-classes :rewrite)