Coder Social home page Coder Social logo

cs201-template's Introduction

CS201-Template

This template is prepared for all three projects: HelloPass, Available Expression Analysis and Reaching Definition Analysis.

LLVM folder is used for installing LLVM. Pass folder contains all three passes for those 3 projects. test folder contains the example input/output for those 3 projects. For example, 1.ll is the input file and 1.out is the output.

In phase2/phase3 test folder, there are two scripts: create_input.sh and test.sh. create_input.sh can generate new input files from C source code and test.sh can easily run your pass using opt tool. For example, if you have 1.c as the source code.

cd test/phase2
sh create_input.sh 1    # this will generate 1.ll for pass input
sh test.sh 1            # this will run LivenessAnalysis pass and generate 1.out as the result (of course you need to implement the pass first)

Pass/HelloPass Code Explanation

  1. The implemented Pass extends from FunctionPass class and overrides runOnFunction(Function &F) function.
  2. runOnFunction(Function &F) function gets called the number of times as many number of functions are present in test code. Name of the function is available using following code snippet.
bool runOnFunction(Function &F) override {
	F.getName();
}
  1. We can iterate over basic blocks of the given function as:
bool runOnFunction(Function &F) override {
	for (auto& basic_block : F)
	{
		...
	}
}
  1. Next, we can iterate over the instructions in a basic block (BB). Note: instructions are in LLVM IR.
bool runOnFunction(Function &F) override {
	for (auto& basic_block : F)
	{
		for (auto& inst : basic_block)
		{
			...
		}
	}
}
  1. Once we get an instruction, then we can cast it as User and iterate over operands of that instruction.
auto* ptr = dyn_cast<User>(&inst);
for (auto it = ptr->op_begin(); it != ptr->op_end(); ++it) 
{
...
}
  1. Use Following API to check whether instruction is a binary operation (Assignment)
if (inst.isBinaryOp())
{
	...
}
  1. Use Following APIs to compare and find operator types
if (inst.isBinaryOp())
{
	inst.getOpcodeName(); //prints OpCode by name such as add, mul etc.
	if(inst.getOpcode() == Instruction::Add)
	{
		errs() << "This is Addition"<<"\n";
	}
	if(inst.getOpcode() == Instruction::Mul)
	{
		errs() << "This is Multiplication"<<"\n";
	}
    // See Other classes Instruction::Sub, Instruction::UDiv, Instruction::SDiv
}
  1. predecessors and successors of a basic block can be easily found by calling those APIs:
bool runOnFunction(Function &F) override {
	for (auto& basic_block : F)
	{
		for (auto *pred: predecessors(&basic_block)) {
		}
		for (auto *succ: successors(&basic_block)) {
		}
	}
	return false;
}
9. 

cs201-template's People

Contributors

xyin014 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.