What is a primitive in computer programming?
In computer programming, a primitive is one of a set of fundamental language elements that serve as the foundation for a programming language. Each language supports a core set of primitives that provide the basic building blocks for instructing a processor on how to carry out specific operations. Primitives vary between languages and platforms based on factors such as processor architecture, programming language, and the programmatic level (e.g., machine code versus a high-level language such as Java).
How are primitives used in programming?
Computer processors do not understand source code, whether an assembly language or high-level programming language. They can only understand machine language (binary code consisting of 0s and 1s), and the machine code must conform to the processor's instruction set architecture (ISA), which is part of the processor's microarchitecture. The ISA defines the primitives that are supported by the processor, as well as how to build instructions for that processor. In effect, the primitives indicate what the processor is capable of doing, such as adding data or loading it into a specific location in memory.
Each primitive is associated with a specific operation code (opcode). An opcode is the binary equivalent of the primitive that tells the processor what operation to carry out. The opcode is typically accompanied by operands, which represent the data to be manipulated by the primitive. Operands are defined as part of an instruction in which primitives serve as the operator. As with ISAs, opcodes are specific to the processor architecture. For example, an add primitive might translate to an operand such as 0011, 0100, 0110 or another set of bits, depending on the underlying architecture.
Primitives also have their counterparts in assembly languages, which are also specific to the processor architecture. An assembly language is a type of source code that, like high-level programming languages, is written in plain text and is human readable.
Assembly languages and primitives
An assembly language uses mnemonics to represent an ISA's primitives. Mnemonics are short descriptive words that serve as a foundation for building instructions for the processor. In the x86 assembly language, for example, the term add is a mnemonic that means add, while sub means subtract and inc means increase by one. An assembler translates the mnemonic-based instructions to machine code, according to the ISA's specifications.
Microcode and primitives
Some processors use microcode to carry out their operations. Microcode is integrated into the microarchitecture and is considered an even lower-level language than machine code. As such, it might break down primitive operations defined in the machine code into smaller instructions. Some sources consider these smaller instructions to be microcode primitives. However, there is no real consensus as to whether these operations qualify as their own types of primitives. As with many aspects of technology, the term primitive is sometimes used in different ways to describe similar or dissimilar concepts.
High-level programming languages and primitives
High-level programming languages are sometimes discussed in terms of having their own primitives. In this case, primitives represent the smallest units of programming logic available to a specific language. The term primitive in this context is usually discussed in relationship to data types. For example, the Java data types int, char and Boolean are all considered primitive types. Other Java types are built on the primitive types.
The term primitive might also be used in other ways in a high-level programming language. For example, it might refer to operators, expressions, procedures or other language elements. Regardless of how it's used, a primitive in a high-level language does not necessarily have a direct counterpart in machine language. For example, adding two numbers in a high-level language might represent a single primitive operation in that language, but when translated to machine code, that operation will likely require multiple lower-level primitive operations.