From: Owen Anderson Date: Sat, 20 Oct 2007 05:40:47 +0000 (+0000) Subject: Use getOrInsertFunction() in tutorial 1. This makes for shorter, simpler, and better... X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=2d279f843c68728a336549e3bb2831ce7f2ec5bf;p=oota-llvm.git Use getOrInsertFunction() in tutorial 1. This makes for shorter, simpler, and better example code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43201 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/docs/tutorial/Tutorial1.html b/docs/tutorial/Tutorial1.html index 967e5f55e8d..422f7996952 100644 --- a/docs/tutorial/Tutorial1.html +++ b/docs/tutorial/Tutorial1.html @@ -90,41 +90,22 @@ Module* makeLLVMModule() {
-  // Create a prototype for our function
-  std::vector<const Type*>argTypes;
-  argTypes.push_back(IntegerType::get(32));
-  argTypes.push_back(IntegerType::get(32));
-  argTypes.push_back(IntegerType::get(32));
-
-  FunctionType* functionSig = FunctionType::get(
-  /*return type*/ IntegerType::get(32),
-  /*arg types*/   argTypes,
-  /*varargs*/     false,
-  /*arg attrs*/   FuncTy_0_PAL);
-
-
- -

LLVM has a strong type system, including types for functions. So, before we can create our function, we need to create a FunctionType object to represent our function’s type. There are four things that go into defining a FunctionType: the return type, the arguments’ types, whether the function is varargs, and any attributes attached to the arguments. If you don’t understand the latter two, don’t worry. They’re not important for now.

- -

We construct our FunctionType by first creating a std::vector of Type’s to hold to types of the arguments. In the case of our mul_add function, that means three 32-bit integers. Then, we pass in the return type (another 32-bit integer), our list of argument types, and the varargs and attributes, and we’ve got ourselves a FunctionType.

- -

Now that we have a FunctionType, of course, it would be nice to use it for something...

- -
-
-  Function* mul_add = new Function(
-  /*func type*/ functionSig,
-  /*linkage*/   GlobalValue::ExternalLinkage,
-  /*name*/      "mul_add",
-  /*module*/    mod);
+  Constant* c->getOrInsertFunction("mul_add",
+  /*ret type*/                     IntegerType::get(32),
+  /*args*/                         IntegerType::get(32),
+                                   IntegerType::get(32),
+                                   IntegerType::get(32));
   
+  Function* mul_add = cast<Function>(c);
   mul_add->setCallingConv(CallingConv::C);
 
-

Creating a function is as easy as calling its constructor and passing the appropriate parameters. The first parameter is the function type that we created earlier. The second is the function’s linkage type. This one is important for optimization and linking, but for now we’ll just play it safe and give it external linkage. If you don’t know what to choose, external is probably your safest bet.

+

We construct our Function by calling getOrInsertFunction() on our module, passing in the name, return type, and argument types of the function. In the case of our mul_add function, that means one 32-bit integer for the return value, and three 32-bit integers for the arguments.

-

The third and fourth parameters give the function a name and add it to our module, respectively. In addition, we set the calling convention for our new function to be the C calling convention. This isn’t strictly necessary, but it insures that our new function will interoperate properly with C code, which is a good thing.

+

You'll notice that getOrInsertFunction doesn't actually return a Function*. This is because, if the function already existed, but with a different prototype, getOrInsertFunction will return a cast of the existing function to the desired prototype. Since we know that there's not already a mul_add function, we can safely just cast c to a Function*. + +

In addition, we set the calling convention for our new function to be the C calling convention. This isn’t strictly necessary, but it insures that our new function will interoperate properly with C code, which is a good thing.